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