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