-
Notifications
You must be signed in to change notification settings - Fork 99
/
views.py
91 lines (67 loc) · 2.89 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# -*- coding: utf-8 -*-
"""Handle the Views of the Homepage and others."""
from django.http import Http404
from django.shortcuts import render
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.list import MultipleObjectMixin
from django.utils.translation import ugettext_lazy as _
from jobs.models import Job
from news.models import NewsArticle
def homepage(request):
news = NewsArticle.objects.order_by('-created')[:3]
jobs = Job.objects.order_by('-created')[:3]
return render(request, 'community/index.html',
{'news': news, 'jobs': jobs})
def learning(request):
title = _('Aprendiendo Python')
return render(request, 'special_page.html', {'title': title, 'slug': 'AprendiendoPython'})
def about_pyar(request):
title = _('Acerca de PyAr')
return render(request, 'special_page.html', {'title': title, 'slug': 'QuienesSomos'})
def members(request):
title = _('¿Dónde viven los miembros de PyAr?')
return render(request, 'special_page.html', {'title': title, 'slug': 'MiembrosDePyAr'})
def mailing_list(request):
title = _('Lista de Correo')
return render(request, 'special_page.html', {'title': title, 'slug': 'ListaDeCorreo'})
class OwnedObject(SingleObjectMixin):
"""An object that needs to verify current user ownership
before allowing manipulation. """
def get_object(self, *args, **kwargs):
obj = super(OwnedObject, self).get_object(*args, **kwargs)
try:
if not obj.owner == self.request.user:
raise Http404()
except AttributeError:
pass
return obj
class FilterableList(MultipleObjectMixin):
"""A list of objects whose queryset depends on excluded
and included tags in the request. """
def dispatch(self, request, *args, **kwargs):
self.included_tags = []
self.excluded_tags = []
for k, v in request.GET.items():
if k.startswith('tag_'):
if v == '1':
self.included_tags.append(k[4:])
elif v == '2':
self.excluded_tags.append(k[4:])
return super(FilterableList, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
object_list = self.model.objects.order_by('-created')
included = self.included_tags
excluded = self.excluded_tags
if included:
object_list = object_list.filter(
tags__name__in=included).distinct()
if excluded:
object_list = object_list.exclude(
tags__name__in=excluded).distinct()
return object_list
def get_context_data(self, **kwargs):
context = super(FilterableList, self).get_context_data(**kwargs)
context['tags'] = self.model.tags.all()
context['included'] = self.included_tags
context['excluded'] = self.excluded_tags
return context