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