Chercheur edit
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
1 # -*- encoding: utf-8 -*-
2 import datetime, simplejson, copy, vobject
3
4 from django.shortcuts import render_to_response
5 from django.template import Context, RequestContext
6 from django.http import HttpResponse, HttpResponseRedirect
7 from django.contrib.auth.decorators import login_required
8 from django.core.urlresolvers import reverse
9 from django import forms
10 from lib.recherche import cherche, google_search
11 from lib import sep
12 from lib.calendrier import evenements, evenement_info, combine
13 from savoirs.globals import configuration
14 import backend_config
15 from forms import *
16 from models import *
17 from chercheurs.models import Chercheur
18 from sitotheque.models import Site
19
20 # sous-menu gauche
21 def index (request):
22 """Page d'accueil"""
23 delta = datetime.timedelta (days = 90)
24 oldest = datetime.date.today () - delta
25 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
26 actualites = actualites[0:configuration['accueil_actualite']]
27 try:
28 erreur_caldav = False
29 events = evenements()[0:configuration['accueil_evenement']]
30 except:
31 erreur_caldav = u"Problème de connexion à l'agenda"
32 events = []
33 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
34 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
35 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
36 return render_to_response ("savoirs/index.html", \
37 Context ({"actualites": actualites,
38 "events": events,
39 "erreur_caldav": erreur_caldav,
40 "caldav_url": configuration['calendrier_publique'],
41 "ressources":ressources,
42 "chercheurs":chercheurs,
43 "sites":sites,
44 }), \
45 context_instance = RequestContext(request))
46
47 # sous-menu droite
48 def 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
53 def nous_contacter (request):
54 return render_to_response ("savoirs/contact.html", \
55 Context (), \
56 context_instance = RequestContext(request))
57
58 # recherche
59 def recherche (request):
60 q = request.GET.get("q", "")
61 page = int(request.GET.get("page", 0))
62
63 r = cherche (page, q)
64
65 return render_to_response ("savoirs/recherche.html", \
66 Context ({'q': q,
67 'page': page,
68 'data': r}), \
69 context_instance = RequestContext(request))
70
71 def avancee (request):
72 type = request.GET.get("type", "")
73 page = int(request.GET.get("page", 0))
74
75 r = {'results': [], 'last_page': 0, 'more_link': ''}
76
77 q = request.GET.get("google-q", "")
78 f = RechercheAvancee ()
79
80 if type == 'google':
81 r = cherche (page, q, type)
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)
96
97 return render_to_response ("savoirs/avancee.html", \
98 Context ({'type': type,
99 'page': page,
100 'data': r,
101 'form': f,
102 'q': q}),
103 context_instance = RequestContext(request))
104
105 def conseils (request):
106 return render_to_response ("savoirs/conseils.html", \
107 Context (), \
108 context_instance = RequestContext(request))
109
110 # ressources
111 def ressource_index(request):
112 ressources = Record.objects.all().order_by('?')
113 return render_to_response ("savoirs/ressource_index.html", \
114 Context ({'ressources':ressources}), \
115 context_instance = RequestContext(request))
116
117 def 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))
125
126 def informations (request):
127 s = sep.SEP()
128 resources = copy.deepcopy (backend_config.RESOURCES)
129
130 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
131 for l in logs:
132 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
133
134 return render_to_response ("savoirs/informations.html", \
135 Context ({'r': resources}), \
136 context_instance = RequestContext(request))
137
138 # actualités
139 def actualite_index(request):
140 delta = datetime.timedelta (days = 90)
141 oldest = datetime.date.today () - delta
142 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
143 return render_to_response ("savoirs/actualite_index.html", \
144 Context ({'actualites': actualites}), \
145 context_instance = RequestContext(request))
146
147 # agenda
148 def evenement_index(request):
149 try:
150 erreur_caldav = False
151 events = evenements()
152 except:
153 erreur_caldav = u"Problème de connexion à l'agenda"
154 events = []
155 return render_to_response ("savoirs/evenement_index.html", \
156 Context ({'evenements':events}), \
157 context_instance = RequestContext(request))
158
159 def 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
165 def 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
179 def 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
186 def evenement_accepter(request, pk):
187 e = Evenement.objects.get(pk = pk)
188 e.save()
189 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
190
191 @login_required
192 def 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
199 @login_required
200 def json_get (request):
201 uri = request.GET.get ("uri")
202 if uri:
203 s = sep.SEP ()
204 res = s.search ({'uri': uri.encode("utf-8")})
205 r = s.get (res)
206
207 return HttpResponse(simplejson.dumps(r[0]),
208 mimetype='application/json')
209
210 @login_required
211 def json_set (request):
212 data = request.POST.get("data")
213 if data:
214 r = simplejson.loads(data)
215 s = sep.SEP ()
216 s.add (r)
217 return HttpResponse(simplejson.dumps("OK"),
218 mimetype='application/json')