index actualites
[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 delta = datetime.timedelta (days = 90)
129 oldest = datetime.date.today () - delta
130 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
131 return render_to_response ("savoirs/actualite_index.html", \
132 Context ({'actualites': actualites}), \
133 context_instance = RequestContext(request))
134
135 # agenda
136 def evenement_index(request):
137 return render_to_response ("savoirs/evenement_index.html", \
138 Context (), \
139 context_instance = RequestContext(request))
140
141 def evenement(request, id):
142 event = evenement_info(id)
143 return render_to_response ("savoirs/evenement.html", \
144 Context ({'event': event.instance.vevent}), \
145 context_instance = RequestContext(request))
146
147 def evenement_ajout(request):
148 template = "savoirs/evenement_ajout.html"
149 if request.method == "POST":
150 form = EvenementForm(request.POST)
151 if form.is_valid():
152 form.save()
153 template = "savoirs/evenement_confirmation.html"
154 else:
155 form = EvenementForm()
156 return render_to_response (template, \
157 Context ({'form': form}), \
158 context_instance = RequestContext(request))
159
160 @login_required
161 def evenement_moderation(request):
162 events = Evenement.objects.filter(approuve = False)
163 return render_to_response ("savoirs/evenement_moderation.html", \
164 Context ({'events': events}), \
165 context_instance = RequestContext(request))
166
167 @login_required
168 def evenement_accepter(request, pk):
169 e = Evenement.objects.get(pk = pk)
170 e.save()
171 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
172
173 @login_required
174 def evenement_refuser(request, pk):
175 evenement = Evenement.objects.get(pk = pk)
176 evenement.actif = False
177 evenement.save()
178 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
179
180
181 @login_required
182 def json_get (request):
183 uri = request.GET.get ("uri")
184 if uri:
185 s = sep.SEP ()
186 res = s.search ({'uri': uri.encode("utf-8")})
187 r = s.get (res)
188
189 return HttpResponse(simplejson.dumps(r[0]),
190 mimetype='application/json')
191
192 @login_required
193 def json_set (request):
194 data = request.POST.get("data")
195 if data:
196 r = simplejson.loads(data)
197 s = sep.SEP ()
198 s.add (r)
199 return HttpResponse(simplejson.dumps("OK"),
200 mimetype='application/json')