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