closed #444 ajout traitement par lot de disciplines, thematiques, pays, regions
[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, evenement_publie, 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
21 def index (request):
22 delta = datetime.timedelta (days = 90)
23 oldest = datetime.date.today () - delta
24 articles = Actualite.objects.filter (visible = '1', date__gt = oldest)
25 articles = articles[0:configuration['accueil_actualite']]
26 try:
27 events = evenements()[0:configuration['accueil_evenement']]
28 except:
29 events = []
30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
32 return render_to_response ("savoirs/index.html", \
33 Context ({"articles": articles,
34 "events": events,
35 "caldav_url": configuration['calendrier_publique'],
36 "chercheurs":chercheurs,
37 "sites":sites,
38 }), \
39 context_instance = RequestContext(request))
40
41 def recherche (request):
42 q = request.GET.get("q", "")
43 page = int(request.GET.get("page", 0))
44
45 r = cherche (page, q)
46
47 return render_to_response ("savoirs/recherche.html", \
48 Context ({'q': q,
49 'page': page,
50 'data': r}), \
51 context_instance = RequestContext(request))
52
53 def avancee (request):
54 type = request.GET.get("type", "")
55 page = int(request.GET.get("page", 0))
56
57 r = {'results': [], 'last_page': 0, 'more_link': ''}
58
59 q = request.GET.get("google-q", "")
60 f = RechercheAvancee ()
61
62 if type == 'google':
63 r = cherche (page, q, type)
64 q = {'q': q}
65 elif type == 'avancee':
66 f = RechercheAvancee (request.GET)
67 if f.is_valid():
68 q = {}
69 for k in ['creator', 'title', 'description', 'subject']:
70 tmp = f.cleaned_data[k].strip()
71 if len (tmp) > 0:
72 q[k] = tmp
73 q['operator'] = '|'
74 if f.cleaned_data['operator'] == 'and':
75 q['operator'] = "&"
76
77 r = cherche (page, q, type)
78
79 return render_to_response ("savoirs/avancee.html", \
80 Context ({'type': type,
81 'page': page,
82 'data': r,
83 'form': f,
84 'q': q}),
85 context_instance = RequestContext(request))
86
87 def conseils (request):
88 return render_to_response ("savoirs/conseils.html", \
89 Context (), \
90 context_instance = RequestContext(request))
91
92 def a_propos (request):
93 return render_to_response ("savoirs/a-propos.html", \
94 Context ({'count': len(backend_config.RESOURCES)}), \
95 context_instance = RequestContext(request))
96
97 def informations (request):
98 s = sep.SEP()
99 resources = copy.deepcopy (backend_config.RESOURCES)
100 for k in backend_config.RESOURCES.keys ():
101 try:
102 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
103 except: pass
104
105 logs = HarvestLog.objects.filter(context = 'moisson').order_by('date')
106 for l in logs:
107 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
108
109 return render_to_response ("savoirs/informations.html", \
110 Context ({'r': resources}), \
111 context_instance = RequestContext(request))
112
113 def nous_contacter (request):
114 return render_to_response ("savoirs/contact.html", \
115 Context (), \
116 context_instance = RequestContext(request))
117
118
119 def evenement(request, id):
120 event = evenement_info(id)
121 return render_to_response ("savoirs/evenement.html", \
122 Context ({'event': event.instance.vevent}), \
123 context_instance = RequestContext(request))
124
125 def evenement_ajout(request):
126 template = "savoirs/evenement_ajout.html"
127 if request.method == "POST":
128 form = EvenementForm(request.POST)
129 if form.is_valid():
130 form.save()
131 template = "savoirs/evenement_confirmation.html"
132 else:
133 form = EvenementForm()
134 return render_to_response (template, \
135 Context ({'form': form}), \
136 context_instance = RequestContext(request))
137
138 @login_required
139 def evenement_moderation(request):
140 events = Evenement.objects.filter(approuve = False)
141 return render_to_response ("savoirs/evenement_moderation.html", \
142 Context ({'events': events}), \
143 context_instance = RequestContext(request))
144
145 @login_required
146 def evenement_accepter(request, pk):
147 e = Evenement.objects.get(pk = pk)
148
149 cal = vobject.iCalendar()
150 cal.add('vevent')
151 cal.vevent.add('summary').value = e.titre
152
153 kw = e.mots_cles.split(",")
154 try:
155 kw.append(e.discipline.nom)
156 kw.append(e.discipline_secondaire.nom)
157 kw.append(e.type)
158 except: pass
159
160 kw = [x.strip() for x in kw if len(x.strip()) > 0]
161 for k in kw:
162 cal.vevent.add('x-auf-keywords').value = k
163
164 if len(kw) > 0:
165 if len(e.description) > 0:
166 e.description += "\n"
167 e.description += u"Mots-cles: " + ", ".join(kw)
168
169 cal.vevent.add('dtstart').value = combine(e.debut, e.fuseau)
170 cal.vevent.add('dtend').value = combine(e.fin, e.fuseau)
171 cal.vevent.add('created').value = combine(datetime.datetime.now(), "UTC")
172 cal.vevent.add('dtstamp').value = combine(datetime.datetime.now(), "UTC")
173 if len(e.description) > 0:
174 cal.vevent.add('description').value = e.description
175 if len(e.contact) > 0:
176 cal.vevent.add('contact').value = e.contact
177 if len(e.url) > 0:
178 cal.vevent.add('url').value = e.url
179 if len(e.lieu) > 0:
180 cal.vevent.add('location').value = e.lieu
181
182 evenement_publie(cal)
183
184 e.actif = False
185 e.save()
186
187 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
188
189 @login_required
190 def evenement_refuser(request, pk):
191 evenement = Evenement.objects.get(pk = pk)
192 evenement.actif = False
193 evenement.save()
194 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
195
196
197 @login_required
198 def json_get (request):
199 uri = request.GET.get ("uri")
200 if uri:
201 s = sep.SEP ()
202 res = s.search ({'uri': uri.encode("utf-8")})
203 r = s.get (res)
204
205 return HttpResponse(simplejson.dumps(r[0]),
206 mimetype='application/json')
207
208 @login_required
209 def json_set (request):
210 data = request.POST.get("data")
211 if data:
212 r = simplejson.loads(data)
213 s = sep.SEP ()
214 s.add (r)
215 return HttpResponse(simplejson.dumps("OK"),
216 mimetype='application/json')