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
1 # -*- encoding: utf-8 -*-
2 import datetime, simplejson, copy, vobject
3
4 from django.shortcuts import render_to_response, get_object_or_404, redirect
5 from django.template import Context, RequestContext
6 from django.http import HttpResponse, HttpResponseRedirect
7 from django.contrib.auth.decorators import login_required
8 from django.core.urlresolvers import reverse
9 from django import forms
10 from django.conf import settings
11 from lib.recherche import google_search, build_search_regexp
12 from lib import sep
13 from lib.calendrier import evenements, evenement_info, combine
14 from savoirs.globals import configuration
15 import backend_config
16 from forms import *
17 from models import *
18 from chercheurs.models import Chercheur
19 from sitotheque.models import Site
20
21 # sous-menu gauche
22 def index (request):
23 """Page d'accueil"""
24 delta = datetime.timedelta (days = 90)
25 oldest = datetime.date.today () - delta
26 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
27 actualites = actualites[0:configuration['accueil_actualite']]
28 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
29 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
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))
40
41 # sous-menu droite
42 def a_propos (request):
43 return render_to_response ("savoirs/a-propos.html", \
44 Context (), \
45 context_instance = RequestContext(request))
46
47 def nous_contacter (request):
48 return render_to_response ("savoirs/contact.html", \
49 Context ({'courriel':settings.CONTACT_EMAIL}), \
50 context_instance = RequestContext(request))
51
52 def legal(request):
53 return render_to_response ("savoirs/legal.html", \
54 Context (), \
55 context_instance = RequestContext(request))
56
57 # recherche
58 def recherche(request):
59 query = request.GET.get("q", "")
60 if not query.strip():
61 return redirect('/')
62 ressources = Record.objects.validated().search(query)
63 actualites = Actualite.objects.filter(visible=1).search(query)
64 evenements = Evenement.objects.filter(approuve=1).search(query)
65 chercheurs = Chercheur.objects.search(query)
66 sites = Site.objects.search(query)
67 search_regexp = build_search_regexp(query)
68 return render_to_response(
69 "savoirs/recherche.html",
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()),
76 context_instance = RequestContext(request)
77 )
78
79 def 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))
89
90 # ressources
91 def ressource_index(request):
92 search_form = RecordSearchForm(request.GET)
93 ressources = search_form.get_query_set()
94 nb_resultats = ressources.count()
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,
99 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
100 context_instance = RequestContext(request)
101 )
102
103 def 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))
111
112 def informations (request):
113 s = sep.SEP()
114 resources = copy.deepcopy (backend_config.RESOURCES)
115
116 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
117 for l in logs:
118 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
119
120 return render_to_response ("savoirs/informations.html", \
121 Context ({'r': resources}), \
122 context_instance = RequestContext(request))
123
124 # actualités
125 def actualite_index(request):
126 search_form = ActualiteSearchForm(request.GET)
127 actualites = search_form.get_query_set()
128 search_regexp = search_form.get_search_regexp()
129 return render_to_response("savoirs/actualite_index.html",
130 dict(actualites=actualites,
131 search_form=search_form,
132 search_regexp=search_regexp,
133 nb_resultats=actualites.count()),
134 context_instance = RequestContext(request))
135
136 # agenda
137 def evenement_index(request):
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,
145 nb_resultats=evenements.count()),
146 context_instance=RequestContext(request))
147
148 def evenement_utilisation(request):
149 return render_to_response ("savoirs/evenement_utilisation.html", \
150 Context (), \
151 context_instance = RequestContext(request))
152
153 def evenement(request, id):
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))
158
159 def 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
173 def 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
180 def evenement_accepter(request, pk):
181 e = Evenement.objects.get(pk = pk)
182 e.save()
183 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
184
185 @login_required
186 def 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
193 @login_required
194 def json_get (request):
195 uri = request.GET.get ("uri")
196 if uri:
197 s = sep.SEP ()
198 res = s.search ({'uri': uri.encode("utf-8")})
199 r = s.get (res)
200
201 return HttpResponse(simplejson.dumps(r[0]),
202 mimetype='application/json')
203
204 @login_required
205 def json_set (request):
206 data = request.POST.get("data")
207 if data:
208 r = simplejson.loads(data)
209 s = sep.SEP ()
210 s.add (r)
211 return HttpResponse(simplejson.dumps("OK"),
212 mimetype='application/json')