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