3a7cb83dfbb4ed2dfbb301a52f0ffab29111f3d6
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / rss.py
1 # -*- encoding: utf-8 -*-
2
3 from datetime import datetime, date, timedelta
4 from dateutil.parser import parse as parse_date
5 from dateutil.tz import tzlocal, tzutc
6
7 import pytz
8 from django.contrib.syndication.views import Feed
9 from django.core.urlresolvers import reverse
10 from django.http import Http404
11 from django.shortcuts import get_object_or_404
12
13 from auf_savoirs_en_partage.chercheurs.forms import ChercheurSearchForm
14 from auf_savoirs_en_partage.chercheurs.models import Groupe, Message
15 from auf_savoirs_en_partage.savoirs.forms import \
16 RessourceSearchForm, ActualiteSearchForm, EvenementSearchForm
17 from auf_savoirs_en_partage.sitotheque.forms import SiteSearchForm
18
19
20 class FilChercheurs(Feed):
21 title = "Savoirs en partage - chercheurs"
22 link = "/chercheurs/"
23 description = \
24 "Fiches de chercheurs mises à jour récemment sur " \
25 "Savoirs en partage"
26
27 def get_object(self, request):
28 search_form = ChercheurSearchForm(request.GET)
29 if search_form.is_valid():
30 return search_form.save(commit=False)
31 else:
32 raise Http404
33
34 def items(self, search):
35 min_date = date.today() - timedelta(days=30)
36 return search.run(min_date=min_date).order_by('-date_modification')
37
38 def item_title(self, chercheur):
39 return unicode(chercheur)
40
41 def item_description(self, chercheur):
42 return chercheur.etablissement_display
43
44 def item_link(self, chercheur):
45 return reverse('chercheur', kwargs=dict(id=chercheur.id))
46
47 def item_pubdate(self, chercheur):
48 d = chercheur.date_modification
49 return datetime(d.year, d.month, d.day, tzinfo=tzlocal())
50
51
52 class FilRessources(Feed):
53 title = "Savoirs en partage - ressources"
54 link = "/ressources/"
55 description = "Ressources nouvellement disponibles sur Savoirs en partage"
56
57 def get_object(self, request):
58 search_form = RessourceSearchForm(request.GET)
59 if search_form.is_valid():
60 return search_form.save(commit=False)
61 else:
62 raise Http404
63 return search_form.save(commit=False)
64
65 def items(self, search):
66 min_date = date.today() - timedelta(days=30)
67 return search.run(min_date=min_date).order_by('-modified')
68
69 def item_title(self, ressource):
70 return ressource.title
71
72 def item_description(self, ressource):
73 return ressource.description
74
75 def item_author_name(self, ressource):
76 return ressource.creator
77
78 def item_pubdate(self, ressource):
79 try:
80 modified = parse_date(ressource.modified)
81 except ValueError:
82 modified = datetime.now()
83 if modified.tzinfo is None:
84 modified.tzinfo = modified.replace(tzinfo=pytz.UTC)
85 return modified
86
87
88 class FilActualitesBase(Feed):
89
90 def get_object(self, request):
91 search_form = ActualiteSearchForm(request.GET)
92 if search_form.is_valid():
93 return search_form.save(commit=False)
94 else:
95 raise Http404
96
97 def items(self, search):
98 min_date = date.today() - timedelta(days=30)
99 return search.run(min_date=min_date).order_by('-date')
100
101 def item_title(self, actualite):
102 return actualite.titre
103
104 def item_description(self, actualite):
105 return actualite.texte
106
107 def item_author_name(self, actualite):
108 return actualite.source.nom
109
110 def item_pubdate(self, actualite):
111 d = actualite.date
112 return datetime(d.year, d.month, d.day, tzinfo=tzutc())
113
114
115 class FilActualites(FilActualitesBase):
116 title = "Savoirs en partage - actualités"
117 link = "/actualites/"
118 description = "Actualités récentes sur Savoirs en partage"
119
120 def items(self, search):
121 return FilActualitesBase.items(self, search).filter_type('actu')
122
123
124 class FilAppels(FilActualitesBase):
125 title = "Savoirs en partage - appels d'offres"
126 link = "/appels/"
127 description = "Appels d'offres récents sur Savoirs en partage"
128
129 def items(self, search):
130 return FilActualitesBase.items(self, search).filter_type('appels')
131
132
133 class FilEvenements(Feed):
134 title = "Savoirs en partage - agenda"
135 link = "/agenda/"
136 description = "Agenda Savoirs en partage"
137 description_template = 'savoirs/rss_evenement_description.html'
138
139 def get_object(self, request):
140 search_form = EvenementSearchForm(request.GET)
141 if search_form.is_valid():
142 return search_form.save(commit=False)
143 else:
144 raise Http404
145
146 def items(self, search):
147 min_date = date.today() - timedelta(days=30)
148 return search.run(min_date=min_date).order_by('-debut')
149
150 def item_title(self, evenement):
151 return evenement.titre
152
153 def item_author_name(self, evenement):
154 return ' '.join([evenement.prenom, evenement.nom])
155
156 def item_author_email(self, evenement):
157 return evenement.courriel
158
159
160 class FilSites(Feed):
161 title = "Savoirs en partage - sites"
162 link = "/sites/"
163 description = "Sites récemment ajoutés à Savoirs en partage"
164
165 def get_object(self, request):
166 search_form = SiteSearchForm(request.GET)
167 if search_form.is_valid():
168 return search_form.save(commit=False)
169 else:
170 raise Http404
171
172 def items(self, search):
173 min_date = date.today() - timedelta(days=30)
174 return search.run(min_date=min_date)
175
176 def item_title(self, site):
177 return site.titre
178
179 def item_description(self, site):
180 return site.description
181
182 def item_author_name(self, site):
183 return site.auteur
184
185
186 class FilMessages(Feed):
187
188 def get_object(self, request, groupe_id):
189 return get_object_or_404(Groupe, pk=groupe_id)
190
191 def title(self, obj):
192 return "Savoirs en partage - Messages pour le groupe: %s" % (obj.nom,)
193
194 def link(self, obj):
195 return obj.get_absolute_url()
196
197 def description(self, obj):
198 return "Derniers messages du groupe %s" % (obj.nom,)
199
200 def items(self, obj):
201 return Message.objects.filter(groupe=obj)[:30]
202
203 def item_title(self, message):
204 return message.titre
205
206 def item_description(self, message):
207 return message.contenu
208
209 def item_author_name(self, message):
210 return message.chercheur