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