try/except sur events caldav
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
CommitLineData
d15017b2 1# -*- encoding: utf-8 -*-
92c7413b
CR
2import datetime, simplejson, copy, vobject
3
d15017b2
CR
4from django.shortcuts import render_to_response
5from django.template import Context, RequestContext
92c7413b 6from django.http import HttpResponse, HttpResponseRedirect
5ecd5424 7from django.contrib.auth.decorators import login_required
92c7413b
CR
8from django.core.urlresolvers import reverse
9
92c7413b
CR
10
11from lib.recherche import cherche, google_search
c08a8cb8 12from lib import sep
92c7413b 13from lib.calendrier import evenements, evenement_info, evenement_publie, combine
ecc62927 14from savoirs.globals import configuration
91ff43e9 15import backend_config
92c7413b
CR
16from forms import *
17from models import *
588d6b93 18from chercheurs.models import Chercheur
92c7413b 19
d15017b2
CR
20
21def 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']]
a860a843
AJ
26 try:
27 events = evenements()[0:configuration['accueil_evenement']]
28 except:
29 events = []
da091176 30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
5ecd5424 31 return render_to_response ("savoirs/index.html", \
92c7413b
CR
32 Context ({"articles": articles,
33 "events": events,
588d6b93 34 "caldav_url": configuration['calendrier_publique'],
35 "chercheurs":chercheurs,
36 }), \
d15017b2
CR
37 context_instance = RequestContext(request))
38
39def recherche (request):
d15017b2
CR
40 q = request.GET.get("q", "")
41 page = int(request.GET.get("page", 0))
42
40a5ebfb 43 r = cherche (page, q)
d15017b2 44
5ecd5424 45 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
46 Context ({'q': q,
47 'page': page,
48 'data': r}), \
49 context_instance = RequestContext(request))
50
a008c7e5
CR
51def avancee (request):
52 type = request.GET.get("type", "")
53 page = int(request.GET.get("page", 0))
54
cebcab65
CR
55 r = {'results': [], 'last_page': 0, 'more_link': ''}
56
a008c7e5 57 q = request.GET.get("google-q", "")
544b4522 58 f = RechercheAvancee ()
a008c7e5
CR
59
60 if type == 'google':
61 r = cherche (page, q, type)
544b4522
CR
62 q = {'q': q}
63 elif type == 'avancee':
64 f = RechercheAvancee (request.GET)
65 if f.is_valid():
66 q = {}
67 for k in ['creator', 'title', 'description', 'subject']:
68 tmp = f.cleaned_data[k].strip()
69 if len (tmp) > 0:
70 q[k] = tmp
71 q['operator'] = '|'
72 if f.cleaned_data['operator'] == 'and':
73 q['operator'] = "&"
74
75 r = cherche (page, q, type)
a008c7e5
CR
76
77 return render_to_response ("savoirs/avancee.html", \
78 Context ({'type': type,
79 'page': page,
80 'data': r,
544b4522 81 'form': f,
a008c7e5
CR
82 'q': q}),
83 context_instance = RequestContext(request))
84
d15017b2 85def conseils (request):
5ecd5424 86 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
87 Context (), \
88 context_instance = RequestContext(request))
89
90def a_propos (request):
5ecd5424 91 return render_to_response ("savoirs/a-propos.html", \
91ff43e9 92 Context ({'count': len(backend_config.RESOURCES)}), \
2d10d74b
CR
93 context_instance = RequestContext(request))
94
95def informations (request):
c08a8cb8 96 s = sep.SEP()
92c7413b 97 t = s.logs()
91ff43e9 98 resources = copy.deepcopy (backend_config.RESOURCES)
92c7413b 99 for k in t.keys ():
91ff43e9
CR
100 try:
101 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
102 except: pass
2d10d74b 103 return render_to_response ("savoirs/informations.html", \
92c7413b 104 Context ({'r': resources}), \
d15017b2
CR
105 context_instance = RequestContext(request))
106
107def nous_contacter (request):
5ecd5424 108 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
109 Context (), \
110 context_instance = RequestContext(request))
5ecd5424 111
92c7413b
CR
112
113def evenement(request, id):
114 event = evenement_info(id)
115 return render_to_response ("savoirs/evenement.html", \
116 Context ({'event': event.instance.vevent}), \
117 context_instance = RequestContext(request))
118
119def evenement_ajout(request):
120 template = "savoirs/evenement_ajout.html"
121 if request.method == "POST":
122 form = EvenementForm(request.POST)
123 if form.is_valid():
124 form.save()
125 template = "savoirs/evenement_confirmation.html"
126 else:
127 form = EvenementForm()
128 return render_to_response (template, \
129 Context ({'form': form}), \
130 context_instance = RequestContext(request))
131
132@login_required
133def evenement_moderation(request):
134 events = Evenement.objects.filter(approuve = False)
135 return render_to_response ("savoirs/evenement_moderation.html", \
136 Context ({'events': events}), \
137 context_instance = RequestContext(request))
138
139@login_required
140def evenement_accepter(request, pk):
141 e = Evenement.objects.get(pk = pk)
142
143 cal = vobject.iCalendar()
144 cal.add('vevent')
145 cal.vevent.add('summary').value = e.titre
146
147 kw = e.mots_cles.split(",")
148 try:
149 kw.append(e.discipline.nom)
150 kw.append(e.discipline_secondaire.nom)
151 kw.append(e.type)
152 except: pass
153
154 kw = [x.strip() for x in kw if len(x.strip()) > 0]
155 for k in kw:
156 cal.vevent.add('x-auf-keywords').value = k
157
158 if len(kw) > 0:
159 if len(e.description) > 0:
160 e.description += "\n"
161 e.description += u"Mots-cles: " + ", ".join(kw)
162
163 cal.vevent.add('dtstart').value = combine(e.debut, e.fuseau)
164 cal.vevent.add('dtend').value = combine(e.fin, e.fuseau)
165 cal.vevent.add('created').value = combine(datetime.datetime.now(), "UTC")
166 cal.vevent.add('dtstamp').value = combine(datetime.datetime.now(), "UTC")
167 if len(e.description) > 0:
168 cal.vevent.add('description').value = e.description
169 if len(e.contact) > 0:
170 cal.vevent.add('contact').value = e.contact
171 if len(e.url) > 0:
172 cal.vevent.add('url').value = e.url
173 if len(e.lieu) > 0:
174 cal.vevent.add('location').value = e.lieu
175
176 evenement_publie(cal)
177
178 e.actif = False
179 e.save()
180
181 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
182
183@login_required
184def evenement_refuser(request, pk):
185 evenement = Evenement.objects.get(pk = pk)
186 evenement.actif = False
187 evenement.save()
188 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
189
190
5ecd5424
CR
191@login_required
192def json_get (request):
193 uri = request.GET.get ("uri")
194 if uri:
c08a8cb8 195 s = sep.SEP ()
5ecd5424 196 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 197 r = s.get (res)
5ecd5424 198
c08a8cb8 199 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
200 mimetype='application/json')
201
202@login_required
203def json_set (request):
204 data = request.POST.get("data")
205 if data:
206 r = simplejson.loads(data)
c08a8cb8 207 s = sep.SEP ()
5ecd5424
CR
208 s.add (r)
209 return HttpResponse(simplejson.dumps("OK"),
210 mimetype='application/json')