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