Bugfix: la recherche plantait quand on ne lui donnait pas de texte à chercher.
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
CommitLineData
d15017b2 1# -*- encoding: utf-8 -*-
92c7413b
CR
2import datetime, simplejson, copy, vobject
3
3c23982e 4from django.shortcuts import render_to_response, get_object_or_404, redirect
d15017b2 5from django.template import Context, RequestContext
92c7413b 6from django.http import HttpResponse, HttpResponseRedirect
5ecd5424 7from django.contrib.auth.decorators import login_required
92c7413b 8from django.core.urlresolvers import reverse
e3c3296e 9from django import forms
0e9597af 10from django.conf import settings
0b1ddc11 11from lib.recherche import google_search, build_search_regexp
c08a8cb8 12from lib import sep
b7a741ad 13from lib.calendrier import evenements, evenement_info, combine
ecc62927 14from savoirs.globals import configuration
91ff43e9 15import backend_config
92c7413b
CR
16from forms import *
17from models import *
588d6b93 18from chercheurs.models import Chercheur
bab65667 19from sitotheque.models import Site
92c7413b 20
f8c16b3d 21# sous-menu gauche
d15017b2 22def index (request):
f8c16b3d 23 """Page d'accueil"""
d15017b2
CR
24 delta = datetime.timedelta (days = 90)
25 oldest = datetime.date.today () - delta
c4c5de1b 26 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
27 actualites = actualites[0:configuration['accueil_actualite']]
4101cfc0 28 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
91e0aefb 29 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
da091176 30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
4101cfc0
EMS
32 return render_to_response("savoirs/index.html",
33 dict(actualites=actualites,
34 evenements=evenements,
35 caldav_url=configuration['calendrier_publique'],
36 ressources=ressources,
37 chercheurs=chercheurs,
38 sites=sites),
39 context_instance = RequestContext(request))
d15017b2 40
f8c16b3d 41# sous-menu droite
42def a_propos (request):
43 return render_to_response ("savoirs/a-propos.html", \
e37563d1 44 Context (), \
f8c16b3d 45 context_instance = RequestContext(request))
46
47def nous_contacter (request):
48 return render_to_response ("savoirs/contact.html", \
0e9597af 49 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 50 context_instance = RequestContext(request))
e37563d1 51
52def legal(request):
53 return render_to_response ("savoirs/legal.html", \
54 Context (), \
55 context_instance = RequestContext(request))
f8c16b3d 56
57# recherche
a2c6bb72 58def recherche(request):
0b72ebef 59 query = request.GET.get("q", "")
3c23982e
EMS
60 if not query.strip():
61 return redirect('/')
f153be1b
EMS
62 ressources = Record.objects.validated().search(query)
63 actualites = Actualite.objects.filter(visible=1).search(query)
7d07a79f 64 evenements = Evenement.objects.filter(approuve=1).search(query)
a2c6bb72 65 chercheurs = Chercheur.objects.search(query)
878de341 66 sites = Site.objects.search(query)
0b72ebef
EMS
67 search_regexp = build_search_regexp(query)
68 return render_to_response(
69 "savoirs/recherche.html",
878de341
EMS
70 dict(q=query, search_regexp=search_regexp,
71 ressources=ressources[:5], total_ressources=ressources.count(),
72 evenements=evenements[:5], total_evenements=evenements.count(),
73 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
74 actualites=actualites[:5], total_actualites=actualites.count(),
75 sites=sites[:5], total_sites=sites.count()),
0b72ebef
EMS
76 context_instance = RequestContext(request)
77 )
d15017b2 78
0b1ddc11
EMS
79def sites_auf(request):
80 q = request.GET.get('q')
81 page = int(request.GET.get('page', 0))
82 try:
83 data = google_search(page, q) if q else None
84 except:
85 data = None
86 return render_to_response('savoirs/sites_auf.html',
87 dict(google_q=q, data=data, page=page),
88 context_instance=RequestContext(request))
d15017b2 89
f8c16b3d 90# ressources
91def ressource_index(request):
da44ce68
EMS
92 search_form = RecordSearchForm(request.GET)
93 ressources = search_form.get_query_set()
878de341 94 nb_resultats = ressources.count()
f61e4846
EMS
95 search_regexp = search_form.get_search_regexp()
96 return render_to_response(
97 "savoirs/ressource_index.html",
98 {'search_form': search_form, 'ressources': ressources,
878de341 99 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
f61e4846
EMS
100 context_instance = RequestContext(request)
101 )
102
91e0aefb 103def ressource_retrieve(request, id):
104 """Notice OAI de la ressource"""
105 ressource = Record.objects.get(id=id)
106 variables = { 'ressource': ressource,
107 }
108 return render_to_response ("savoirs/ressource_retrieve.html", \
109 Context (variables),
110 context_instance = RequestContext(request))
f8c16b3d 111
2d10d74b 112def informations (request):
c08a8cb8 113 s = sep.SEP()
91ff43e9 114 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 115
72b466ce 116 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 117 for l in logs:
118 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
119
2d10d74b 120 return render_to_response ("savoirs/informations.html", \
92c7413b 121 Context ({'r': resources}), \
d15017b2
CR
122 context_instance = RequestContext(request))
123
f8c16b3d 124# actualités
125def actualite_index(request):
c9cbc784
EMS
126 search_form = ActualiteSearchForm(request.GET)
127 actualites = search_form.get_query_set()
128 search_regexp = search_form.get_search_regexp()
2f9c4d6c 129 return render_to_response("savoirs/actualite_index.html",
c9cbc784
EMS
130 dict(actualites=actualites,
131 search_form=search_form,
132 search_regexp=search_regexp,
878de341 133 nb_resultats=actualites.count()),
2f9c4d6c 134 context_instance = RequestContext(request))
5ecd5424 135
f8c16b3d 136# agenda
137def evenement_index(request):
4101cfc0
EMS
138 search_form = EvenementSearchForm(request.GET)
139 evenements = search_form.get_query_set()
140 search_regexp = search_form.get_search_regexp()
141 return render_to_response("savoirs/evenement_index.html",
142 dict(evenements=evenements,
143 search_form=search_form,
144 search_regexp=search_regexp,
878de341 145 nb_resultats=evenements.count()),
4101cfc0 146 context_instance=RequestContext(request))
0b5b6fe0 147
148def evenement_utilisation(request):
149 return render_to_response ("savoirs/evenement_utilisation.html", \
150 Context (), \
151 context_instance = RequestContext(request))
152
92c7413b 153def evenement(request, id):
4101cfc0
EMS
154 evenement = get_object_or_404(Evenement, pk=id)
155 return render_to_response("savoirs/evenement.html",
156 dict(evenement=evenement),
157 context_instance=RequestContext(request))
92c7413b
CR
158
159def evenement_ajout(request):
160 template = "savoirs/evenement_ajout.html"
161 if request.method == "POST":
162 form = EvenementForm(request.POST)
163 if form.is_valid():
164 form.save()
165 template = "savoirs/evenement_confirmation.html"
166 else:
167 form = EvenementForm()
168 return render_to_response (template, \
169 Context ({'form': form}), \
170 context_instance = RequestContext(request))
171
172@login_required
173def evenement_moderation(request):
174 events = Evenement.objects.filter(approuve = False)
175 return render_to_response ("savoirs/evenement_moderation.html", \
176 Context ({'events': events}), \
177 context_instance = RequestContext(request))
178
179@login_required
180def evenement_accepter(request, pk):
181 e = Evenement.objects.get(pk = pk)
92c7413b 182 e.save()
92c7413b
CR
183 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
184
185@login_required
186def evenement_refuser(request, pk):
187 evenement = Evenement.objects.get(pk = pk)
188 evenement.actif = False
189 evenement.save()
190 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
191
192
5ecd5424
CR
193@login_required
194def json_get (request):
195 uri = request.GET.get ("uri")
196 if uri:
c08a8cb8 197 s = sep.SEP ()
5ecd5424 198 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 199 r = s.get (res)
5ecd5424 200
c08a8cb8 201 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
202 mimetype='application/json')
203
204@login_required
205def json_set (request):
206 data = request.POST.get("data")
207 if data:
208 r = simplejson.loads(data)
c08a8cb8 209 s = sep.SEP ()
5ecd5424
CR
210 s.add (r)
211 return HttpResponse(simplejson.dumps("OK"),
212 mimetype='application/json')