prefixe mail admin
[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 import forms
10 from django.conf import settings
11 from lib.recherche import google_search, build_search_regexp
12 from lib import sep
13 from lib.calendrier import evenements, evenement_info, combine
14 from savoirs.globals import configuration
15 import backend_config
16 from forms import *
17 from models import *
18 from chercheurs.models import Chercheur
19 from sitotheque.models import Site
20
21 # sous-menu gauche
22 def index (request):
23 """Page d'accueil"""
24 delta = datetime.timedelta (days = 90)
25 oldest = datetime.date.today () - delta
26 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
27 actualites = actualites[0:configuration['accueil_actualite']]
28 evenements = Evenement.objects.filter(approuve=True)[0:configuration['accueil_evenement']]
29 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
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))
40
41 # sous-menu droite
42 def a_propos (request):
43 return render_to_response ("savoirs/a-propos.html", \
44 Context (), \
45 context_instance = RequestContext(request))
46
47 def nous_contacter (request):
48 return render_to_response ("savoirs/contact.html", \
49 Context ({'courriel':settings.CONTACT_EMAIL}), \
50 context_instance = RequestContext(request))
51
52 def legal(request):
53 return render_to_response ("savoirs/legal.html", \
54 Context (), \
55 context_instance = RequestContext(request))
56
57 # recherche
58 def recherche(request):
59 query = request.GET.get("q", "")
60 if not query.strip():
61 return redirect('/')
62 ressources = Record.objects.validated().search(query)
63 actualites = Actualite.objects.filter(visible=1).search(query)
64 evenements = Evenement.objects.filter(approuve=1).search(query)
65 chercheurs = Chercheur.objects.search(query)
66 sites = Site.objects.search(query)
67 try:
68 sites_auf = google_search(0, query)['results']
69 except:
70 sites_auf = []
71 search_regexp = build_search_regexp(query)
72 return render_to_response(
73 "savoirs/recherche.html",
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(),
79 sites=sites[:5], total_sites=sites.count(),
80 sites_auf=sites_auf[:5]),
81 context_instance = RequestContext(request)
82 )
83
84 def 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))
94
95 # ressources
96 def ressource_index(request):
97 search_form = RecordSearchForm(request.GET)
98 ressources = search_form.get_query_set()
99 nb_resultats = ressources.count()
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,
104 'nb_resultats': nb_resultats, 'search_regexp': search_regexp},
105 context_instance = RequestContext(request)
106 )
107
108 def 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))
116
117 def informations (request):
118 s = sep.SEP()
119 resources = copy.deepcopy (backend_config.RESOURCES)
120
121 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
122 for l in logs:
123 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
124
125 return render_to_response ("savoirs/informations.html", \
126 Context ({'r': resources}), \
127 context_instance = RequestContext(request))
128
129 # actualités
130 def actualite_index(request):
131 search_form = ActualiteSearchForm(request.GET)
132 actualites = search_form.get_query_set()
133 search_regexp = search_form.get_search_regexp()
134 return render_to_response("savoirs/actualite_index.html",
135 dict(actualites=actualites,
136 search_form=search_form,
137 search_regexp=search_regexp,
138 nb_resultats=actualites.count()),
139 context_instance = RequestContext(request))
140
141 # agenda
142 def evenement_index(request):
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,
150 nb_resultats=evenements.count()),
151 context_instance=RequestContext(request))
152
153 def evenement_utilisation(request):
154 return render_to_response ("savoirs/evenement_utilisation.html", \
155 Context (), \
156 context_instance = RequestContext(request))
157
158 def evenement(request, id):
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))
163
164 def 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
178 def 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
185 def evenement_accepter(request, pk):
186 e = Evenement.objects.get(pk = pk)
187 e.save()
188 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
189
190 @login_required
191 def 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
198 @login_required
199 def json_get (request):
200 uri = request.GET.get ("uri")
201 if uri:
202 s = sep.SEP ()
203 res = s.search ({'uri': uri.encode("utf-8")})
204 r = s.get (res)
205
206 return HttpResponse(simplejson.dumps(r[0]),
207 mimetype='application/json')
208
209 @login_required
210 def json_set (request):
211 data = request.POST.get("data")
212 if data:
213 r = simplejson.loads(data)
214 s = sep.SEP ()
215 s.add (r)
216 return HttpResponse(simplejson.dumps("OK"),
217 mimetype='application/json')