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