Fils RSS pour toutes les briques
[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
9 from chercheurs.forms import ChercheurSearchForm
10 from savoirs.forms import RessourceSearchForm, ActualiteSearchForm, EvenementSearchForm
11 from sitotheque.forms import SiteSearchForm
12
13 class FilChercheurs(Feed):
14 title = "Savoirs en partage - chercheurs"
15 link = "/chercheurs/"
16 description = "Fiches de chercheurs mises à jour récemment sur Savoirs en partage"
17
18 def get_object(self, request):
19 search_form = ChercheurSearchForm(request.GET)
20 return search_form.save(commit=False)
21
22 def items(self, search):
23 min_date = date.today() - timedelta(days=30)
24 return search.run().order_by('-date_modification').filter_date_modification(min=min_date)
25
26 def item_title(self, chercheur):
27 return unicode(chercheur)
28
29 def item_description(self, chercheur):
30 return chercheur.etablissement_display
31
32 def item_link(self, chercheur):
33 return reverse('chercheur', kwargs=dict(id=chercheur.id))
34
35 def item_pubdate(self, chercheur):
36 d = chercheur.date_modification
37 return datetime(d.year, d.month, d.day, tzinfo=tzlocal())
38
39 class FilRessources(Feed):
40 title = "Savoirs en partage - ressources"
41 link = "/ressources/"
42 description = "Ressources nouvellement disponibles sur Savoirs en partage"
43
44 def get_object(self, request):
45 search_form = RessourceSearchForm(request.GET)
46 return search_form.save(commit=False)
47
48 def items(self, search):
49 min_date = date.today() - timedelta(days=30)
50 return search.run().order_by('-modified').filter_modified(min=min_date)
51
52 def item_title(self, ressource):
53 return ressource.title
54
55 def item_description(self, ressource):
56 return ressource.description
57
58 def item_author_name(self, ressource):
59 return ressource.creator
60
61 def item_pubdate(self, ressource):
62 try:
63 modified = parse_date(ressource.modified)
64 except ValueError:
65 modified = datetime.now()
66 if modified.tzinfo is None:
67 modified.tzinfo = tzutc()
68 return modified
69
70 class FilActualitesBase(Feed):
71
72 def get_object(self, request):
73 search_form = ActualiteSearchForm(request.GET)
74 return search_form.save(commit=False)
75
76 def items(self, search):
77 min_date = date.today() - timedelta(days=30)
78 return search.run().filter_date(min=min_date).order_by('-date')
79
80 def item_title(self, actualite):
81 return actualite.titre
82
83 def item_description(self, actualite):
84 return actualite.texte
85
86 def item_author_name(self, actualite):
87 return actualite.source.nom
88
89 def item_pubdate(self, actualite):
90 d = actualite.date
91 return datetime(d.year, d.month, d.day, tzinfo=tzutc())
92
93 class FilActualites(FilActualitesBase):
94 title = "Savoirs en partage - actualités"
95 link = "/actualites/"
96 description = "Actualités récentes sur Savoirs en partage"
97
98 def items(self, search):
99 return FilActualitesBase.items(self, search).filter_type('actu')
100
101 class FilAppels(FilActualitesBase):
102 title = "Savoirs en partage - appels d'offres"
103 link = "/appels/"
104 description = "Appels d'offres récents sur Savoirs en partage"
105
106 def items(self, search):
107 return FilActualitesBase.items(self, search).filter_type('appels')
108
109 class FilEvenements(Feed):
110 title = "Savoirs en partage - agenda"
111 link = "/agenda/"
112 description = "Agenda Savoirs en partage"
113 description_template = 'savoirs/rss_evenement_description.html'
114
115 def get_object(self, request):
116 search_form = EvenementSearchForm(request.GET)
117 return search_form.save(commit=False)
118
119 def items(self, search):
120 min_date = date.today()
121 max_date = date.today() + timedelta(days=30)
122 return search.run().filter_debut(min=min_date, max=max_date).order_by('-debut')
123
124 def item_title(self, evenement):
125 return evenement.titre
126
127 def item_author_name(self, evenement):
128 return ' '.join([evenement.prenom, evenement.nom])
129
130 def item_author_email(self, evenement):
131 return evenement.courriel
132
133 class FilSites(Feed):
134 title = "Savoirs en partage - sites"
135 link = "/sites/"
136 description = "Sites récemment ajoutés à Savoirs en partage"
137
138 def get_object(self, request):
139 search_form = SiteSearchForm(request.GET)
140 return search_form.save(commit=False)
141
142 def items(self, search):
143 min_date = date.today() - timedelta(days=365)
144 return search.run().filter_date_maj(min=min_date)
145
146 def item_title(self, site):
147 return site.titre
148
149 def item_description(self, site):
150 return site.description
151
152 def item_author_name(self, site):
153 return site.auteur