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