Recherche de ressources par discipline et région.
[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
2127a108 9from django.shortcuts import get_object_or_404
e3c3296e 10from django import forms
0e9597af 11from django.conf import settings
0b1ddc11 12from lib.recherche import google_search, build_search_regexp
c08a8cb8 13from lib import sep
b7a741ad 14from lib.calendrier import evenements, evenement_info, combine
ecc62927 15from savoirs.globals import configuration
91ff43e9 16import backend_config
92c7413b
CR
17from forms import *
18from models import *
588d6b93 19from chercheurs.models import Chercheur
bab65667 20from sitotheque.models import Site
92c7413b 21
f8c16b3d 22# sous-menu gauche
d15017b2 23def index (request):
f8c16b3d 24 """Page d'accueil"""
d15017b2
CR
25 delta = datetime.timedelta (days = 90)
26 oldest = datetime.date.today () - delta
c4c5de1b 27 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
28 actualites = actualites[0:configuration['accueil_actualite']]
4101cfc0 29 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
91e0aefb 30 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
da091176 31 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 32 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
4101cfc0
EMS
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))
d15017b2 41
f8c16b3d 42# sous-menu droite
43def a_propos (request):
44 return render_to_response ("savoirs/a-propos.html", \
e37563d1 45 Context (), \
f8c16b3d 46 context_instance = RequestContext(request))
47
48def nous_contacter (request):
49 return render_to_response ("savoirs/contact.html", \
0e9597af 50 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 51 context_instance = RequestContext(request))
e37563d1 52
53def legal(request):
54 return render_to_response ("savoirs/legal.html", \
55 Context (), \
56 context_instance = RequestContext(request))
f8c16b3d 57
58# recherche
a2c6bb72 59def recherche(request):
0b72ebef 60 query = request.GET.get("q", "")
3c23982e
EMS
61 if not query.strip():
62 return redirect('/')
f153be1b
EMS
63 ressources = Record.objects.validated().search(query)
64 actualites = Actualite.objects.filter(visible=1).search(query)
7d07a79f 65 evenements = Evenement.objects.filter(approuve=1).search(query)
a2c6bb72 66 chercheurs = Chercheur.objects.search(query)
878de341 67 sites = Site.objects.search(query)
184f139f
EMS
68 try:
69 sites_auf = google_search(0, query)['results']
70 except:
71 sites_auf = []
0b72ebef
EMS
72 search_regexp = build_search_regexp(query)
73 return render_to_response(
74 "savoirs/recherche.html",
878de341
EMS
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(),
184f139f
EMS
80 sites=sites[:5], total_sites=sites.count(),
81 sites_auf=sites_auf[:5]),
0b72ebef
EMS
82 context_instance = RequestContext(request)
83 )
d15017b2 84
0b1ddc11
EMS
85def 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))
d15017b2 95
f8c16b3d 96# ressources
97def ressource_index(request):
da44ce68
EMS
98 search_form = RecordSearchForm(request.GET)
99 ressources = search_form.get_query_set()
878de341 100 nb_resultats = ressources.count()
f61e4846
EMS
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,
878de341 105 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
f61e4846
EMS
106 context_instance = RequestContext(request)
107 )
108
91e0aefb 109def ressource_retrieve(request, id):
110 """Notice OAI de la ressource"""
2127a108 111 ressource = get_object_or_404(Record, id=id)
91e0aefb 112 variables = { 'ressource': ressource,
9113aa36
EMS
113 'disciplines': ressource.disciplines.all(),
114 'regions': ressource.regions.all()
91e0aefb 115 }
116 return render_to_response ("savoirs/ressource_retrieve.html", \
117 Context (variables),
118 context_instance = RequestContext(request))
f8c16b3d 119
2d10d74b 120def informations (request):
c08a8cb8 121 s = sep.SEP()
91ff43e9 122 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 123
72b466ce 124 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 125 for l in logs:
126 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
127
2d10d74b 128 return render_to_response ("savoirs/informations.html", \
92c7413b 129 Context ({'r': resources}), \
d15017b2
CR
130 context_instance = RequestContext(request))
131
f8c16b3d 132# actualités
133def actualite_index(request):
c9cbc784
EMS
134 search_form = ActualiteSearchForm(request.GET)
135 actualites = search_form.get_query_set()
136 search_regexp = search_form.get_search_regexp()
2f9c4d6c 137 return render_to_response("savoirs/actualite_index.html",
c9cbc784
EMS
138 dict(actualites=actualites,
139 search_form=search_form,
140 search_regexp=search_regexp,
878de341 141 nb_resultats=actualites.count()),
2f9c4d6c 142 context_instance = RequestContext(request))
5ecd5424 143
f8c16b3d 144# agenda
145def evenement_index(request):
4101cfc0
EMS
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,
878de341 153 nb_resultats=evenements.count()),
4101cfc0 154 context_instance=RequestContext(request))
0b5b6fe0 155
156def evenement_utilisation(request):
157 return render_to_response ("savoirs/evenement_utilisation.html", \
158 Context (), \
159 context_instance = RequestContext(request))
160
92c7413b 161def evenement(request, id):
4101cfc0
EMS
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))
92c7413b
CR
166
167def 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
181def 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
188def evenement_accepter(request, pk):
189 e = Evenement.objects.get(pk = pk)
92c7413b 190 e.save()
92c7413b
CR
191 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
192
193@login_required
194def 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
5ecd5424
CR
201@login_required
202def json_get (request):
203 uri = request.GET.get ("uri")
204 if uri:
c08a8cb8 205 s = sep.SEP ()
5ecd5424 206 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 207 r = s.get (res)
5ecd5424 208
c08a8cb8 209 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
210 mimetype='application/json')
211
212@login_required
213def json_set (request):
214 data = request.POST.get("data")
215 if data:
216 r = simplejson.loads(data)
c08a8cb8 217 s = sep.SEP ()
5ecd5424
CR
218 s.add (r)
219 return HttpResponse(simplejson.dumps("OK"),
220 mimetype='application/json')