Ajout des chercheurs à la recherche transversale.
[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
4101cfc0 4from django.shortcuts import render_to_response, get_object_or_404
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
0b72ebef 11from lib.recherche import cherche, 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", \
44 Context ({'count': len(backend_config.RESOURCES)}), \
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))
51
52# recherche
a2c6bb72 53def recherche(request):
0b72ebef 54 query = request.GET.get("q", "")
f153be1b
EMS
55 ressources = Record.objects.validated().search(query)
56 actualites = Actualite.objects.filter(visible=1).search(query)
7d07a79f 57 evenements = Evenement.objects.filter(approuve=1).search(query)
a2c6bb72 58 chercheurs = Chercheur.objects.search(query)
0b72ebef
EMS
59 search_regexp = build_search_regexp(query)
60 return render_to_response(
61 "savoirs/recherche.html",
0cbedca8 62 dict(q=query, ressources=ressources[:5], actualites=actualites[:5],
7d07a79f 63 evenements=evenements[:5], total_evenements=len(evenements),
a2c6bb72 64 chercheurs=chercheurs[:10], total_chercheurs=len(chercheurs),
0cbedca8
EMS
65 total_ressources=len(ressources), total_actualites=len(actualites),
66 search_regexp=search_regexp),
0b72ebef
EMS
67 context_instance = RequestContext(request)
68 )
d15017b2 69
a008c7e5
CR
70def avancee (request):
71 type = request.GET.get("type", "")
72 page = int(request.GET.get("page", 0))
73
cebcab65
CR
74 r = {'results': [], 'last_page': 0, 'more_link': ''}
75
a008c7e5 76 q = request.GET.get("google-q", "")
544b4522 77 f = RechercheAvancee ()
a008c7e5
CR
78
79 if type == 'google':
80 r = cherche (page, q, type)
544b4522
CR
81 q = {'q': q}
82 elif type == 'avancee':
83 f = RechercheAvancee (request.GET)
84 if f.is_valid():
85 q = {}
86 for k in ['creator', 'title', 'description', 'subject']:
87 tmp = f.cleaned_data[k].strip()
88 if len (tmp) > 0:
89 q[k] = tmp
90 q['operator'] = '|'
91 if f.cleaned_data['operator'] == 'and':
92 q['operator'] = "&"
93
94 r = cherche (page, q, type)
a008c7e5
CR
95
96 return render_to_response ("savoirs/avancee.html", \
97 Context ({'type': type,
98 'page': page,
99 'data': r,
544b4522 100 'form': f,
a008c7e5
CR
101 'q': q}),
102 context_instance = RequestContext(request))
103
d15017b2 104def conseils (request):
5ecd5424 105 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
106 Context (), \
107 context_instance = RequestContext(request))
108
f8c16b3d 109# ressources
110def ressource_index(request):
da44ce68
EMS
111 search_form = RecordSearchForm(request.GET)
112 ressources = search_form.get_query_set()
f61e4846
EMS
113 nb_resultats = len(ressources)
114 search_regexp = search_form.get_search_regexp()
115 return render_to_response(
116 "savoirs/ressource_index.html",
117 {'search_form': search_form, 'ressources': ressources,
118 'nb_resultats': len(ressources), 'search_regexp': search_regexp},
119 context_instance = RequestContext(request)
120 )
121
91e0aefb 122def ressource_retrieve(request, id):
123 """Notice OAI de la ressource"""
124 ressource = Record.objects.get(id=id)
125 variables = { 'ressource': ressource,
126 }
127 return render_to_response ("savoirs/ressource_retrieve.html", \
128 Context (variables),
129 context_instance = RequestContext(request))
f8c16b3d 130
2d10d74b 131def informations (request):
c08a8cb8 132 s = sep.SEP()
91ff43e9 133 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 134
72b466ce 135 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 136 for l in logs:
137 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
138
2d10d74b 139 return render_to_response ("savoirs/informations.html", \
92c7413b 140 Context ({'r': resources}), \
d15017b2
CR
141 context_instance = RequestContext(request))
142
f8c16b3d 143# actualités
144def actualite_index(request):
c9cbc784
EMS
145 search_form = ActualiteSearchForm(request.GET)
146 actualites = search_form.get_query_set()
147 search_regexp = search_form.get_search_regexp()
2f9c4d6c 148 return render_to_response("savoirs/actualite_index.html",
c9cbc784
EMS
149 dict(actualites=actualites,
150 search_form=search_form,
151 search_regexp=search_regexp,
152 nb_resultats=len(actualites)),
2f9c4d6c 153 context_instance = RequestContext(request))
5ecd5424 154
f8c16b3d 155# agenda
156def evenement_index(request):
4101cfc0
EMS
157 search_form = EvenementSearchForm(request.GET)
158 evenements = search_form.get_query_set()
159 search_regexp = search_form.get_search_regexp()
160 return render_to_response("savoirs/evenement_index.html",
161 dict(evenements=evenements,
162 search_form=search_form,
163 search_regexp=search_regexp,
164 nb_resultats=len(evenements)),
165 context_instance=RequestContext(request))
92c7413b
CR
166
167def evenement(request, id):
4101cfc0
EMS
168 evenement = get_object_or_404(Evenement, pk=id)
169 return render_to_response("savoirs/evenement.html",
170 dict(evenement=evenement),
171 context_instance=RequestContext(request))
92c7413b
CR
172
173def evenement_ajout(request):
174 template = "savoirs/evenement_ajout.html"
175 if request.method == "POST":
176 form = EvenementForm(request.POST)
177 if form.is_valid():
178 form.save()
179 template = "savoirs/evenement_confirmation.html"
180 else:
181 form = EvenementForm()
182 return render_to_response (template, \
183 Context ({'form': form}), \
184 context_instance = RequestContext(request))
185
186@login_required
187def evenement_moderation(request):
188 events = Evenement.objects.filter(approuve = False)
189 return render_to_response ("savoirs/evenement_moderation.html", \
190 Context ({'events': events}), \
191 context_instance = RequestContext(request))
192
193@login_required
194def evenement_accepter(request, pk):
195 e = Evenement.objects.get(pk = pk)
92c7413b 196 e.save()
92c7413b
CR
197 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
198
199@login_required
200def evenement_refuser(request, pk):
201 evenement = Evenement.objects.get(pk = pk)
202 evenement.actif = False
203 evenement.save()
204 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
205
206
5ecd5424
CR
207@login_required
208def json_get (request):
209 uri = request.GET.get ("uri")
210 if uri:
c08a8cb8 211 s = sep.SEP ()
5ecd5424 212 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 213 r = s.get (res)
5ecd5424 214
c08a8cb8 215 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
216 mimetype='application/json')
217
218@login_required
219def json_set (request):
220 data = request.POST.get("data")
221 if data:
222 r = simplejson.loads(data)
c08a8cb8 223 s = sep.SEP ()
5ecd5424
CR
224 s.add (r)
225 return HttpResponse(simplejson.dumps("OK"),
226 mimetype='application/json')