Sites AUF + Amélioration de la performance de la recherche.
[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
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 ressources = Record.objects.validated().search(query)
61 actualites = Actualite.objects.filter(visible=1).search(query)
62 evenements = Evenement.objects.filter(approuve=1).search(query)
63 chercheurs = Chercheur.objects.search(query)
64 sites = Site.objects.search(query)
65 search_regexp = build_search_regexp(query)
66 return render_to_response(
67 "savoirs/recherche.html",
68 dict(q=query, search_regexp=search_regexp,
69 ressources=ressources[:5], total_ressources=ressources.count(),
70 evenements=evenements[:5], total_evenements=evenements.count(),
71 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
72 actualites=actualites[:5], total_actualites=actualites.count(),
73 sites=sites[:5], total_sites=sites.count()),
74 context_instance = RequestContext(request)
75 )
76
77 def sites_auf(request):
78 q = request.GET.get('q')
79 page = int(request.GET.get('page', 0))
80 try:
81 data = google_search(page, q) if q else None
82 except:
83 data = None
84 return render_to_response('savoirs/sites_auf.html',
85 dict(google_q=q, data=data, page=page),
86 context_instance=RequestContext(request))
87
88 # ressources
89 def ressource_index(request):
90 search_form = RecordSearchForm(request.GET)
91 ressources = search_form.get_query_set()
92 nb_resultats = ressources.count()
93 search_regexp = search_form.get_search_regexp()
94 return render_to_response(
95 "savoirs/ressource_index.html",
96 {'search_form': search_form, 'ressources': ressources,
97 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
98 context_instance = RequestContext(request)
99 )
100
101 def ressource_retrieve(request, id):
102 """Notice OAI de la ressource"""
103 ressource = Record.objects.get(id=id)
104 variables = { 'ressource': ressource,
105 }
106 return render_to_response ("savoirs/ressource_retrieve.html", \
107 Context (variables),
108 context_instance = RequestContext(request))
109
110 def informations (request):
111 s = sep.SEP()
112 resources = copy.deepcopy (backend_config.RESOURCES)
113
114 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
115 for l in logs:
116 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
117
118 return render_to_response ("savoirs/informations.html", \
119 Context ({'r': resources}), \
120 context_instance = RequestContext(request))
121
122 # actualités
123 def actualite_index(request):
124 search_form = ActualiteSearchForm(request.GET)
125 actualites = search_form.get_query_set()
126 search_regexp = search_form.get_search_regexp()
127 return render_to_response("savoirs/actualite_index.html",
128 dict(actualites=actualites,
129 search_form=search_form,
130 search_regexp=search_regexp,
131 nb_resultats=actualites.count()),
132 context_instance = RequestContext(request))
133
134 # agenda
135 def evenement_index(request):
136 search_form = EvenementSearchForm(request.GET)
137 evenements = search_form.get_query_set()
138 search_regexp = search_form.get_search_regexp()
139 return render_to_response("savoirs/evenement_index.html",
140 dict(evenements=evenements,
141 search_form=search_form,
142 search_regexp=search_regexp,
143 nb_resultats=evenements.count()),
144 context_instance=RequestContext(request))
145
146 def evenement_utilisation(request):
147 return render_to_response ("savoirs/evenement_utilisation.html", \
148 Context (), \
149 context_instance = RequestContext(request))
150
151 def evenement(request, id):
152 evenement = get_object_or_404(Evenement, pk=id)
153 return render_to_response("savoirs/evenement.html",
154 dict(evenement=evenement),
155 context_instance=RequestContext(request))
156
157 def evenement_ajout(request):
158 template = "savoirs/evenement_ajout.html"
159 if request.method == "POST":
160 form = EvenementForm(request.POST)
161 if form.is_valid():
162 form.save()
163 template = "savoirs/evenement_confirmation.html"
164 else:
165 form = EvenementForm()
166 return render_to_response (template, \
167 Context ({'form': form}), \
168 context_instance = RequestContext(request))
169
170 @login_required
171 def evenement_moderation(request):
172 events = Evenement.objects.filter(approuve = False)
173 return render_to_response ("savoirs/evenement_moderation.html", \
174 Context ({'events': events}), \
175 context_instance = RequestContext(request))
176
177 @login_required
178 def evenement_accepter(request, pk):
179 e = Evenement.objects.get(pk = pk)
180 e.save()
181 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
182
183 @login_required
184 def evenement_refuser(request, pk):
185 evenement = Evenement.objects.get(pk = pk)
186 evenement.actif = False
187 evenement.save()
188 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
189
190
191 @login_required
192 def json_get (request):
193 uri = request.GET.get ("uri")
194 if uri:
195 s = sep.SEP ()
196 res = s.search ({'uri': uri.encode("utf-8")})
197 r = s.get (res)
198
199 return HttpResponse(simplejson.dumps(r[0]),
200 mimetype='application/json')
201
202 @login_required
203 def json_set (request):
204 data = request.POST.get("data")
205 if data:
206 r = simplejson.loads(data)
207 s = sep.SEP ()
208 s.add (r)
209 return HttpResponse(simplejson.dumps("OK"),
210 mimetype='application/json')