Recherche transversale: actualités
[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
d15017b2
CR
4from django.shortcuts import render_to_response
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']]
6fed87c2 28 try:
65fae967 29 erreur_caldav = False
6fed87c2 30 events = evenements()[0:configuration['accueil_evenement']]
31 except:
65fae967 32 erreur_caldav = u"Problème de connexion à l'agenda"
6fed87c2 33 events = []
ee46b1a2
AJ
34
35
91e0aefb 36 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
da091176 37 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 38 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
5ecd5424 39 return render_to_response ("savoirs/index.html", \
c4c5de1b 40 Context ({"actualites": actualites,
92c7413b 41 "events": events,
65fae967 42 "erreur_caldav": erreur_caldav,
588d6b93 43 "caldav_url": configuration['calendrier_publique'],
91e0aefb 44 "ressources":ressources,
588d6b93 45 "chercheurs":chercheurs,
6e001443 46 "sites":sites,
588d6b93 47 }), \
d15017b2
CR
48 context_instance = RequestContext(request))
49
f8c16b3d 50# sous-menu droite
51def a_propos (request):
52 return render_to_response ("savoirs/a-propos.html", \
53 Context ({'count': len(backend_config.RESOURCES)}), \
54 context_instance = RequestContext(request))
55
56def nous_contacter (request):
57 return render_to_response ("savoirs/contact.html", \
0e9597af 58 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 59 context_instance = RequestContext(request))
60
61# recherche
d15017b2 62def recherche (request):
0b72ebef
EMS
63 query = request.GET.get("q", "")
64 ressources = Record.objects.search(query)
0cbedca8 65 actualites = Actualite.objects.search(query)
0b72ebef
EMS
66 search_regexp = build_search_regexp(query)
67 return render_to_response(
68 "savoirs/recherche.html",
0cbedca8
EMS
69 dict(q=query, ressources=ressources[:5], actualites=actualites[:5],
70 total_ressources=len(ressources), total_actualites=len(actualites),
71 search_regexp=search_regexp),
0b72ebef
EMS
72 context_instance = RequestContext(request)
73 )
d15017b2 74
a008c7e5
CR
75def avancee (request):
76 type = request.GET.get("type", "")
77 page = int(request.GET.get("page", 0))
78
cebcab65
CR
79 r = {'results': [], 'last_page': 0, 'more_link': ''}
80
a008c7e5 81 q = request.GET.get("google-q", "")
544b4522 82 f = RechercheAvancee ()
a008c7e5
CR
83
84 if type == 'google':
85 r = cherche (page, q, type)
544b4522
CR
86 q = {'q': q}
87 elif type == 'avancee':
88 f = RechercheAvancee (request.GET)
89 if f.is_valid():
90 q = {}
91 for k in ['creator', 'title', 'description', 'subject']:
92 tmp = f.cleaned_data[k].strip()
93 if len (tmp) > 0:
94 q[k] = tmp
95 q['operator'] = '|'
96 if f.cleaned_data['operator'] == 'and':
97 q['operator'] = "&"
98
99 r = cherche (page, q, type)
a008c7e5
CR
100
101 return render_to_response ("savoirs/avancee.html", \
102 Context ({'type': type,
103 'page': page,
104 'data': r,
544b4522 105 'form': f,
a008c7e5
CR
106 'q': q}),
107 context_instance = RequestContext(request))
108
d15017b2 109def conseils (request):
5ecd5424 110 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
111 Context (), \
112 context_instance = RequestContext(request))
113
f8c16b3d 114# ressources
115def ressource_index(request):
da44ce68
EMS
116 search_form = RecordSearchForm(request.GET)
117 ressources = search_form.get_query_set()
f61e4846
EMS
118 nb_resultats = len(ressources)
119 search_regexp = search_form.get_search_regexp()
120 return render_to_response(
121 "savoirs/ressource_index.html",
122 {'search_form': search_form, 'ressources': ressources,
123 'nb_resultats': len(ressources), 'search_regexp': search_regexp},
124 context_instance = RequestContext(request)
125 )
126
91e0aefb 127def ressource_retrieve(request, id):
128 """Notice OAI de la ressource"""
129 ressource = Record.objects.get(id=id)
130 variables = { 'ressource': ressource,
131 }
132 return render_to_response ("savoirs/ressource_retrieve.html", \
133 Context (variables),
134 context_instance = RequestContext(request))
f8c16b3d 135
2d10d74b 136def informations (request):
c08a8cb8 137 s = sep.SEP()
91ff43e9 138 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 139
72b466ce 140 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 141 for l in logs:
142 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
143
2d10d74b 144 return render_to_response ("savoirs/informations.html", \
92c7413b 145 Context ({'r': resources}), \
d15017b2
CR
146 context_instance = RequestContext(request))
147
f8c16b3d 148# actualités
149def actualite_index(request):
2f9c4d6c
EMS
150 delta = datetime.timedelta(days=90)
151 oldest = datetime.date.today() - delta
152 actualites = Actualite.objects.filter(visible=True, date__gt=oldest)
153 query = request.GET.get('q')
154 if query:
155 actualites = actualites.search(query)
156 return render_to_response("savoirs/actualite_index.html",
157 {'actualites': actualites},
158 context_instance = RequestContext(request))
5ecd5424 159
f8c16b3d 160# agenda
161def evenement_index(request):
2e7bc859
AJ
162 try:
163 erreur_caldav = False
164 events = evenements()
165 except:
166 erreur_caldav = u"Problème de connexion à l'agenda"
167 events = []
f8c16b3d 168 return render_to_response ("savoirs/evenement_index.html", \
50a045b7 169 Context ({'evenements':events}), \
f8c16b3d 170 context_instance = RequestContext(request))
92c7413b
CR
171
172def evenement(request, id):
173 event = evenement_info(id)
174 return render_to_response ("savoirs/evenement.html", \
175 Context ({'event': event.instance.vevent}), \
176 context_instance = RequestContext(request))
177
178def evenement_ajout(request):
179 template = "savoirs/evenement_ajout.html"
180 if request.method == "POST":
181 form = EvenementForm(request.POST)
182 if form.is_valid():
183 form.save()
184 template = "savoirs/evenement_confirmation.html"
185 else:
186 form = EvenementForm()
187 return render_to_response (template, \
188 Context ({'form': form}), \
189 context_instance = RequestContext(request))
190
191@login_required
192def evenement_moderation(request):
193 events = Evenement.objects.filter(approuve = False)
194 return render_to_response ("savoirs/evenement_moderation.html", \
195 Context ({'events': events}), \
196 context_instance = RequestContext(request))
197
198@login_required
199def evenement_accepter(request, pk):
200 e = Evenement.objects.get(pk = pk)
92c7413b 201 e.save()
92c7413b
CR
202 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
203
204@login_required
205def evenement_refuser(request, pk):
206 evenement = Evenement.objects.get(pk = pk)
207 evenement.actif = False
208 evenement.save()
209 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
210
211
5ecd5424
CR
212@login_required
213def json_get (request):
214 uri = request.GET.get ("uri")
215 if uri:
c08a8cb8 216 s = sep.SEP ()
5ecd5424 217 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 218 r = s.get (res)
5ecd5424 219
c08a8cb8 220 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
221 mimetype='application/json')
222
223@login_required
224def json_set (request):
225 data = request.POST.get("data")
226 if data:
227 r = simplejson.loads(data)
c08a8cb8 228 s = sep.SEP ()
5ecd5424
CR
229 s.add (r)
230 return HttpResponse(simplejson.dumps("OK"),
231 mimetype='application/json')