2015-11-24 15:09:46 +00:00
|
|
|
# This file contains all the views that concern the page model
|
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2015-11-25 13:45:18 +00:00
|
|
|
from django.views.generic import ListView, DetailView
|
|
|
|
from django.views.generic.edit import UpdateView
|
2015-11-26 16:40:31 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
|
|
|
from django.utils.decorators import method_decorator
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2015-12-02 15:43:40 +00:00
|
|
|
from core.models import Page, PageRev, LockError
|
2015-11-27 14:39:42 +00:00
|
|
|
from core.views.forms import PagePropForm
|
|
|
|
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2015-11-25 13:45:18 +00:00
|
|
|
class PageListView(ListView):
|
|
|
|
model = Page
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2015-11-25 13:45:18 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PageListView, self).get_context_data(**kwargs)
|
|
|
|
return context
|
|
|
|
|
2015-11-27 14:39:42 +00:00
|
|
|
class PageView(CanViewMixin, DetailView):
|
|
|
|
model = Page
|
2015-11-26 16:40:31 +00:00
|
|
|
|
2015-11-25 13:45:18 +00:00
|
|
|
def get_object(self):
|
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
|
|
|
|
return self.page
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PageView, self).get_context_data(**kwargs)
|
2015-12-02 10:09:50 +00:00
|
|
|
if "page" not in context.keys():
|
|
|
|
context['new_page'] = self.kwargs['page_name']
|
2015-12-08 10:10:29 +00:00
|
|
|
if self.request.user.is_owner(self.object):
|
|
|
|
context['can_edit_prop'] = True
|
|
|
|
if self.request.user.can_edit(self.object):
|
|
|
|
context['can_edit'] = True
|
|
|
|
|
2015-12-02 10:09:50 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
class PageHistView(CanViewMixin, DetailView):
|
|
|
|
model = Page
|
|
|
|
template_name_suffix = '_hist'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
|
|
|
|
return self.page
|
|
|
|
|
|
|
|
class PageRevView(CanViewMixin, DetailView):
|
|
|
|
model = Page
|
|
|
|
template_name = 'core/page_detail.html'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
|
|
|
|
return self.page
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PageRevView, self).get_context_data(**kwargs)
|
|
|
|
if self.page is not None:
|
|
|
|
context['page'] = self.page
|
|
|
|
try:
|
|
|
|
rev = self.page.revisions.get(id=self.kwargs['rev'])
|
|
|
|
context['rev'] = rev
|
|
|
|
except:
|
|
|
|
# By passing, the template will just display the normal page without taking revision into account
|
|
|
|
pass
|
2015-11-24 15:09:46 +00:00
|
|
|
else:
|
2015-11-25 13:45:18 +00:00
|
|
|
context['new_page'] = self.kwargs['page_name']
|
|
|
|
return context
|
|
|
|
|
2015-11-27 14:39:42 +00:00
|
|
|
class PagePropView(CanEditPropMixin, UpdateView):
|
2015-11-25 13:45:18 +00:00
|
|
|
model = Page
|
2015-11-27 14:39:42 +00:00
|
|
|
form_class = PagePropForm
|
2015-11-25 13:45:18 +00:00
|
|
|
template_name_suffix = '_prop'
|
2015-11-24 15:09:46 +00:00
|
|
|
|
2015-11-25 13:45:18 +00:00
|
|
|
def get_object(self):
|
|
|
|
page_name = self.kwargs['page_name']
|
|
|
|
p = Page.get_page_by_full_name(page_name)
|
2015-11-26 10:27:52 +00:00
|
|
|
# Create the page if it does not exists
|
2015-11-25 13:45:18 +00:00
|
|
|
if p == None:
|
|
|
|
parent_name = '/'.join(page_name.split('/')[:-1])
|
|
|
|
name = page_name.split('/')[-1]
|
|
|
|
if parent_name == "":
|
2015-11-30 12:04:46 +00:00
|
|
|
p = Page(name=name)
|
2015-11-25 13:45:18 +00:00
|
|
|
else:
|
|
|
|
parent = Page.get_page_by_full_name(parent_name)
|
2015-11-30 12:04:46 +00:00
|
|
|
p = Page(name=name, parent=parent)
|
2015-11-25 13:45:18 +00:00
|
|
|
self.page = p
|
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
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PagePropView, self).get_context_data(**kwargs)
|
2015-12-02 10:09:50 +00:00
|
|
|
if "page" not in context.keys():
|
2015-11-25 13:45:18 +00:00
|
|
|
context['new_page'] = self.kwargs['page_name']
|
|
|
|
return context
|
|
|
|
|
2015-11-27 14:39:42 +00:00
|
|
|
class PageEditView(CanEditMixin, UpdateView):
|
2015-12-02 10:09:50 +00:00
|
|
|
model = PageRev
|
2015-11-30 12:04:46 +00:00
|
|
|
fields = ['title', 'content',]
|
2015-11-25 13:45:18 +00:00
|
|
|
template_name_suffix = '_edit'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
|
2015-12-02 10:09:50 +00:00
|
|
|
if self.page is not None:
|
|
|
|
# First edit
|
|
|
|
if self.page.revisions.all() is None:
|
|
|
|
rev = PageRev(author=request.user)
|
|
|
|
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):
|
|
|
|
context = super(PageEditView, self).get_context_data(**kwargs)
|
2015-12-02 10:09:50 +00:00
|
|
|
if self.page is not None:
|
|
|
|
context['page'] = self.page
|
2015-11-24 15:09:46 +00:00
|
|
|
else:
|
2015-11-25 13:45:18 +00:00
|
|
|
context['new_page'] = self.kwargs['page_name']
|
|
|
|
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
|
|
|
|
new_rev = PageRev(title=rev.title,
|
|
|
|
content=rev.content,
|
|
|
|
)
|
|
|
|
new_rev.author = self.request.user
|
|
|
|
new_rev.page = self.page
|
|
|
|
form.instance = new_rev
|
|
|
|
return super(PageEditView, self).form_valid(form)
|
|
|
|
|