404 si pas did
[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, redirect
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.shortcuts import get_object_or_404
10 from django import forms
11 from django.conf import settings
12 from lib.recherche import google_search, build_search_regexp
13 from lib import sep
14 from lib.calendrier import evenements, evenement_info, combine
15 from savoirs.globals import configuration
16 import backend_config
17 from forms import *
18 from models import *
19 from chercheurs.models import Chercheur
20 from sitotheque.models import Site
21
22 # sous-menu gauche
23 def index (request):
24 """Page d'accueil"""
25 delta = datetime.timedelta (days = 90)
26 oldest = datetime.date.today () - delta
27 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
28 actualites = actualites[0:configuration['accueil_actualite']]
29 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
30 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
31 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
32 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
33 return render_to_response("savoirs/index.html",
34 dict(actualites=actualites,
35 evenements=evenements,
36 caldav_url=configuration['calendrier_publique'],
37 ressources=ressources,
38 chercheurs=chercheurs,
39 sites=sites),
40 context_instance = RequestContext(request))
41
42 # sous-menu droite
43 def a_propos (request):
44 return render_to_response ("savoirs/a-propos.html", \
45 Context (), \
46 context_instance = RequestContext(request))
47
48 def nous_contacter (request):
49 return render_to_response ("savoirs/contact.html", \
50 Context ({'courriel':settings.CONTACT_EMAIL}), \
51 context_instance = RequestContext(request))
52
53 def legal(request):
54 return render_to_response ("savoirs/legal.html", \
55 Context (), \
56 context_instance = RequestContext(request))
57
58 # recherche
59 def recherche(request):
60 query = request.GET.get("q", "")
61 if not query.strip():
62 return redirect('/')
63 ressources = Record.objects.validated().search(query)
64 actualites = Actualite.objects.filter(visible=1).search(query)
65 evenements = Evenement.objects.filter(approuve=1).search(query)
66 chercheurs = Chercheur.objects.search(query)
67 sites = Site.objects.search(query)
68 try:
69 sites_auf = google_search(0, query)['results']
70 except:
71 sites_auf = []
72 search_regexp = build_search_regexp(query)
73 return render_to_response(
74 "savoirs/recherche.html",
75 dict(q=query, search_regexp=search_regexp,
76 ressources=ressources[:5], total_ressources=ressources.count(),
77 evenements=evenements[:5], total_evenements=evenements.count(),
78 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
79 actualites=actualites[:5], total_actualites=actualites.count(),
80 sites=sites[:5], total_sites=sites.count(),
81 sites_auf=sites_auf[:5]),
82 context_instance = RequestContext(request)
83 )
84
85 def sites_auf(request):
86 q = request.GET.get('q')
87 page = int(request.GET.get('page', 0))
88 try:
89 data = google_search(page, q) if q else None
90 except:
91 data = None
92 return render_to_response('savoirs/sites_auf.html',
93 dict(google_q=q, data=data, page=page),
94 context_instance=RequestContext(request))
95
96 # ressources
97 def ressource_index(request):
98 search_form = RecordSearchForm(request.GET)
99 ressources = search_form.get_query_set()
100 nb_resultats = ressources.count()
101 search_regexp = search_form.get_search_regexp()
102 return render_to_response(
103 "savoirs/ressource_index.html",
104 {'search_form': search_form, 'ressources': ressources,
105 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
106 context_instance = RequestContext(request)
107 )
108
109 def ressource_retrieve(request, id):
110 """Notice OAI de la ressource"""
111 ressource = get_object_or_404(Record, id=id)
112 variables = { 'ressource': ressource,
113 }
114 return render_to_response ("savoirs/ressource_retrieve.html", \
115 Context (variables),
116 context_instance = RequestContext(request))
117
118 def informations (request):
119 s = sep.SEP()
120 resources = copy.deepcopy (backend_config.RESOURCES)
121
122 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
123 for l in logs:
124 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
125
126 return render_to_response ("savoirs/informations.html", \
127 Context ({'r': resources}), \
128 context_instance = RequestContext(request))
129
130 # actualités
131 def actualite_index(request):
132 search_form = ActualiteSearchForm(request.GET)
133 actualites = search_form.get_query_set()
134 search_regexp = search_form.get_search_regexp()
135 return render_to_response("savoirs/actualite_index.html",
136 dict(actualites=actualites,
137 search_form=search_form,
138 search_regexp=search_regexp,
139 nb_resultats=actualites.count()),
140 context_instance = RequestContext(request))
141
142 # agenda
143 def evenement_index(request):
144 search_form = EvenementSearchForm(request.GET)
145 evenements = search_form.get_query_set()
146 search_regexp = search_form.get_search_regexp()
147 return render_to_response("savoirs/evenement_index.html",
148 dict(evenements=evenements,
149 search_form=search_form,
150 search_regexp=search_regexp,
151 nb_resultats=evenements.count()),
152 context_instance=RequestContext(request))
153
154 def evenement_utilisation(request):
155 return render_to_response ("savoirs/evenement_utilisation.html", \
156 Context (), \
157 context_instance = RequestContext(request))
158
159 def evenement(request, id):
160 evenement = get_object_or_404(Evenement, pk=id)
161 return render_to_response("savoirs/evenement.html",
162 dict(evenement=evenement),
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')