Chercheurs
[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 *
18
d15017b2
CR
19
20def 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']]
92c7413b 25 events = evenements()[0:configuration['accueil_evenement']]
5ecd5424 26 return render_to_response ("savoirs/index.html", \
92c7413b
CR
27 Context ({"articles": articles,
28 "events": events,
29 "caldav_url": configuration['calendrier_publique']}), \
d15017b2
CR
30 context_instance = RequestContext(request))
31
32def recherche (request):
d15017b2
CR
33 q = request.GET.get("q", "")
34 page = int(request.GET.get("page", 0))
35
40a5ebfb 36 r = cherche (page, q)
d15017b2 37
5ecd5424 38 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
39 Context ({'q': q,
40 'page': page,
41 'data': r}), \
42 context_instance = RequestContext(request))
43
a008c7e5
CR
44def avancee (request):
45 type = request.GET.get("type", "")
46 page = int(request.GET.get("page", 0))
47
cebcab65
CR
48 r = {'results': [], 'last_page': 0, 'more_link': ''}
49
a008c7e5 50 q = request.GET.get("google-q", "")
544b4522 51 f = RechercheAvancee ()
a008c7e5
CR
52
53 if type == 'google':
54 r = cherche (page, q, type)
544b4522
CR
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)
a008c7e5
CR
69
70 return render_to_response ("savoirs/avancee.html", \
71 Context ({'type': type,
72 'page': page,
73 'data': r,
544b4522 74 'form': f,
a008c7e5
CR
75 'q': q}),
76 context_instance = RequestContext(request))
77
d15017b2 78def conseils (request):
5ecd5424 79 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
80 Context (), \
81 context_instance = RequestContext(request))
82
83def a_propos (request):
5ecd5424 84 return render_to_response ("savoirs/a-propos.html", \
91ff43e9 85 Context ({'count': len(backend_config.RESOURCES)}), \
2d10d74b
CR
86 context_instance = RequestContext(request))
87
88def informations (request):
c08a8cb8 89 s = sep.SEP()
92c7413b 90 t = s.logs()
91ff43e9 91 resources = copy.deepcopy (backend_config.RESOURCES)
92c7413b 92 for k in t.keys ():
91ff43e9
CR
93 try:
94 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
95 except: pass
2d10d74b 96 return render_to_response ("savoirs/informations.html", \
92c7413b 97 Context ({'r': resources}), \
d15017b2
CR
98 context_instance = RequestContext(request))
99
100def nous_contacter (request):
5ecd5424 101 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
102 Context (), \
103 context_instance = RequestContext(request))
5ecd5424 104
92c7413b
CR
105
106def 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
112def 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
126def 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
133def 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
177def 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
5ecd5424
CR
184@login_required
185def json_get (request):
186 uri = request.GET.get ("uri")
187 if uri:
c08a8cb8 188 s = sep.SEP ()
5ecd5424 189 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 190 r = s.get (res)
5ecd5424 191
c08a8cb8 192 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
193 mimetype='application/json')
194
195@login_required
196def json_set (request):
197 data = request.POST.get("data")
198 if data:
199 r = simplejson.loads(data)
c08a8cb8 200 s = sep.SEP ()
5ecd5424
CR
201 s.add (r)
202 return HttpResponse(simplejson.dumps("OK"),
203 mimetype='application/json')