404 si pas did
[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
3c23982e 4from django.shortcuts import render_to_response, get_object_or_404, redirect
d15017b2 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
2127a108 9from django.shortcuts import get_object_or_404
e3c3296e 10from django import forms
0e9597af 11from django.conf import settings
0b1ddc11 12from lib.recherche import google_search, build_search_regexp
c08a8cb8 13from lib import sep
b7a741ad 14from lib.calendrier import evenements, evenement_info, combine
ecc62927 15from savoirs.globals import configuration
91ff43e9 16import backend_config
92c7413b
CR
17from forms import *
18from models import *
588d6b93 19from chercheurs.models import Chercheur
bab65667 20from sitotheque.models import Site
92c7413b 21
f8c16b3d 22# sous-menu gauche
d15017b2 23def index (request):
f8c16b3d 24 """Page d'accueil"""
d15017b2
CR
25 delta = datetime.timedelta (days = 90)
26 oldest = datetime.date.today () - delta
c4c5de1b 27 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
28 actualites = actualites[0:configuration['accueil_actualite']]
4101cfc0 29 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
91e0aefb 30 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
da091176 31 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 32 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
4101cfc0
EMS
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))
d15017b2 41
f8c16b3d 42# sous-menu droite
43def a_propos (request):
44 return render_to_response ("savoirs/a-propos.html", \
e37563d1 45 Context (), \
f8c16b3d 46 context_instance = RequestContext(request))
47
48def nous_contacter (request):
49 return render_to_response ("savoirs/contact.html", \
0e9597af 50 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 51 context_instance = RequestContext(request))
e37563d1 52
53def legal(request):
54 return render_to_response ("savoirs/legal.html", \
55 Context (), \
56 context_instance = RequestContext(request))
f8c16b3d 57
58# recherche
a2c6bb72 59def recherche(request):
0b72ebef 60 query = request.GET.get("q", "")
3c23982e
EMS
61 if not query.strip():
62 return redirect('/')
f153be1b
EMS
63 ressources = Record.objects.validated().search(query)
64 actualites = Actualite.objects.filter(visible=1).search(query)
7d07a79f 65 evenements = Evenement.objects.filter(approuve=1).search(query)
a2c6bb72 66 chercheurs = Chercheur.objects.search(query)
878de341 67 sites = Site.objects.search(query)
184f139f
EMS
68 try:
69 sites_auf = google_search(0, query)['results']
70 except:
71 sites_auf = []
0b72ebef
EMS
72 search_regexp = build_search_regexp(query)
73 return render_to_response(
74 "savoirs/recherche.html",
878de341
EMS
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(),
184f139f
EMS
80 sites=sites[:5], total_sites=sites.count(),
81 sites_auf=sites_auf[:5]),
0b72ebef
EMS
82 context_instance = RequestContext(request)
83 )
d15017b2 84
0b1ddc11
EMS
85def 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))
d15017b2 95
f8c16b3d 96# ressources
97def ressource_index(request):
da44ce68
EMS
98 search_form = RecordSearchForm(request.GET)
99 ressources = search_form.get_query_set()
878de341 100 nb_resultats = ressources.count()
f61e4846
EMS
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,
878de341 105 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
f61e4846
EMS
106 context_instance = RequestContext(request)
107 )
108
91e0aefb 109def ressource_retrieve(request, id):
110 """Notice OAI de la ressource"""
2127a108 111 ressource = get_object_or_404(Record, id=id)
91e0aefb 112 variables = { 'ressource': ressource,
113 }
114 return render_to_response ("savoirs/ressource_retrieve.html", \
115 Context (variables),
116 context_instance = RequestContext(request))
f8c16b3d 117
2d10d74b 118def informations (request):
c08a8cb8 119 s = sep.SEP()
91ff43e9 120 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 121
72b466ce 122 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 123 for l in logs:
124 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
125
2d10d74b 126 return render_to_response ("savoirs/informations.html", \
92c7413b 127 Context ({'r': resources}), \
d15017b2
CR
128 context_instance = RequestContext(request))
129
f8c16b3d 130# actualités
131def actualite_index(request):
c9cbc784
EMS
132 search_form = ActualiteSearchForm(request.GET)
133 actualites = search_form.get_query_set()
134 search_regexp = search_form.get_search_regexp()
2f9c4d6c 135 return render_to_response("savoirs/actualite_index.html",
c9cbc784
EMS
136 dict(actualites=actualites,
137 search_form=search_form,
138 search_regexp=search_regexp,
878de341 139 nb_resultats=actualites.count()),
2f9c4d6c 140 context_instance = RequestContext(request))
5ecd5424 141
f8c16b3d 142# agenda
143def evenement_index(request):
4101cfc0
EMS
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,
878de341 151 nb_resultats=evenements.count()),
4101cfc0 152 context_instance=RequestContext(request))
0b5b6fe0 153
154def evenement_utilisation(request):
155 return render_to_response ("savoirs/evenement_utilisation.html", \
156 Context (), \
157 context_instance = RequestContext(request))
158
92c7413b 159def evenement(request, id):
4101cfc0
EMS
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))
92c7413b
CR
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')