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