Réparé les pièces jointes dans l'agenda.
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
CommitLineData
d15017b2 1# -*- encoding: utf-8 -*-
1719bf4e
EMS
2import copy
3import pytz
4import simplejson
92c7413b 5
3c23982e 6from django.shortcuts import render_to_response, get_object_or_404, redirect
d15017b2 7from django.template import Context, RequestContext
92c7413b 8from django.http import HttpResponse, HttpResponseRedirect
5ecd5424 9from django.contrib.auth.decorators import login_required
92c7413b 10from django.core.urlresolvers import reverse
2127a108 11from django.shortcuts import get_object_or_404
f0692c02 12from django.utils.safestring import mark_safe
e3c3296e 13from django import forms
0e9597af 14from django.conf import settings
2b564f72 15from lib.recherche import google_search, excerpt_function
c08a8cb8 16from lib import sep
b7a741ad 17from lib.calendrier import evenements, evenement_info, combine
ecc62927 18from savoirs.globals import configuration
1719bf4e 19from savoirs.models import build_time_zone_choices
91ff43e9 20import backend_config
92c7413b
CR
21from forms import *
22from models import *
588d6b93 23from chercheurs.models import Chercheur
bab65667 24from sitotheque.models import Site
92c7413b 25
bae03b7b
EMS
26# Accueil
27
f0692c02 28def index(request, discipline=None, region=None):
f8c16b3d 29 """Page d'accueil"""
5212238e
EMS
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)
a611bc4a
EMS
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]
bae03b7b
EMS
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))
d15017b2 60
f8c16b3d 61# sous-menu droite
f0692c02 62def a_propos(request):
f8c16b3d 63 return render_to_response ("savoirs/a-propos.html", \
e37563d1 64 Context (), \
f8c16b3d 65 context_instance = RequestContext(request))
66
f0692c02 67def nous_contacter(request):
f8c16b3d 68 return render_to_response ("savoirs/contact.html", \
0e9597af 69 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 70 context_instance = RequestContext(request))
e37563d1 71
f0692c02 72def legal(request):
e37563d1 73 return render_to_response ("savoirs/legal.html", \
74 Context (), \
75 context_instance = RequestContext(request))
f8c16b3d 76
77# recherche
c1b134f8 78def recherche(request, discipline=None, region=None):
0b72ebef 79 query = request.GET.get("q", "")
3c23982e 80 if not query.strip():
37cce5f0
EMS
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
2e37be2b
EMS
91 actualites = Actualite.objects.search(query).order_by('-date')
92 evenements = Evenement.objects.search(query).order_by('-debut')
5212238e 93 ressources = Record.objects.search(query)
2e37be2b 94 chercheurs = Chercheur.objects.search(query).order_by('-date_modification')
5212238e
EMS
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)
184f139f
EMS
110 try:
111 sites_auf = google_search(0, query)['results']
112 except:
113 sites_auf = []
f0692c02
EMS
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:
5212238e 120 params['discipline'] = unicode(discipline.id)
f0692c02 121 if region:
5212238e 122 params['region'] = unicode(region.id)
f0692c02
EMS
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
5212238e
EMS
128 excerpt = excerpt_function(Record.objects, query)
129
0b72ebef
EMS
130 return render_to_response(
131 "savoirs/recherche.html",
5212238e
EMS
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),
0b72ebef
EMS
139 context_instance = RequestContext(request)
140 )
d15017b2 141
f0692c02 142def sites_auf(request):
0b1ddc11
EMS
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))
d15017b2 152
f8c16b3d 153# ressources
f0692c02 154def ressource_index(request):
da44ce68 155 search_form = RecordSearchForm(request.GET)
f0692c02 156 ressources = search_form.get_query_set()
878de341 157 nb_resultats = ressources.count()
2b564f72 158 excerpt = excerpt_function(Record.objects, search_form.cleaned_data['q'])
f61e4846
EMS
159 return render_to_response(
160 "savoirs/ressource_index.html",
5212238e
EMS
161 dict(search_form=search_form, ressources=ressources,
162 nb_resultats=nb_resultats, excerpt=excerpt),
163 context_instance=RequestContext(request)
f61e4846
EMS
164 )
165
f0692c02 166def ressource_retrieve(request, id):
91e0aefb 167 """Notice OAI de la ressource"""
2127a108 168 ressource = get_object_or_404(Record, id=id)
c1b134f8
EMS
169 return render_to_response(
170 "savoirs/ressource_retrieve.html",
06f7a401 171 dict(ressource=ressource, disciplines=ressource.disciplines.all(),
c1b134f8
EMS
172 regions=ressource.regions.all()),
173 context_instance=RequestContext(request)
174 )
f8c16b3d 175
2d10d74b 176def informations (request):
c08a8cb8 177 s = sep.SEP()
91ff43e9 178 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 179
72b466ce 180 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 181 for l in logs:
182 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
183
2d10d74b 184 return render_to_response ("savoirs/informations.html", \
92c7413b 185 Context ({'r': resources}), \
d15017b2
CR
186 context_instance = RequestContext(request))
187
f8c16b3d 188# actualités
f0692c02 189def actualite_index(request):
c9cbc784 190 search_form = ActualiteSearchForm(request.GET)
f0692c02 191 actualites = search_form.get_query_set()
2b564f72 192 excerpt = excerpt_function(Actualite.objects, search_form.cleaned_data['q'])
c1b134f8
EMS
193 return render_to_response(
194 "savoirs/actualite_index.html",
195 dict(actualites=actualites, search_form=search_form,
5212238e 196 excerpt=excerpt, nb_resultats=actualites.count()),
c1b134f8 197 context_instance = RequestContext(request))
5ecd5424 198
c30f12c5
EMS
199def 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
f8c16b3d 205# agenda
f0692c02 206def evenement_index(request):
4101cfc0 207 search_form = EvenementSearchForm(request.GET)
f0692c02 208 evenements = search_form.get_query_set()
5212238e 209 excerpt = excerpt_function(Evenement.objects, search_form.cleaned_data['q'])
c1b134f8
EMS
210 return render_to_response(
211 "savoirs/evenement_index.html",
212 dict(evenements=evenements, search_form=search_form,
5212238e 213 excerpt=excerpt, nb_resultats=evenements.count()),
c1b134f8
EMS
214 context_instance=RequestContext(request)
215 )
d566e9c1 216
f0692c02 217def evenement_utilisation(request):
c1b134f8
EMS
218 return render_to_response("savoirs/evenement_utilisation.html", {},
219 context_instance = RequestContext(request))
0b5b6fe0 220
f0692c02 221def evenement(request, id):
4101cfc0
EMS
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))
92c7413b 226
f0692c02 227def evenement_ajout(request):
92c7413b
CR
228 template = "savoirs/evenement_ajout.html"
229 if request.method == "POST":
ea3822a7 230 form = EvenementForm(request.POST, request.FILES)
92c7413b
CR
231 if form.is_valid():
232 form.save()
233 template = "savoirs/evenement_confirmation.html"
234 else:
235 form = EvenementForm()
c1b134f8
EMS
236 return render_to_response(template, dict(form=form),
237 context_instance=RequestContext(request))
92c7413b 238
1719bf4e
EMS
239def 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
92c7413b
CR
247@login_required
248def 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
255def evenement_accepter(request, pk):
256 e = Evenement.objects.get(pk = pk)
92c7413b 257 e.save()
92c7413b
CR
258 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
259
260@login_required
261def 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
5ecd5424
CR
267@login_required
268def json_get (request):
269 uri = request.GET.get ("uri")
270 if uri:
c08a8cb8 271 s = sep.SEP ()
5ecd5424 272 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 273 r = s.get (res)
5ecd5424 274
c08a8cb8 275 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
276 mimetype='application/json')
277
278@login_required
279def json_set (request):
280 data = request.POST.get("data")
281 if data:
282 r = simplejson.loads(data)
c08a8cb8 283 s = sep.SEP ()
5ecd5424
CR
284 s.add (r)
285 return HttpResponse(simplejson.dumps("OK"),
286 mimetype='application/json')