page.py 5.79 KB
Newer Older
Skia's avatar
Skia committed
1 2
# This file contains all the views that concern the page model
from django.shortcuts import render, redirect, get_object_or_404
3
from django.views.generic import ListView, DetailView
Skia's avatar
Skia committed
4
from django.views.generic.edit import UpdateView, CreateView
5 6
from django.contrib.auth.decorators import login_required, permission_required
from django.utils.decorators import method_decorator
Skia's avatar
Skia committed
7 8
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
Skia's avatar
Skia committed
9

Skia's avatar
Skia committed
10
from core.models import Page, PageRev, LockError
11 12
from core.views.forms import PagePropForm
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
Skia's avatar
Skia committed
13

Skia's avatar
Skia committed
14
class PageListView(CanViewMixin, ListView):
15
    model = Page
Skia's avatar
Skia committed
16
    template_name = 'core/page_list.jinja'
Skia's avatar
Skia committed
17

18 19 20 21
    def get_context_data(self, **kwargs):
        context = super(PageListView, self).get_context_data(**kwargs)
        return context

22 23
class PageView(CanViewMixin, DetailView):
    model = Page
Skia's avatar
Skia committed
24
    template_name = 'core/page_detail.jinja'
25

26 27 28 29 30 31
    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)
32 33 34 35 36 37
        if "page" not in context.keys():
            context['new_page'] = self.kwargs['page_name']
        return context

class PageHistView(CanViewMixin, DetailView):
    model = Page
Skia's avatar
Skia committed
38
    template_name = 'core/page_hist.jinja'
39 40 41 42 43 44 45

    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
Skia's avatar
Skia committed
46
    template_name = 'core/page_detail.jinja'
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    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
Skia's avatar
Skia committed
62
        else:
63 64 65
            context['new_page'] = self.kwargs['page_name']
        return context

Skia's avatar
Skia committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
class PageCreateView(CanEditPropMixin, CreateView):
    model = Page
    form_class = modelform_factory(Page,
            fields = ['parent', 'name', 'owner_group', 'edit_groups', 'view_groups', ],
            widgets={
                'edit_groups':CheckboxSelectMultiple,
                'view_groups':CheckboxSelectMultiple,
                })
    template_name = 'core/page_prop.jinja'

    def get_initial(self):
        init = {}
        if 'page' in self.request.GET.keys():
            page_name = self.request.GET['page']
            parent_name = '/'.join(page_name.split('/')[:-1])
            parent = Page.get_page_by_full_name(parent_name)
            if parent is not None:
                init['parent'] = parent.id
            init['name'] = page_name.split('/')[-1]
        return init

    def get_context_data(self, **kwargs):
        context = super(PageCreateView, self).get_context_data(**kwargs)
        context['new_page'] = True
        return context

    def form_valid(self, form):
        form.instance.set_lock(self.request.user)
        return super(PageCreateView, self).form_valid(form)

96
class PagePropView(CanEditPropMixin, UpdateView):
97
    model = Page
Skia's avatar
Skia committed
98 99 100 101 102 103
    form_class = modelform_factory(Page,
            fields = ['parent', 'name', 'owner_group', 'edit_groups', 'view_groups', ],
            widgets={
                'edit_groups':CheckboxSelectMultiple,
                'view_groups':CheckboxSelectMultiple,
                })
Skia's avatar
Skia committed
104
    template_name = 'core/page_prop.jinja'
Skia's avatar
Skia committed
105 106
    slug_field = '_full_name'
    slug_url_kwarg = 'page_name'
Skia's avatar
Skia committed
107

108
    def get_object(self):
Skia's avatar
Skia committed
109
        o = super(PagePropView, self).get_object()
Skia's avatar
Skia committed
110
        # Create the page if it does not exists
Skia's avatar
Skia committed
111 112 113 114 115 116 117 118 119
        #if p == None:
        #    parent_name = '/'.join(page_name.split('/')[:-1])
        #    name = page_name.split('/')[-1]
        #    if parent_name == "":
        #        p = Page(name=name)
        #    else:
        #        parent = Page.get_page_by_full_name(parent_name)
        #        p = Page(name=name, parent=parent)
        self.page = o
Skia's avatar
Skia committed
120 121 122 123
        try:
            self.page.set_lock_recursive(self.request.user)
        except LockError as e:
            raise e
124 125
        return self.page

126
class PageEditView(CanEditMixin, UpdateView):
127
    model = PageRev
128
    fields = ['title', 'content',]
Skia's avatar
Skia committed
129
    template_name = 'core/pagerev_edit.jinja'
130 131 132

    def get_object(self):
        self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
133 134 135 136 137 138
        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)
Skia's avatar
Skia committed
139 140 141 142
            try:
                self.page.set_lock(self.request.user)
            except LockError as e:
                raise e
Skia's avatar
Skia committed
143
            return self.page.revisions.last()
144
        return None
145 146 147

    def get_context_data(self, **kwargs):
        context = super(PageEditView, self).get_context_data(**kwargs)
148 149
        if self.page is not None:
            context['page'] = self.page
Skia's avatar
Skia committed
150
        else:
151 152 153
            context['new_page'] = self.kwargs['page_name']
        return context

154
    def form_valid(self, form):
Skia's avatar
Skia committed
155
        # TODO : factor that, but first make some tests
156 157 158 159 160 161 162 163 164
        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)