7116607fbdf996cbbab70d3bff23f652a6a07b55
[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, get_object_or_404
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 django.conf import settings
11 from lib.recherche import cherche, google_search, build_search_regexp
12 from lib import sep
13 from lib.calendrier import evenements, evenement_info, combine
14 from savoirs.globals import configuration
15 import backend_config
16 from forms import *
17 from models import *
18 from chercheurs.models import Chercheur
19 from sitotheque.models import Site
20
21 # sous-menu gauche
22 def index (request):
23 """Page d'accueil"""
24 delta = datetime.timedelta (days = 90)
25 oldest = datetime.date.today () - delta
26 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
27 actualites = actualites[0:configuration['accueil_actualite']]
28 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
29 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
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))
40
41 # sous-menu droite
42 def a_propos (request):
43 return render_to_response ("savoirs/a-propos.html", \
44 Context (), \
45 context_instance = RequestContext(request))
46
47 def nous_contacter (request):
48 return render_to_response ("savoirs/contact.html", \
49 Context ({'courriel':settings.CONTACT_EMAIL}), \
50 context_instance = RequestContext(request))
51
52 def legal(request):
53 return render_to_response ("savoirs/legal.html", \
54 Context (), \
55 context_instance = RequestContext(request))
56
57 # recherche
58 def recherche(request):
59 query = request.GET.get("q", "")
60 ressources = Record.objects.validated().search(query)
61 actualites = Actualite.objects.filter(visible=1).search(query)
62 evenements = Evenement.objects.filter(approuve=1).search(query)
63 chercheurs = Chercheur.objects.search(query)
64 sites = Site.objects.search(query)
65 search_regexp = build_search_regexp(query)
66 return render_to_response(
67 "savoirs/recherche.html",
68 dict(q=query, search_regexp=search_regexp,
69 ressources=ressources[:5], total_ressources=ressources.count(),
70 evenements=evenements[:5], total_evenements=evenements.count(),
71 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
72 actualites=actualites[:5], total_actualites=actualites.count(),
73 sites=sites[:5], total_sites=sites.count()),
74 context_instance = RequestContext(request)
75 )
76
77 def avancee (request):
78 type = request.GET.get("type", "")
79 page = int(request.GET.get("page", 0))
80
81 r = {'results': [], 'last_page': 0, 'more_link': ''}
82
83 q = request.GET.get("google-q", "")
84 f = RechercheAvancee ()
85
86 if type == 'google':
87 r = cherche (page, q, type)
88 q = {'q': q}
89 elif type == 'avancee':
90 f = RechercheAvancee (request.GET)
91 if f.is_valid():
92 q = {}
93 for k in ['creator', 'title', 'description', 'subject']:
94 tmp = f.cleaned_data[k].strip()
95 if len (tmp) > 0:
96 q[k] = tmp
97 q['operator'] = '|'
98 if f.cleaned_data['operator'] == 'and':
99 q['operator'] = "&"
100
101 r = cherche (page, q, type)
102
103 return render_to_response ("savoirs/avancee.html", \
104 Context ({'type': type,
105 'page': page,
106 'data': r,
107 'search_regexp': None, # pour pouvoir utiliser la même template de résultat
108 'form': f,
109 'q': q}),
110 context_instance = RequestContext(request))
111
112 def conseils (request):
113 return render_to_response ("savoirs/conseils.html", \
114 Context (), \
115 context_instance = RequestContext(request))
116
117 # ressources
118 def ressource_index(request):
119 search_form = RecordSearchForm(request.GET)
120 ressources = search_form.get_query_set()
121 nb_resultats = ressources.count()
122 search_regexp = search_form.get_search_regexp()
123 return render_to_response(
124 "savoirs/ressource_index.html",
125 {'search_form': search_form, 'ressources': ressources,
126 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
127 context_instance = RequestContext(request)
128 )
129
130 def ressource_retrieve(request, id):
131 """Notice OAI de la ressource"""
132 ressource = Record.objects.get(id=id)
133 variables = { 'ressource': ressource,
134 }
135 return render_to_response ("savoirs/ressource_retrieve.html", \
136 Context (variables),
137 context_instance = RequestContext(request))
138
139 def informations (request):
140 s = sep.SEP()
141 resources = copy.deepcopy (backend_config.RESOURCES)
142
143 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
144 for l in logs:
145 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
146
147 return render_to_response ("savoirs/informations.html", \
148 Context ({'r': resources}), \
149 context_instance = RequestContext(request))
150
151 # actualités
152 def actualite_index(request):
153 search_form = ActualiteSearchForm(request.GET)
154 actualites = search_form.get_query_set()
155 search_regexp = search_form.get_search_regexp()
156 return render_to_response("savoirs/actualite_index.html",
157 dict(actualites=actualites,
158 search_form=search_form,
159 search_regexp=search_regexp,
160 nb_resultats=actualites.count()),
161 context_instance = RequestContext(request))
162
163 # agenda
164 def evenement_index(request):
165 search_form = EvenementSearchForm(request.GET)
166 evenements = search_form.get_query_set()
167 search_regexp = search_form.get_search_regexp()
168 return render_to_response("savoirs/evenement_index.html",
169 dict(evenements=evenements,
170 search_form=search_form,
171 search_regexp=search_regexp,
172 nb_resultats=evenements.count()),
173 context_instance=RequestContext(request))
174
175 def evenement_utilisation(request):
176 return render_to_response ("savoirs/evenement_utilisation.html", \
177 Context (), \
178 context_instance = RequestContext(request))
179
180 def evenement(request, id):
181 evenement = get_object_or_404(Evenement, pk=id)
182 return render_to_response("savoirs/evenement.html",
183 dict(evenement=evenement),
184 context_instance=RequestContext(request))
185
186 def evenement_ajout(request):
187 template = "savoirs/evenement_ajout.html"
188 if request.method == "POST":
189 form = EvenementForm(request.POST)
190 if form.is_valid():
191 form.save()
192 template = "savoirs/evenement_confirmation.html"
193 else:
194 form = EvenementForm()
195 return render_to_response (template, \
196 Context ({'form': form}), \
197 context_instance = RequestContext(request))
198
199 @login_required
200 def evenement_moderation(request):
201 events = Evenement.objects.filter(approuve = False)
202 return render_to_response ("savoirs/evenement_moderation.html", \
203 Context ({'events': events}), \
204 context_instance = RequestContext(request))
205
206 @login_required
207 def evenement_accepter(request, pk):
208 e = Evenement.objects.get(pk = pk)
209 e.save()
210 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
211
212 @login_required
213 def evenement_refuser(request, pk):
214 evenement = Evenement.objects.get(pk = pk)
215 evenement.actif = False
216 evenement.save()
217 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
218
219
220 @login_required
221 def json_get (request):
222 uri = request.GET.get ("uri")
223 if uri:
224 s = sep.SEP ()
225 res = s.search ({'uri': uri.encode("utf-8")})
226 r = s.get (res)
227
228 return HttpResponse(simplejson.dumps(r[0]),
229 mimetype='application/json')
230
231 @login_required
232 def json_set (request):
233 data = request.POST.get("data")
234 if data:
235 r = simplejson.loads(data)
236 s = sep.SEP ()
237 s.add (r)
238 return HttpResponse(simplejson.dumps("OK"),
239 mimetype='application/json')