Project

General

Profile

Download (3.76 KB) Statistics
| Branch: | Tag: | Revision:

root / corbo / views.py @ e090253d

1
from datetime import datetime
2

    
3
from django.conf import settings
4
from django.core.urlresolvers import reverse
5
from django.views.generic import CreateView, UpdateView, DeleteView, \
6
                                 ListView, TemplateView
7
from django.contrib.syndication.views import Feed
8
from django.utils.feedgenerator import Atom1Feed
9
from django.utils.http import urlencode
10

    
11
import models
12
from .forms import AnnounceForm, CategoryForm
13

    
14
class HomepageView(ListView):
15
    model = models.Announce
16
    template_name = 'corbo/homepage.html'
17

    
18
homepage = HomepageView.as_view()
19

    
20
class AnnounceCreateView(CreateView):
21
    form_class = AnnounceForm
22
    template_name = 'corbo/announce_form.html'
23

    
24
    def get_success_url(self):
25
        """
26
        redirect to the category page of the new created announce
27
        """
28
        return reverse('manage') + '?' + urlencode({'category': self.object.category.id})
29

    
30
add_announce = AnnounceCreateView.as_view()
31

    
32
class AnnounceEditView(UpdateView):
33
    model = models.Announce
34
    form_class = AnnounceForm
35

    
36
    def get_success_url(self):
37
        return reverse('manage') + '?' + urlencode({'category': self.object.category.id})
38

    
39
edit_announce = AnnounceEditView.as_view()
40

    
41
class AnnounceDeleteView(DeleteView):
42
    model = models.Announce
43

    
44
    def get_success_url(self):
45
        return self.request.META['HTTP_REFERER'] or \
46
            reverse('manage') + '?' + urlencode({'category': self.object.category.id})
47

    
48
delete_announce = AnnounceDeleteView.as_view()
49

    
50
class CategoryCreateView(CreateView):
51
    form_class = CategoryForm
52
    template_name = 'corbo/category_form.html'
53

    
54
    def get_success_url(self):
55
        return reverse('manage')
56

    
57
    def form_valid(self, form):
58
        form.save()
59
        return super(CategoryCreateView, self).form_valid(form)
60

    
61
add_category = CategoryCreateView.as_view()
62

    
63
class CategoryEditView(UpdateView):
64
    form_class = CategoryForm
65
    model = models.Category
66

    
67
    def get_success_url(self):
68
        return self.request.META['HTTP_REFERER'] or \
69
            reverse('manage') + '?' + urlencode({'category': self.object.id})
70

    
71
edit_category = CategoryEditView.as_view()
72

    
73
class DeleteView(DeleteView):
74
    model = models.Category
75

    
76
    def get_success_url(self):
77
        return reverse('manage')
78

    
79
delete_category = DeleteView.as_view()
80

    
81
class ManageView(ListView):
82
    paginate_by = settings.ANNOUNCES_PER_PAGE
83
    template_name = 'corbo/manage.html'
84
    model = models.Announce
85

    
86
    def get_queryset(self):
87
        queryset = super(ManageView, self).get_queryset()
88
        if self.request.GET.get('category'):
89
            queryset = queryset.filter(category__id=self.request.GET['category'])
90
        return queryset
91

    
92
    def get_context_data(self, **kwargs):
93
        context = super(ManageView, self).get_context_data(**kwargs)
94
        context['categories'] = models.Category.objects.all().order_by('-ctime')
95
        if self.request.GET.get('category'):
96
            try:
97
                context['category_id'] = int(self.request.GET['category'])
98
            except:
99
                pass
100
        return context
101

    
102
manage = ManageView.as_view()
103

    
104
class AtomView(Feed):
105
    title = settings.RSS_TITLE
106
    description = settings.RSS_DESCRIPTION
107
    link = settings.RSS_LINK
108
    feed_item_link_template = settings.RSS_LINK_TEMPLATE
109
    feed_type = Atom1Feed
110

    
111
    def __init__(self, **kwargs):
112
        self.__dict__.update(kwargs)
113

    
114
    def items(self):
115
        return models.Announce.objects.exclude(expiration_time__lt=datetime.now()).order_by('-publication_time')
116

    
117
    def item_title(self, item):
118
        return item.title
119

    
120
    def item_description(self, item):
121
        return item.text
122

    
123
    def item_link(self, item):
124
        return self.feed_item_link_template.format(item.pk)
125

    
126
    def item_pubdate(self, item):
127
        return item.publication_time or item.m_time
128

    
129
atom = AtomView()
(10-10/11)