texte de présentation agenda + conditions utilisation
[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
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 cherche, 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 ressources = Record.objects.validated().search(query)
61 actualites = Actualite.objects.filter(visible=1).search(query)
62 evenements = Evenement.objects.filter(approuve=1).search(query)
63 chercheurs = Chercheur.objects.search(query)
64 sites = Site.objects.search(query)
65 search_regexp = build_search_regexp(query)
66 return render_to_response(
67 "savoirs/recherche.html",
68 dict(q=query, search_regexp=search_regexp,
69 ressources=ressources[:5], total_ressources=ressources.count(),
70 evenements=evenements[:5], total_evenements=evenements.count(),
71 chercheurs=chercheurs[:10], total_chercheurs=chercheurs.count(),
72 actualites=actualites[:5], total_actualites=actualites.count(),
73 sites=sites[:5], total_sites=sites.count()),
74 context_instance = RequestContext(request)
75 )
76
77 def avancee (request):
78 type = request.GET.get("type", "")
79 page = int(request.GET.get("page", 0))
80
81 r = {'results': [], 'last_page': 0, 'more_link': ''}
82
83 q = request.GET.get("google-q", "")
84 f = RechercheAvancee ()
85
86 if type == 'google':
87 r = cherche (page, q, type)
88 q = {'q': q}
89 elif type == 'avancee':
90 f = RechercheAvancee (request.GET)
91 if f.is_valid():
92 q = {}
93 for k in ['creator', 'title', 'description', 'subject']:
94 tmp = f.cleaned_data[k].strip()
95 if len (tmp) > 0:
96 q[k] = tmp
97 q['operator'] = '|'
98 if f.cleaned_data['operator'] == 'and':
99 q['operator'] = "&"
100
101 r = cherche (page, q, type)
102
103 return render_to_response ("savoirs/avancee.html", \
104 Context ({'type': type,
105 'page': page,
106 'data': r,
107 'form': f,
108 'q': q}),
109 context_instance = RequestContext(request))
110
111 def conseils (request):
112 return render_to_response ("savoirs/conseils.html", \
113 Context (), \
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 = Record.objects.get(id=id)
132 variables = { 'ressource': ressource,
133 }
134 return render_to_response ("savoirs/ressource_retrieve.html", \
135 Context (variables),
136 context_instance = RequestContext(request))
137
138 def informations (request):
139 s = sep.SEP()
140 resources = copy.deepcopy (backend_config.RESOURCES)
141
142 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
143 for l in logs:
144 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
145
146 return render_to_response ("savoirs/informations.html", \
147 Context ({'r': resources}), \
148 context_instance = RequestContext(request))
149
150 # actualités
151 def actualite_index(request):
152 search_form = ActualiteSearchForm(request.GET)
153 actualites = search_form.get_query_set()
154 search_regexp = search_form.get_search_regexp()
155 return render_to_response("savoirs/actualite_index.html",
156 dict(actualites=actualites,
157 search_form=search_form,
158 search_regexp=search_regexp,
159 nb_resultats=actualites.count()),
160 context_instance = RequestContext(request))
161
162 # agenda
163 def evenement_index(request):
164 search_form = EvenementSearchForm(request.GET)
165 evenements = search_form.get_query_set()
166 search_regexp = search_form.get_search_regexp()
167 return render_to_response("savoirs/evenement_index.html",
168 dict(evenements=evenements,
169 search_form=search_form,
170 search_regexp=search_regexp,
171 nb_resultats=evenements.count()),
172 context_instance=RequestContext(request))
173
174 def evenement_utilisation(request):
175 return render_to_response ("savoirs/evenement_utilisation.html", \
176 Context (), \
177 context_instance = RequestContext(request))
178
179 def evenement(request, id):
180 evenement = get_object_or_404(Evenement, pk=id)
181 return render_to_response("savoirs/evenement.html",
182 dict(evenement=evenement),
183 context_instance=RequestContext(request))
184
185 def evenement_ajout(request):
186 template = "savoirs/evenement_ajout.html"
187 if request.method == "POST":
188 form = EvenementForm(request.POST)
189 if form.is_valid():
190 form.save()
191 template = "savoirs/evenement_confirmation.html"
192 else:
193 form = EvenementForm()
194 return render_to_response (template, \
195 Context ({'form': form}), \
196 context_instance = RequestContext(request))
197
198 @login_required
199 def evenement_moderation(request):
200 events = Evenement.objects.filter(approuve = False)
201 return render_to_response ("savoirs/evenement_moderation.html", \
202 Context ({'events': events}), \
203 context_instance = RequestContext(request))
204
205 @login_required
206 def evenement_accepter(request, pk):
207 e = Evenement.objects.get(pk = pk)
208 e.save()
209 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
210
211 @login_required
212 def evenement_refuser(request, pk):
213 evenement = Evenement.objects.get(pk = pk)
214 evenement.actif = False
215 evenement.save()
216 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
217
218
219 @login_required
220 def json_get (request):
221 uri = request.GET.get ("uri")
222 if uri:
223 s = sep.SEP ()
224 res = s.search ({'uri': uri.encode("utf-8")})
225 r = s.get (res)
226
227 return HttpResponse(simplejson.dumps(r[0]),
228 mimetype='application/json')
229
230 @login_required
231 def json_set (request):
232 data = request.POST.get("data")
233 if data:
234 r = simplejson.loads(data)
235 s = sep.SEP ()
236 s.add (r)
237 return HttpResponse(simplejson.dumps("OK"),
238 mimetype='application/json')