try/except sur events caldav
[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
10
11 from lib.recherche import cherche, google_search
12 from lib import sep
13 from lib.calendrier import evenements, evenement_info, evenement_publie, combine
14 from savoirs.globals import configuration
15 import backend_config
16 from forms import *
17 from models import *
18 from chercheurs.models import Chercheur
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 return render_to_response ("savoirs/index.html", \
32 Context ({"articles": articles,
33 "events": events,
34 "caldav_url": configuration['calendrier_publique'],
35 "chercheurs":chercheurs,
36 }), \
37 context_instance = RequestContext(request))
38
39 def recherche (request):
40 q = request.GET.get("q", "")
41 page = int(request.GET.get("page", 0))
42
43 r = cherche (page, q)
44
45 return render_to_response ("savoirs/recherche.html", \
46 Context ({'q': q,
47 'page': page,
48 'data': r}), \
49 context_instance = RequestContext(request))
50
51 def avancee (request):
52 type = request.GET.get("type", "")
53 page = int(request.GET.get("page", 0))
54
55 r = {'results': [], 'last_page': 0, 'more_link': ''}
56
57 q = request.GET.get("google-q", "")
58 f = RechercheAvancee ()
59
60 if type == 'google':
61 r = cherche (page, q, type)
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)
76
77 return render_to_response ("savoirs/avancee.html", \
78 Context ({'type': type,
79 'page': page,
80 'data': r,
81 'form': f,
82 'q': q}),
83 context_instance = RequestContext(request))
84
85 def conseils (request):
86 return render_to_response ("savoirs/conseils.html", \
87 Context (), \
88 context_instance = RequestContext(request))
89
90 def a_propos (request):
91 return render_to_response ("savoirs/a-propos.html", \
92 Context ({'count': len(backend_config.RESOURCES)}), \
93 context_instance = RequestContext(request))
94
95 def informations (request):
96 s = sep.SEP()
97 t = s.logs()
98 resources = copy.deepcopy (backend_config.RESOURCES)
99 for k in t.keys ():
100 try:
101 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
102 except: pass
103 return render_to_response ("savoirs/informations.html", \
104 Context ({'r': resources}), \
105 context_instance = RequestContext(request))
106
107 def nous_contacter (request):
108 return render_to_response ("savoirs/contact.html", \
109 Context (), \
110 context_instance = RequestContext(request))
111
112
113 def 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
119 def 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
133 def 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
140 def 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
184 def 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
191 @login_required
192 def json_get (request):
193 uri = request.GET.get ("uri")
194 if uri:
195 s = sep.SEP ()
196 res = s.search ({'uri': uri.encode("utf-8")})
197 r = s.get (res)
198
199 return HttpResponse(simplejson.dumps(r[0]),
200 mimetype='application/json')
201
202 @login_required
203 def json_set (request):
204 data = request.POST.get("data")
205 if data:
206 r = simplejson.loads(data)
207 s = sep.SEP ()
208 s.add (r)
209 return HttpResponse(simplejson.dumps("OK"),
210 mimetype='application/json')