Créé une page individuelle pour chaque actualité
[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.utils.safestring import mark_safe
11 from django import forms
12 from django.conf import settings
13 from lib.recherche import google_search, build_search_regexp
14 from lib import sep
15 from lib.calendrier import evenements, evenement_info, combine
16 from savoirs.globals import configuration
17 import backend_config
18 from forms import *
19 from models import *
20 from chercheurs.models import Chercheur
21 from sitotheque.models import Site
22
23 # Accueil
24
25 def index(request, discipline=None, region=None):
26 """Page d'accueil"""
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]
30 chercheurs = Chercheur.objects.filter_discipline(discipline).filter_region(region).order_by('-date_modification')[:10]
31 sites = Site.objects.filter_discipline(discipline).filter_region(region).order_by('-date_maj')[:4]
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))
38
39 # sous-menu droite
40 def a_propos(request):
41 return render_to_response ("savoirs/a-propos.html", \
42 Context (), \
43 context_instance = RequestContext(request))
44
45 def nous_contacter(request):
46 return render_to_response ("savoirs/contact.html", \
47 Context ({'courriel':settings.CONTACT_EMAIL}), \
48 context_instance = RequestContext(request))
49
50 def legal(request):
51 return render_to_response ("savoirs/legal.html", \
52 Context (), \
53 context_instance = RequestContext(request))
54
55 # recherche
56 def recherche(request, discipline=None, region=None):
57 query = request.GET.get("q", "")
58 if not query.strip():
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
69 ressources = Record.objects.validated().filter_discipline(discipline).filter_region(region).search(query)
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')
73 sites = Site.objects.filter_discipline(discipline).filter_region(region).search(query)
74 try:
75 sites_auf = google_search(0, query)['results']
76 except:
77 sites_auf = []
78 search_regexp = build_search_regexp(query)
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:
85 params['discipline'] = discipline
86 if region:
87 params['region'] = region
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
93 return render_to_response(
94 "savoirs/recherche.html",
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),
102 context_instance = RequestContext(request)
103 )
104
105 def sites_auf(request):
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))
115
116 # ressources
117 def ressource_index(request):
118 search_form = RecordSearchForm(request.GET)
119 ressources = search_form.get_query_set()
120 nb_resultats = ressources.count()
121 search_regexp = search_form.get_search_regexp()
122 return render_to_response(
123 "savoirs/ressource_index.html",
124 {'search_form': search_form, 'ressources': ressources,
125 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
126 context_instance = RequestContext(request)
127 )
128
129 def ressource_retrieve(request, id):
130 """Notice OAI de la ressource"""
131 ressource = get_object_or_404(Record, id=id)
132 return render_to_response(
133 "savoirs/ressource_retrieve.html",
134 dict(ressource=ressource, disciplines=ressource.disciplines.all(),
135 regions=ressource.regions.all()),
136 context_instance=RequestContext(request)
137 )
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(
157 "savoirs/actualite_index.html",
158 dict(actualites=actualites, search_form=search_form,
159 search_regexp=search_regexp, nb_resultats=actualites.count()),
160 context_instance = RequestContext(request))
161
162 def 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
168 # agenda
169 def evenement_index(request):
170 search_form = EvenementSearchForm(request.GET)
171 evenements = search_form.get_query_set()
172 search_regexp = search_form.get_search_regexp()
173 return render_to_response(
174 "savoirs/evenement_index.html",
175 dict(evenements=evenements, search_form=search_form,
176 search_regexp=search_regexp, nb_resultats=evenements.count()),
177 context_instance=RequestContext(request)
178 )
179
180 def evenement_utilisation(request):
181 return render_to_response("savoirs/evenement_utilisation.html", {},
182 context_instance = RequestContext(request))
183
184 def evenement(request, id):
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))
189
190 def evenement_ajout(request):
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()
199 return render_to_response(template, dict(form=form),
200 context_instance=RequestContext(request))
201
202 @login_required
203 def 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
210 def evenement_accepter(request, pk):
211 e = Evenement.objects.get(pk = pk)
212 e.save()
213 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
214
215 @login_required
216 def 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
223 @login_required
224 def json_get (request):
225 uri = request.GET.get ("uri")
226 if uri:
227 s = sep.SEP ()
228 res = s.search ({'uri': uri.encode("utf-8")})
229 r = s.get (res)
230
231 return HttpResponse(simplejson.dumps(r[0]),
232 mimetype='application/json')
233
234 @login_required
235 def json_set (request):
236 data = request.POST.get("data")
237 if data:
238 r = simplejson.loads(data)
239 s = sep.SEP ()
240 s.add (r)
241 return HttpResponse(simplejson.dumps("OK"),
242 mimetype='application/json')