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