kick-off templates chercheurs
[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 events = evenements()[0:configuration['accueil_evenement']]
27 chercheurs = Chercheur.objects.all().order_by('?')[:5]
28 return render_to_response ("savoirs/index.html", \
29 Context ({"articles": articles,
30 "events": events,
31 "caldav_url": configuration['calendrier_publique'],
32 "chercheurs":chercheurs,
33 }), \
34 context_instance = RequestContext(request))
35
36 def recherche (request):
37 q = request.GET.get("q", "")
38 page = int(request.GET.get("page", 0))
39
40 r = cherche (page, q)
41
42 return render_to_response ("savoirs/recherche.html", \
43 Context ({'q': q,
44 'page': page,
45 'data': r}), \
46 context_instance = RequestContext(request))
47
48 def avancee (request):
49 type = request.GET.get("type", "")
50 page = int(request.GET.get("page", 0))
51
52 r = {'results': [], 'last_page': 0, 'more_link': ''}
53
54 q = request.GET.get("google-q", "")
55 f = RechercheAvancee ()
56
57 if type == 'google':
58 r = cherche (page, q, type)
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)
73
74 return render_to_response ("savoirs/avancee.html", \
75 Context ({'type': type,
76 'page': page,
77 'data': r,
78 'form': f,
79 'q': q}),
80 context_instance = RequestContext(request))
81
82 def conseils (request):
83 return render_to_response ("savoirs/conseils.html", \
84 Context (), \
85 context_instance = RequestContext(request))
86
87 def a_propos (request):
88 return render_to_response ("savoirs/a-propos.html", \
89 Context ({'count': len(backend_config.RESOURCES)}), \
90 context_instance = RequestContext(request))
91
92 def informations (request):
93 s = sep.SEP()
94 t = s.logs()
95 resources = copy.deepcopy (backend_config.RESOURCES)
96 for k in t.keys ():
97 try:
98 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
99 except: pass
100 return render_to_response ("savoirs/informations.html", \
101 Context ({'r': resources}), \
102 context_instance = RequestContext(request))
103
104 def nous_contacter (request):
105 return render_to_response ("savoirs/contact.html", \
106 Context (), \
107 context_instance = RequestContext(request))
108
109
110 def 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
116 def 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
130 def 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
137 def 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
181 def 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
188 @login_required
189 def json_get (request):
190 uri = request.GET.get ("uri")
191 if uri:
192 s = sep.SEP ()
193 res = s.search ({'uri': uri.encode("utf-8")})
194 r = s.get (res)
195
196 return HttpResponse(simplejson.dumps(r[0]),
197 mimetype='application/json')
198
199 @login_required
200 def json_set (request):
201 data = request.POST.get("data")
202 if data:
203 r = simplejson.loads(data)
204 s = sep.SEP ()
205 s.add (r)
206 return HttpResponse(simplejson.dumps("OK"),
207 mimetype='application/json')