2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
2024-09-23 08:25:27 +00:00
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
2023-04-04 16:39:45 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2015-11-24 15:09:46 +00:00
|
|
|
# This file contains all the views that concern the page model
|
2016-05-31 11:00:24 +00:00
|
|
|
from django.forms.models import modelform_factory
|
2017-09-12 19:10:32 +00:00
|
|
|
from django.http import Http404
|
2017-10-01 18:52:29 +00:00
|
|
|
from django.shortcuts import redirect
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.views.generic import DetailView, ListView
|
|
|
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from core.models import LockError, Page, PageRev
|
|
|
|
from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin
|
2017-09-12 19:10:32 +00:00
|
|
|
from core.views.forms import MarkdownInput, PageForm, PagePropForm
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2017-09-12 19:10:32 +00:00
|
|
|
class CanEditPagePropMixin(CanEditPropMixin):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().dispatch(request, *args, **kwargs)
|
2017-09-12 19:10:32 +00:00
|
|
|
if self.object.is_club_page:
|
|
|
|
raise Http404
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2016-02-05 15:59:42 +00:00
|
|
|
class PageListView(CanViewMixin, ListView):
|
2015-11-25 13:45:18 +00:00
|
|
|
model = Page
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/page_list.jinja"
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2015-11-27 14:39:42 +00:00
|
|
|
class PageView(CanViewMixin, DetailView):
|
|
|
|
model = Page
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/page_detail.jinja"
|
2015-11-26 16:40:31 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().dispatch(request, *args, **kwargs)
|
2017-10-01 18:52:29 +00:00
|
|
|
if self.object and self.object.need_club_redirection:
|
2018-10-04 19:29:19 +00:00
|
|
|
return redirect("club:club_view", club_id=self.object.club.id)
|
2017-10-01 18:52:29 +00:00
|
|
|
return res
|
|
|
|
|
2015-11-25 13:45:18 +00:00
|
|
|
def get_object(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs["page_name"])
|
2015-11-25 13:45:18 +00:00
|
|
|
return self.page
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2024-10-15 09:36:26 +00:00
|
|
|
if "page" not in context:
|
2018-10-04 19:29:19 +00:00
|
|
|
context["new_page"] = self.kwargs["page_name"]
|
2015-12-02 10:09:50 +00:00
|
|
|
return context
|
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2015-12-02 10:09:50 +00:00
|
|
|
class PageHistView(CanViewMixin, DetailView):
|
|
|
|
model = Page
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/page_hist.jinja"
|
2015-12-02 10:09:50 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().dispatch(request, *args, **kwargs)
|
2017-10-01 18:52:29 +00:00
|
|
|
if self.object.need_club_redirection:
|
2018-10-04 19:29:19 +00:00
|
|
|
return redirect("club:club_hist", club_id=self.object.club.id)
|
2017-10-01 18:52:29 +00:00
|
|
|
return res
|
|
|
|
|
2015-12-02 10:09:50 +00:00
|
|
|
def get_object(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs["page_name"])
|
2015-12-02 10:09:50 +00:00
|
|
|
return self.page
|
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2015-12-02 10:09:50 +00:00
|
|
|
class PageRevView(CanViewMixin, DetailView):
|
|
|
|
model = Page
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/page_detail.jinja"
|
2015-12-02 10:09:50 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().dispatch(request, *args, **kwargs)
|
2023-05-02 11:07:36 +00:00
|
|
|
self.object = self.get_object()
|
|
|
|
|
|
|
|
if self.object is None:
|
|
|
|
return redirect("core:page_create", page_name=self.kwargs["page_name"])
|
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
if self.object.need_club_redirection:
|
2018-10-04 19:29:19 +00:00
|
|
|
return redirect(
|
|
|
|
"club:club_view_rev", club_id=self.object.club.id, rev_id=kwargs["rev"]
|
|
|
|
)
|
2017-10-01 18:52:29 +00:00
|
|
|
return res
|
|
|
|
|
2024-10-15 09:36:26 +00:00
|
|
|
def get_object(self, *args, **kwargs):
|
2018-10-04 19:29:19 +00:00
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs["page_name"])
|
2015-12-02 10:09:50 +00:00
|
|
|
return self.page
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2024-10-15 09:36:26 +00:00
|
|
|
if not self.page:
|
|
|
|
return context | {"new_page": self.kwargs["page_name"]}
|
|
|
|
context["page"] = self.page
|
|
|
|
context["rev"] = self.page.revisions.filter(id=self.kwargs["rev"]).first()
|
2015-11-25 13:45:18 +00:00
|
|
|
return context
|
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2016-11-05 12:37:30 +00:00
|
|
|
class PageCreateView(CanCreateMixin, CreateView):
|
2016-05-31 11:00:24 +00:00
|
|
|
model = Page
|
2017-09-12 19:10:32 +00:00
|
|
|
form_class = PageForm
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/page_prop.jinja"
|
2016-05-31 11:00:24 +00:00
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
init = {}
|
2024-10-15 09:36:26 +00:00
|
|
|
if "page" in self.request.GET:
|
2018-10-04 19:29:19 +00:00
|
|
|
page_name = self.request.GET["page"]
|
|
|
|
parent_name = "/".join(page_name.split("/")[:-1])
|
2016-05-31 11:00:24 +00:00
|
|
|
parent = Page.get_page_by_full_name(parent_name)
|
|
|
|
if parent is not None:
|
2018-10-04 19:29:19 +00:00
|
|
|
init["parent"] = parent.id
|
|
|
|
init["name"] = page_name.split("/")[-1]
|
2016-05-31 11:00:24 +00:00
|
|
|
return init
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2018-10-04 19:29:19 +00:00
|
|
|
context["new_page"] = True
|
2016-05-31 11:00:24 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.set_lock(self.request.user)
|
2024-06-27 12:46:43 +00:00
|
|
|
ret = super().form_valid(form)
|
2016-12-14 17:05:19 +00:00
|
|
|
return ret
|
2016-05-31 11:00:24 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2017-09-12 19:10:32 +00:00
|
|
|
class PagePropView(CanEditPagePropMixin, UpdateView):
|
2015-11-25 13:45:18 +00:00
|
|
|
model = Page
|
2017-09-12 19:10:32 +00:00
|
|
|
form_class = PagePropForm
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/page_prop.jinja"
|
|
|
|
slug_field = "_full_name"
|
|
|
|
slug_url_kwarg = "page_name"
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2024-10-15 09:36:26 +00:00
|
|
|
def get_object(self, queryset=None):
|
|
|
|
self.page = super().get_object()
|
2015-12-02 15:43:40 +00:00
|
|
|
try:
|
|
|
|
self.page.set_lock_recursive(self.request.user)
|
|
|
|
except LockError as e:
|
|
|
|
raise e
|
2015-11-25 13:45:18 +00:00
|
|
|
return self.page
|
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
class PageEditViewBase(CanEditMixin, UpdateView):
|
2015-12-02 10:09:50 +00:00
|
|
|
model = PageRev
|
2018-10-04 19:29:19 +00:00
|
|
|
form_class = modelform_factory(
|
|
|
|
model=PageRev, fields=["title", "content"], widgets={"content": MarkdownInput}
|
|
|
|
)
|
|
|
|
template_name = "core/pagerev_edit.jinja"
|
2015-11-25 13:45:18 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs["page_name"])
|
2017-10-01 18:52:29 +00:00
|
|
|
return self._get_revision()
|
|
|
|
|
|
|
|
def _get_revision(self):
|
2015-12-02 10:09:50 +00:00
|
|
|
if self.page is not None:
|
|
|
|
# First edit
|
|
|
|
if self.page.revisions.all() is None:
|
2017-10-01 18:52:29 +00:00
|
|
|
rev = PageRev(author=self.request.user)
|
2015-12-02 10:09:50 +00:00
|
|
|
rev.save()
|
|
|
|
self.page.revisions.add(rev)
|
2015-12-02 15:43:40 +00:00
|
|
|
try:
|
|
|
|
self.page.set_lock(self.request.user)
|
|
|
|
except LockError as e:
|
|
|
|
raise e
|
2015-12-09 10:22:50 +00:00
|
|
|
return self.page.revisions.last()
|
2015-12-02 10:09:50 +00:00
|
|
|
return None
|
2015-11-25 13:45:18 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2015-12-02 10:09:50 +00:00
|
|
|
if self.page is not None:
|
2018-10-04 19:29:19 +00:00
|
|
|
context["page"] = self.page
|
2015-11-24 15:09:46 +00:00
|
|
|
else:
|
2018-10-04 19:29:19 +00:00
|
|
|
context["new_page"] = self.kwargs["page_name"]
|
2015-11-25 13:45:18 +00:00
|
|
|
return context
|
|
|
|
|
2015-12-02 10:09:50 +00:00
|
|
|
def form_valid(self, form):
|
2015-12-02 15:43:40 +00:00
|
|
|
# TODO : factor that, but first make some tests
|
2015-12-02 10:09:50 +00:00
|
|
|
rev = form.instance
|
2018-10-04 19:29:19 +00:00
|
|
|
new_rev = PageRev(title=rev.title, content=rev.content)
|
2015-12-02 10:09:50 +00:00
|
|
|
new_rev.author = self.request.user
|
|
|
|
new_rev.page = self.page
|
|
|
|
form.instance = new_rev
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().form_valid(form)
|
2017-10-01 18:52:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PageEditView(PageEditViewBase):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
res = super().dispatch(request, *args, **kwargs)
|
2017-10-06 15:44:41 +00:00
|
|
|
if self.object and self.object.page.need_club_redirection:
|
2018-10-04 19:29:19 +00:00
|
|
|
return redirect("club:club_edit_page", club_id=self.object.page.club.id)
|
2017-10-01 18:52:29 +00:00
|
|
|
return res
|
2015-12-02 10:09:50 +00:00
|
|
|
|
2017-03-08 12:25:23 +00:00
|
|
|
|
2017-09-12 19:10:32 +00:00
|
|
|
class PageDeleteView(CanEditPagePropMixin, DeleteView):
|
2017-03-08 12:25:23 +00:00
|
|
|
model = Page
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/delete_confirm.jinja"
|
|
|
|
pk_url_kwarg = "page_id"
|
2017-03-08 12:25:23 +00:00
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse_lazy("core:page_list")
|