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