Réparé le tri des chercheurs par nom, établissement ou pays en conjugaison avec
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
1 # -*- encoding: utf-8 -*-
2 import copy
3 import pytz
4 import simplejson
5
6 from django.shortcuts import render_to_response, get_object_or_404, redirect
7 from django.template import Context, RequestContext
8 from django.http import HttpResponse, HttpResponseRedirect
9 from django.contrib.auth.decorators import login_required
10 from django.core.urlresolvers import reverse
11 from django.shortcuts import get_object_or_404
12 from django.utils.safestring import mark_safe
13 from django import forms
14 from django.conf import settings
15 from lib.recherche import google_search, excerpt_function
16 from lib import sep
17 from lib.calendrier import evenements, evenement_info, combine
18 from savoirs.globals import configuration
19 from savoirs.models import build_time_zone_choices
20 import backend_config
21 from forms import *
22 from models import *
23 from chercheurs.models import Chercheur
24 from sitotheque.models import Site
25
26 # Accueil
27
28 def index(request, discipline=None, region=None):
29 """Page d'accueil"""
30 actualites = Actualite.objects
31 evenements = Evenement.objects
32 ressources = Record.objects
33 chercheurs = Chercheur.objects
34 sites = Site.objects
35 if discipline:
36 discipline = Discipline.objects.get(pk=discipline)
37 actualites = actualites.filter_discipline(discipline)
38 evenements = evenements.filter_discipline(discipline)
39 ressources = ressources.filter_discipline(discipline)
40 chercheurs = chercheurs.filter_discipline(discipline)
41 sites = sites.filter_discipline(discipline)
42 if region:
43 region = Region.objects.get(pk=region)
44 actualites = actualites.filter_region(region)
45 evenements = evenements.filter_region(region)
46 ressources = ressources.filter_region(region)
47 chercheurs = chercheurs.filter_region(region)
48 sites = sites.filter_region(region)
49 actualites = actualites.order_by('-date')[0:4]
50 evenements = evenements.order_by('-debut')[0:4]
51 ressources = ressources.order_by('-id')[0:4]
52 chercheurs = chercheurs.order_by('-date_modification')[0:10]
53 sites = sites.order_by('-date_maj')[0:4]
54 return render_to_response(
55 "savoirs/index.html",
56 dict(actualites=actualites, evenements=evenements,
57 caldav_url=configuration['calendrier_publique'],
58 ressources=ressources, chercheurs=chercheurs, sites=sites),
59 context_instance = RequestContext(request))
60
61 # sous-menu droite
62 def a_propos(request):
63 return render_to_response ("savoirs/a-propos.html", \
64 Context (), \
65 context_instance = RequestContext(request))
66
67 def nous_contacter(request):
68 return render_to_response ("savoirs/contact.html", \
69 Context ({'courriel':settings.CONTACT_EMAIL}), \
70 context_instance = RequestContext(request))
71
72 def legal(request):
73 return render_to_response ("savoirs/legal.html", \
74 Context (), \
75 context_instance = RequestContext(request))
76
77 # recherche
78 def recherche(request, discipline=None, region=None):
79 query = request.GET.get("q", "")
80 if not query.strip():
81
82 # Si on n'a pas de recherche par mots-clés, on redirige vers
83 # l'accueil.
84 kwargs = {}
85 if discipline:
86 kwargs['discipline'] = discipline
87 if region:
88 kwargs['region'] = region
89 return HttpResponseRedirect(reverse('savoirs.views.index', kwargs=kwargs))
90
91 actualites = Actualite.objects.search(query).order_by('-date')
92 evenements = Evenement.objects.search(query).order_by('-debut')
93 ressources = Record.objects.search(query)
94 chercheurs = Chercheur.objects.search(query).order_by('-date_modification')
95 sites = Site.objects.search(query)
96 if discipline:
97 discipline = Discipline.objects.get(pk=discipline)
98 actualites = actualites.filter_discipline(discipline)
99 evenements = evenements.filter_discipline(discipline)
100 ressources = ressources.filter_discipline(discipline)
101 chercheurs = chercheurs.filter_discipline(discipline)
102 sites = sites.filter_discipline(discipline)
103 if region:
104 region = Region.objects.get(pk=region)
105 actualites = actualites.filter_region(region)
106 evenements = evenements.filter_region(region)
107 ressources = ressources.filter_region(region)
108 chercheurs = chercheurs.filter_region(region)
109 sites = sites.filter_region(region)
110 try:
111 sites_auf = google_search(0, query)['results']
112 except:
113 sites_auf = []
114
115 # Bâtissons une query string pour les liens vers les briques
116 params = {}
117 if query:
118 params['q'] = query
119 if discipline:
120 params['discipline'] = unicode(discipline.id)
121 if region:
122 params['region'] = unicode(region.id)
123 if params:
124 briques_query_string = mark_safe('?' + '&'.join(k + '=' + v.replace('"', '"') for (k, v) in params.iteritems()))
125 else:
126 briques_query_string = None
127
128 excerpt = excerpt_function(Record.objects, query)
129
130 return render_to_response(
131 "savoirs/recherche.html",
132 dict(q=query, excerpt=excerpt,
133 ressources=ressources[0:5], total_ressources=ressources.count(),
134 evenements=evenements[0:5], total_evenements=evenements.count(),
135 chercheurs=chercheurs[0:10], total_chercheurs=chercheurs.count(),
136 actualites=actualites[0:5], total_actualites=actualites.count(),
137 sites=sites[0:5], total_sites=sites.count(),
138 sites_auf=sites_auf[0:5], briques_query_string=briques_query_string),
139 context_instance = RequestContext(request)
140 )
141
142 def sites_auf(request):
143 q = request.GET.get('q')
144 page = int(request.GET.get('page', 0))
145 try:
146 data = google_search(page, q) if q else None
147 except:
148 data = None
149 return render_to_response('savoirs/sites_auf.html',
150 dict(google_q=q, data=data, page=page),
151 context_instance=RequestContext(request))
152
153 # ressources
154 def ressource_index(request):
155 search_form = RecordSearchForm(request.GET)
156 ressources = search_form.get_query_set()
157 nb_resultats = ressources.count()
158 excerpt = excerpt_function(Record.objects, search_form.cleaned_data['q'])
159 return render_to_response(
160 "savoirs/ressource_index.html",
161 dict(search_form=search_form, ressources=ressources,
162 nb_resultats=nb_resultats, excerpt=excerpt),
163 context_instance=RequestContext(request)
164 )
165
166 def ressource_retrieve(request, id):
167 """Notice OAI de la ressource"""
168 ressource = get_object_or_404(Record, id=id)
169 return render_to_response(
170 "savoirs/ressource_retrieve.html",
171 dict(ressource=ressource, disciplines=ressource.disciplines.all(),
172 regions=ressource.regions.all()),
173 context_instance=RequestContext(request)
174 )
175
176 def informations (request):
177 s = sep.SEP()
178 resources = copy.deepcopy (backend_config.RESOURCES)
179
180 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
181 for l in logs:
182 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
183
184 return render_to_response ("savoirs/informations.html", \
185 Context ({'r': resources}), \
186 context_instance = RequestContext(request))
187
188 # actualités
189 def actualite_index(request):
190 search_form = ActualiteSearchForm(request.GET)
191 actualites = search_form.get_query_set()
192 excerpt = excerpt_function(Actualite.objects, search_form.cleaned_data['q'])
193 return render_to_response(
194 "savoirs/actualite_index.html",
195 dict(actualites=actualites, search_form=search_form,
196 excerpt=excerpt, nb_resultats=actualites.count()),
197 context_instance = RequestContext(request))
198
199 def actualite(request, id):
200 actualite = get_object_or_404(Actualite, pk=id)
201 return render_to_response("savoirs/actualite.html",
202 dict(actualite=actualite),
203 context_instance=RequestContext(request))
204
205 # agenda
206 def evenement_index(request):
207 search_form = EvenementSearchForm(request.GET)
208 evenements = search_form.get_query_set()
209 excerpt = excerpt_function(Evenement.objects, search_form.cleaned_data['q'])
210 return render_to_response(
211 "savoirs/evenement_index.html",
212 dict(evenements=evenements, search_form=search_form,
213 excerpt=excerpt, nb_resultats=evenements.count()),
214 context_instance=RequestContext(request)
215 )
216
217 def evenement_utilisation(request):
218 return render_to_response("savoirs/evenement_utilisation.html", {},
219 context_instance = RequestContext(request))
220
221 def evenement(request, id):
222 evenement = get_object_or_404(Evenement, pk=id)
223 return render_to_response("savoirs/evenement.html",
224 dict(evenement=evenement),
225 context_instance=RequestContext(request))
226
227 def evenement_ajout(request):
228 template = "savoirs/evenement_ajout.html"
229 if request.method == "POST":
230 form = EvenementForm(request.POST)
231 if form.is_valid():
232 form.save()
233 template = "savoirs/evenement_confirmation.html"
234 else:
235 form = EvenementForm()
236 return render_to_response(template, dict(form=form),
237 context_instance=RequestContext(request))
238
239 def options_fuseau_horaire(request):
240 pays = request.GET.get('pays')
241 choices = build_time_zone_choices(request.GET.get('pays'))
242 if len(choices) > 1:
243 choices = [('', '---------')] + choices
244 return render_to_response('savoirs/options_fuseau_horaire.html', dict(choices=choices),
245 context_instance=RequestContext(request))
246
247 @login_required
248 def evenement_moderation(request):
249 events = Evenement.objects.filter(approuve = False)
250 return render_to_response ("savoirs/evenement_moderation.html", \
251 Context ({'events': events}), \
252 context_instance = RequestContext(request))
253
254 @login_required
255 def evenement_accepter(request, pk):
256 e = Evenement.objects.get(pk = pk)
257 e.save()
258 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
259
260 @login_required
261 def evenement_refuser(request, pk):
262 evenement = Evenement.objects.get(pk = pk)
263 evenement.actif = False
264 evenement.save()
265 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
266
267 @login_required
268 def json_get (request):
269 uri = request.GET.get ("uri")
270 if uri:
271 s = sep.SEP ()
272 res = s.search ({'uri': uri.encode("utf-8")})
273 r = s.get (res)
274
275 return HttpResponse(simplejson.dumps(r[0]),
276 mimetype='application/json')
277
278 @login_required
279 def json_set (request):
280 data = request.POST.get("data")
281 if data:
282 r = simplejson.loads(data)
283 s = sep.SEP ()
284 s.add (r)
285 return HttpResponse(simplejson.dumps("OK"),
286 mimetype='application/json')