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