Créé une page individuelle pour chaque actualité
[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
f0692c02 10from django.utils.safestring import mark_safe
e3c3296e 11from django import forms
0e9597af 12from django.conf import settings
116db1fd 13from lib.recherche import google_search, build_search_regexp
c08a8cb8 14from lib import sep
b7a741ad 15from lib.calendrier import evenements, evenement_info, combine
ecc62927 16from savoirs.globals import configuration
91ff43e9 17import backend_config
92c7413b
CR
18from forms import *
19from models import *
588d6b93 20from chercheurs.models import Chercheur
bab65667 21from sitotheque.models import Site
92c7413b 22
bae03b7b
EMS
23# Accueil
24
f0692c02 25def index(request, discipline=None, region=None):
f8c16b3d 26 """Page d'accueil"""
b7b93c65
EMS
27 actualites = Actualite.objects.filter(visible=True).filter_discipline(discipline).filter_region(region).order_by('-date')[:4]
28 evenements = Evenement.objects.filter(approuve=True).filter_discipline(discipline).filter_region(region).order_by('-debut')[:4]
29 ressources = Record.objects.validated().filter_discipline(discipline).filter_region(region).order_by('-id')[:4]
116db1fd 30 chercheurs = Chercheur.objects.filter_discipline(discipline).filter_region(region).order_by('-date_modification')[:10]
b7b93c65 31 sites = Site.objects.filter_discipline(discipline).filter_region(region).order_by('-date_maj')[:4]
bae03b7b
EMS
32 return render_to_response(
33 "savoirs/index.html",
34 dict(actualites=actualites, evenements=evenements,
35 caldav_url=configuration['calendrier_publique'],
36 ressources=ressources, chercheurs=chercheurs, sites=sites),
37 context_instance = RequestContext(request))
d15017b2 38
f8c16b3d 39# sous-menu droite
f0692c02 40def a_propos(request):
f8c16b3d 41 return render_to_response ("savoirs/a-propos.html", \
e37563d1 42 Context (), \
f8c16b3d 43 context_instance = RequestContext(request))
44
f0692c02 45def nous_contacter(request):
f8c16b3d 46 return render_to_response ("savoirs/contact.html", \
0e9597af 47 Context ({'courriel':settings.CONTACT_EMAIL}), \
f8c16b3d 48 context_instance = RequestContext(request))
e37563d1 49
f0692c02 50def legal(request):
e37563d1 51 return render_to_response ("savoirs/legal.html", \
52 Context (), \
53 context_instance = RequestContext(request))
f8c16b3d 54
55# recherche
c1b134f8 56def recherche(request, discipline=None, region=None):
0b72ebef 57 query = request.GET.get("q", "")
3c23982e 58 if not query.strip():
37cce5f0
EMS
59
60 # Si on n'a pas de recherche par mots-clés, on redirige vers
61 # l'accueil.
62 kwargs = {}
63 if discipline:
64 kwargs['discipline'] = discipline
65 if region:
66 kwargs['region'] = region
67 return HttpResponseRedirect(reverse('savoirs.views.index', kwargs=kwargs))
68
116db1fd 69 ressources = Record.objects.validated().filter_discipline(discipline).filter_region(region).search(query)
7020ea3d
EMS
70 actualites = Actualite.objects.filter(visible=1).filter_discipline(discipline).filter_region(region).search(query).order_by('-date')
71 evenements = Evenement.objects.filter(approuve=1).filter_discipline(discipline).filter_region(region).search(query).order_by('-debut')
72 chercheurs = Chercheur.objects.filter_discipline(discipline).filter_region(region).search(query).order_by('-date_modification')
116db1fd 73 sites = Site.objects.filter_discipline(discipline).filter_region(region).search(query)
184f139f
EMS
74 try:
75 sites_auf = google_search(0, query)['results']
76 except:
77 sites_auf = []
116db1fd 78 search_regexp = build_search_regexp(query)
f0692c02
EMS
79
80 # Bâtissons une query string pour les liens vers les briques
81 params = {}
82 if query:
83 params['q'] = query
84 if discipline:
116db1fd 85 params['discipline'] = discipline
f0692c02 86 if region:
116db1fd 87 params['region'] = region
f0692c02
EMS
88 if params:
89 briques_query_string = mark_safe('?' + '&'.join(k + '=' + v.replace('"', '"') for (k, v) in params.iteritems()))
90 else:
91 briques_query_string = None
92
0b72ebef
EMS
93 return render_to_response(
94 "savoirs/recherche.html",
116db1fd
EMS
95 dict(q=query, search_regexp=search_regexp,
96 ressources=ressources[:5], total_ressources=ressources.count(),
97 evenements=evenements[:5], total_evenements=evenements.count(),
98 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
99 actualites=actualites[:5], total_actualites=actualites.count(),
100 sites=sites[:5], total_sites=sites.count(),
101 sites_auf=sites_auf[:5], briques_query_string=briques_query_string),
0b72ebef
EMS
102 context_instance = RequestContext(request)
103 )
d15017b2 104
f0692c02 105def sites_auf(request):
0b1ddc11
EMS
106 q = request.GET.get('q')
107 page = int(request.GET.get('page', 0))
108 try:
109 data = google_search(page, q) if q else None
110 except:
111 data = None
112 return render_to_response('savoirs/sites_auf.html',
113 dict(google_q=q, data=data, page=page),
114 context_instance=RequestContext(request))
d15017b2 115
f8c16b3d 116# ressources
f0692c02 117def ressource_index(request):
da44ce68 118 search_form = RecordSearchForm(request.GET)
f0692c02 119 ressources = search_form.get_query_set()
878de341 120 nb_resultats = ressources.count()
116db1fd 121 search_regexp = search_form.get_search_regexp()
f61e4846
EMS
122 return render_to_response(
123 "savoirs/ressource_index.html",
116db1fd
EMS
124 {'search_form': search_form, 'ressources': ressources,
125 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
126 context_instance = RequestContext(request)
f61e4846
EMS
127 )
128
f0692c02 129def ressource_retrieve(request, id):
91e0aefb 130 """Notice OAI de la ressource"""
2127a108 131 ressource = get_object_or_404(Record, id=id)
c1b134f8
EMS
132 return render_to_response(
133 "savoirs/ressource_retrieve.html",
06f7a401 134 dict(ressource=ressource, disciplines=ressource.disciplines.all(),
c1b134f8
EMS
135 regions=ressource.regions.all()),
136 context_instance=RequestContext(request)
137 )
f8c16b3d 138
2d10d74b 139def informations (request):
c08a8cb8 140 s = sep.SEP()
91ff43e9 141 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 142
72b466ce 143 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 144 for l in logs:
145 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
146
2d10d74b 147 return render_to_response ("savoirs/informations.html", \
92c7413b 148 Context ({'r': resources}), \
d15017b2
CR
149 context_instance = RequestContext(request))
150
f8c16b3d 151# actualités
f0692c02 152def actualite_index(request):
c9cbc784 153 search_form = ActualiteSearchForm(request.GET)
f0692c02 154 actualites = search_form.get_query_set()
116db1fd 155 search_regexp = search_form.get_search_regexp()
c1b134f8
EMS
156 return render_to_response(
157 "savoirs/actualite_index.html",
158 dict(actualites=actualites, search_form=search_form,
116db1fd 159 search_regexp=search_regexp, nb_resultats=actualites.count()),
c1b134f8 160 context_instance = RequestContext(request))
5ecd5424 161
c30f12c5
EMS
162def actualite(request, id):
163 actualite = get_object_or_404(Actualite, pk=id)
164 return render_to_response("savoirs/actualite.html",
165 dict(actualite=actualite),
166 context_instance=RequestContext(request))
167
f8c16b3d 168# agenda
f0692c02 169def evenement_index(request):
4101cfc0 170 search_form = EvenementSearchForm(request.GET)
f0692c02 171 evenements = search_form.get_query_set()
116db1fd 172 search_regexp = search_form.get_search_regexp()
c1b134f8
EMS
173 return render_to_response(
174 "savoirs/evenement_index.html",
175 dict(evenements=evenements, search_form=search_form,
116db1fd 176 search_regexp=search_regexp, nb_resultats=evenements.count()),
c1b134f8
EMS
177 context_instance=RequestContext(request)
178 )
d566e9c1 179
f0692c02 180def evenement_utilisation(request):
c1b134f8
EMS
181 return render_to_response("savoirs/evenement_utilisation.html", {},
182 context_instance = RequestContext(request))
0b5b6fe0 183
f0692c02 184def evenement(request, id):
4101cfc0
EMS
185 evenement = get_object_or_404(Evenement, pk=id)
186 return render_to_response("savoirs/evenement.html",
187 dict(evenement=evenement),
188 context_instance=RequestContext(request))
92c7413b 189
f0692c02 190def evenement_ajout(request):
92c7413b
CR
191 template = "savoirs/evenement_ajout.html"
192 if request.method == "POST":
193 form = EvenementForm(request.POST)
194 if form.is_valid():
195 form.save()
196 template = "savoirs/evenement_confirmation.html"
197 else:
198 form = EvenementForm()
c1b134f8
EMS
199 return render_to_response(template, dict(form=form),
200 context_instance=RequestContext(request))
92c7413b
CR
201
202@login_required
203def evenement_moderation(request):
204 events = Evenement.objects.filter(approuve = False)
205 return render_to_response ("savoirs/evenement_moderation.html", \
206 Context ({'events': events}), \
207 context_instance = RequestContext(request))
208
209@login_required
210def evenement_accepter(request, pk):
211 e = Evenement.objects.get(pk = pk)
92c7413b 212 e.save()
92c7413b
CR
213 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
214
215@login_required
216def evenement_refuser(request, pk):
217 evenement = Evenement.objects.get(pk = pk)
218 evenement.actif = False
219 evenement.save()
220 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
221
222
5ecd5424
CR
223@login_required
224def json_get (request):
225 uri = request.GET.get ("uri")
226 if uri:
c08a8cb8 227 s = sep.SEP ()
5ecd5424 228 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 229 r = s.get (res)
5ecd5424 230
c08a8cb8 231 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
232 mimetype='application/json')
233
234@login_required
235def json_set (request):
236 data = request.POST.get("data")
237 if data:
238 r = simplejson.loads(data)
c08a8cb8 239 s = sep.SEP ()
5ecd5424
CR
240 s.add (r)
241 return HttpResponse(simplejson.dumps("OK"),
242 mimetype='application/json')