page.py 2.7 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
4
from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView
Skia's avatar
Skia committed
5
6
7

from core.models import Page

8
9
class PageListView(ListView):
    model = Page
Skia's avatar
Skia committed
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    def get_context_data(self, **kwargs):
        context = super(PageListView, self).get_context_data(**kwargs)
        return context

class PageView(DetailView):
    model = Page

    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)
        if "page" in context.keys():
            context['tests'] = "PAGE_FOUND : "+context['page'].title
Skia's avatar
Skia committed
26
        else:
27
28
29
30
31
32
33
34
            context['tests'] = "PAGE_NOT_FOUND"
            context['new_page'] = self.kwargs['page_name']
        return context

class PagePropView(UpdateView):
    model = Page
    fields = ['parent', 'name', 'owner_group', 'edit_group', 'view_group', ]
    template_name_suffix = '_prop'
Skia's avatar
Skia committed
35

36
37
38
39
40
41
    def __init__(self, *args, **kwargs):
        super(PagePropView, self).__init__(*args, **kwargs)

    def get_object(self):
        page_name = self.kwargs['page_name']
        p = Page.get_page_by_full_name(page_name)
Skia's avatar
Skia committed
42
        # Create the page if it does not exists
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        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 = p
        return self.page

    def get_context_data(self, **kwargs):
        context = super(PagePropView, self).get_context_data(**kwargs)
        if "page" in context.keys():
            context['tests'] = "PAGE_FOUND : "+context['page'].title
Skia's avatar
Skia committed
58
        else:
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            context['tests'] = "PAGE_NOT_FOUND"
            context['new_page'] = self.kwargs['page_name']
        return context

class PageEditView(UpdateView):
    model = Page
    fields = ['title', 'content',]
    template_name_suffix = '_edit'

    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(PageEditView, self).get_context_data(**kwargs)
        if "page" in context.keys():
            context['tests'] = "PAGE_FOUND : "+context['page'].title
Skia's avatar
Skia committed
76
        else:
77
78
79
80
            context['tests'] = "PAGE_NOT_FOUND"
            context['new_page'] = self.kwargs['page_name']
        return context