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