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