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