Merge branch 'master' of gitosis@git.auf:auf_savoirs_en_partage_django
[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
e3c3296e 9from django import forms
0e9597af 10from django.conf import settings
0b1ddc11 11from lib.recherche import google_search, build_search_regexp
c08a8cb8 12from lib import sep
b7a741ad 13from lib.calendrier import evenements, evenement_info, combine
ecc62927 14from savoirs.globals import configuration
91ff43e9 15import backend_config
92c7413b
CR
16from forms import *
17from models import *
588d6b93 18from chercheurs.models import Chercheur
bab65667 19from sitotheque.models import Site
92c7413b 20
f8c16b3d 21# sous-menu gauche
d15017b2 22def index (request):
f8c16b3d 23 """Page d'accueil"""
d15017b2
CR
24 delta = datetime.timedelta (days = 90)
25 oldest = datetime.date.today () - delta
c4c5de1b 26 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
27 actualites = actualites[0:configuration['accueil_actualite']]
4101cfc0 28 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
91e0aefb 29 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
da091176 30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
4101cfc0
EMS
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))
d15017b2 40
f8c16b3d 41# sous-menu droite
42def a_propos (request):
43 return render_to_response ("savoirs/a-propos.html", \
e37563d1 44 Context (), \
f8c16b3d 45 context_instance = RequestContext(request))
46
47def nous_contacter (request):
48 return render_to_response ("savoirs/contact.html", \
0e9597af 49 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 50 context_instance = RequestContext(request))
e37563d1 51
52def legal(request):
53 return render_to_response ("savoirs/legal.html", \
54 Context (), \
55 context_instance = RequestContext(request))
f8c16b3d 56
57# recherche
a2c6bb72 58def recherche(request):
0b72ebef 59 query = request.GET.get("q", "")
3c23982e
EMS
60 if not query.strip():
61 return redirect('/')
f153be1b
EMS
62 ressources = Record.objects.validated().search(query)
63 actualites = Actualite.objects.filter(visible=1).search(query)
7d07a79f 64 evenements = Evenement.objects.filter(approuve=1).search(query)
a2c6bb72 65 chercheurs = Chercheur.objects.search(query)
878de341 66 sites = Site.objects.search(query)
184f139f
EMS
67 try:
68 sites_auf = google_search(0, query)['results']
69 except:
70 sites_auf = []
0b72ebef
EMS
71 search_regexp = build_search_regexp(query)
72 return render_to_response(
73 "savoirs/recherche.html",
878de341
EMS
74 dict(q=query, search_regexp=search_regexp,
75 ressources=ressources[:5], total_ressources=ressources.count(),
76 evenements=evenements[:5], total_evenements=evenements.count(),
77 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
78 actualites=actualites[:5], total_actualites=actualites.count(),
184f139f
EMS
79 sites=sites[:5], total_sites=sites.count(),
80 sites_auf=sites_auf[:5]),
0b72ebef
EMS
81 context_instance = RequestContext(request)
82 )
d15017b2 83
0b1ddc11
EMS
84def sites_auf(request):
85 q = request.GET.get('q')
86 page = int(request.GET.get('page', 0))
87 try:
88 data = google_search(page, q) if q else None
89 except:
90 data = None
91 return render_to_response('savoirs/sites_auf.html',
92 dict(google_q=q, data=data, page=page),
93 context_instance=RequestContext(request))
d15017b2 94
f8c16b3d 95# ressources
96def ressource_index(request):
da44ce68
EMS
97 search_form = RecordSearchForm(request.GET)
98 ressources = search_form.get_query_set()
878de341 99 nb_resultats = ressources.count()
f61e4846
EMS
100 search_regexp = search_form.get_search_regexp()
101 return render_to_response(
102 "savoirs/ressource_index.html",
103 {'search_form': search_form, 'ressources': ressources,
878de341 104 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
f61e4846
EMS
105 context_instance = RequestContext(request)
106 )
107
91e0aefb 108def ressource_retrieve(request, id):
109 """Notice OAI de la ressource"""
110 ressource = Record.objects.get(id=id)
111 variables = { 'ressource': ressource,
112 }
113 return render_to_response ("savoirs/ressource_retrieve.html", \
114 Context (variables),
115 context_instance = RequestContext(request))
f8c16b3d 116
2d10d74b 117def informations (request):
c08a8cb8 118 s = sep.SEP()
91ff43e9 119 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 120
72b466ce 121 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 122 for l in logs:
123 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
124
2d10d74b 125 return render_to_response ("savoirs/informations.html", \
92c7413b 126 Context ({'r': resources}), \
d15017b2
CR
127 context_instance = RequestContext(request))
128
f8c16b3d 129# actualités
130def actualite_index(request):
c9cbc784
EMS
131 search_form = ActualiteSearchForm(request.GET)
132 actualites = search_form.get_query_set()
133 search_regexp = search_form.get_search_regexp()
2f9c4d6c 134 return render_to_response("savoirs/actualite_index.html",
c9cbc784
EMS
135 dict(actualites=actualites,
136 search_form=search_form,
137 search_regexp=search_regexp,
878de341 138 nb_resultats=actualites.count()),
2f9c4d6c 139 context_instance = RequestContext(request))
5ecd5424 140
f8c16b3d 141# agenda
142def evenement_index(request):
4101cfc0
EMS
143 search_form = EvenementSearchForm(request.GET)
144 evenements = search_form.get_query_set()
145 search_regexp = search_form.get_search_regexp()
146 return render_to_response("savoirs/evenement_index.html",
147 dict(evenements=evenements,
148 search_form=search_form,
149 search_regexp=search_regexp,
878de341 150 nb_resultats=evenements.count()),
4101cfc0 151 context_instance=RequestContext(request))
0b5b6fe0 152
153def evenement_utilisation(request):
154 return render_to_response ("savoirs/evenement_utilisation.html", \
155 Context (), \
156 context_instance = RequestContext(request))
157
92c7413b 158def evenement(request, id):
4101cfc0
EMS
159 evenement = get_object_or_404(Evenement, pk=id)
160 return render_to_response("savoirs/evenement.html",
161 dict(evenement=evenement),
162 context_instance=RequestContext(request))
92c7413b
CR
163
164def evenement_ajout(request):
165 template = "savoirs/evenement_ajout.html"
166 if request.method == "POST":
167 form = EvenementForm(request.POST)
168 if form.is_valid():
169 form.save()
170 template = "savoirs/evenement_confirmation.html"
171 else:
172 form = EvenementForm()
173 return render_to_response (template, \
174 Context ({'form': form}), \
175 context_instance = RequestContext(request))
176
177@login_required
178def evenement_moderation(request):
179 events = Evenement.objects.filter(approuve = False)
180 return render_to_response ("savoirs/evenement_moderation.html", \
181 Context ({'events': events}), \
182 context_instance = RequestContext(request))
183
184@login_required
185def evenement_accepter(request, pk):
186 e = Evenement.objects.get(pk = pk)
92c7413b 187 e.save()
92c7413b
CR
188 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
189
190@login_required
191def evenement_refuser(request, pk):
192 evenement = Evenement.objects.get(pk = pk)
193 evenement.actif = False
194 evenement.save()
195 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
196
197
5ecd5424
CR
198@login_required
199def json_get (request):
200 uri = request.GET.get ("uri")
201 if uri:
c08a8cb8 202 s = sep.SEP ()
5ecd5424 203 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 204 r = s.get (res)
5ecd5424 205
c08a8cb8 206 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
207 mimetype='application/json')
208
209@login_required
210def json_set (request):
211 data = request.POST.get("data")
212 if data:
213 r = simplejson.loads(data)
c08a8cb8 214 s = sep.SEP ()
5ecd5424
CR
215 s.add (r)
216 return HttpResponse(simplejson.dumps("OK"),
217 mimetype='application/json')