Merge branch 'master' of gitosis@git.auf:auf_savoirs_en_partage_django
[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
b7a741ad 12from lib.calendrier import evenements, evenement_info, 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
f8c16b3d 20# sous-menu gauche
d15017b2 21def index (request):
f8c16b3d 22 """Page d'accueil"""
d15017b2
CR
23 delta = datetime.timedelta (days = 90)
24 oldest = datetime.date.today () - delta
c4c5de1b 25 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
26 actualites = actualites[0:configuration['accueil_actualite']]
6fed87c2 27 try:
65fae967 28 erreur_caldav = False
6fed87c2 29 events = evenements()[0:configuration['accueil_evenement']]
30 except:
65fae967 31 erreur_caldav = u"Problème de connexion à l'agenda"
6fed87c2 32 events = []
91e0aefb 33 ressources = Record.objects.all().order_by('?')[:configuration['accueil_ressource']]
da091176 34 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 35 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
5ecd5424 36 return render_to_response ("savoirs/index.html", \
c4c5de1b 37 Context ({"actualites": actualites,
92c7413b 38 "events": events,
65fae967 39 "erreur_caldav": erreur_caldav,
588d6b93 40 "caldav_url": configuration['calendrier_publique'],
91e0aefb 41 "ressources":ressources,
588d6b93 42 "chercheurs":chercheurs,
6e001443 43 "sites":sites,
588d6b93 44 }), \
d15017b2
CR
45 context_instance = RequestContext(request))
46
f8c16b3d 47# sous-menu droite
48def a_propos (request):
49 return render_to_response ("savoirs/a-propos.html", \
50 Context ({'count': len(backend_config.RESOURCES)}), \
51 context_instance = RequestContext(request))
52
53def nous_contacter (request):
54 return render_to_response ("savoirs/contact.html", \
55 Context (), \
56 context_instance = RequestContext(request))
57
58# recherche
d15017b2 59def recherche (request):
d15017b2
CR
60 q = request.GET.get("q", "")
61 page = int(request.GET.get("page", 0))
62
40a5ebfb 63 r = cherche (page, q)
d15017b2 64
5ecd5424 65 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
66 Context ({'q': q,
67 'page': page,
68 'data': r}), \
69 context_instance = RequestContext(request))
70
a008c7e5
CR
71def avancee (request):
72 type = request.GET.get("type", "")
73 page = int(request.GET.get("page", 0))
74
cebcab65
CR
75 r = {'results': [], 'last_page': 0, 'more_link': ''}
76
a008c7e5 77 q = request.GET.get("google-q", "")
544b4522 78 f = RechercheAvancee ()
a008c7e5
CR
79
80 if type == 'google':
81 r = cherche (page, q, type)
544b4522
CR
82 q = {'q': q}
83 elif type == 'avancee':
84 f = RechercheAvancee (request.GET)
85 if f.is_valid():
86 q = {}
87 for k in ['creator', 'title', 'description', 'subject']:
88 tmp = f.cleaned_data[k].strip()
89 if len (tmp) > 0:
90 q[k] = tmp
91 q['operator'] = '|'
92 if f.cleaned_data['operator'] == 'and':
93 q['operator'] = "&"
94
95 r = cherche (page, q, type)
a008c7e5
CR
96
97 return render_to_response ("savoirs/avancee.html", \
98 Context ({'type': type,
99 'page': page,
100 'data': r,
544b4522 101 'form': f,
a008c7e5
CR
102 'q': q}),
103 context_instance = RequestContext(request))
104
d15017b2 105def conseils (request):
5ecd5424 106 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
107 Context (), \
108 context_instance = RequestContext(request))
109
f8c16b3d 110# ressources
111def ressource_index(request):
112 return render_to_response ("savoirs/ressource_index.html", \
113 Context (), \
2d10d74b 114 context_instance = RequestContext(request))
91e0aefb 115
116def ressource_retrieve(request, id):
117 """Notice OAI de la ressource"""
118 ressource = Record.objects.get(id=id)
119 variables = { 'ressource': ressource,
120 }
121 return render_to_response ("savoirs/ressource_retrieve.html", \
122 Context (variables),
123 context_instance = RequestContext(request))
f8c16b3d 124
2d10d74b 125def informations (request):
c08a8cb8 126 s = sep.SEP()
91ff43e9 127 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 128
72b466ce 129 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 130 for l in logs:
131 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
132
2d10d74b 133 return render_to_response ("savoirs/informations.html", \
92c7413b 134 Context ({'r': resources}), \
d15017b2
CR
135 context_instance = RequestContext(request))
136
f8c16b3d 137# actualités
138def actualite_index(request):
add2851c
AJ
139 delta = datetime.timedelta (days = 90)
140 oldest = datetime.date.today () - delta
141 actualites = Actualite.objects.filter (visible = '1', date__gt = oldest)
f8c16b3d 142 return render_to_response ("savoirs/actualite_index.html", \
add2851c 143 Context ({'actualites': actualites}), \
d15017b2 144 context_instance = RequestContext(request))
5ecd5424 145
f8c16b3d 146# agenda
147def evenement_index(request):
148 return render_to_response ("savoirs/evenement_index.html", \
149 Context (), \
150 context_instance = RequestContext(request))
92c7413b
CR
151
152def evenement(request, id):
153 event = evenement_info(id)
154 return render_to_response ("savoirs/evenement.html", \
155 Context ({'event': event.instance.vevent}), \
156 context_instance = RequestContext(request))
157
158def evenement_ajout(request):
159 template = "savoirs/evenement_ajout.html"
160 if request.method == "POST":
161 form = EvenementForm(request.POST)
162 if form.is_valid():
163 form.save()
164 template = "savoirs/evenement_confirmation.html"
165 else:
166 form = EvenementForm()
167 return render_to_response (template, \
168 Context ({'form': form}), \
169 context_instance = RequestContext(request))
170
171@login_required
172def evenement_moderation(request):
173 events = Evenement.objects.filter(approuve = False)
174 return render_to_response ("savoirs/evenement_moderation.html", \
175 Context ({'events': events}), \
176 context_instance = RequestContext(request))
177
178@login_required
179def evenement_accepter(request, pk):
180 e = Evenement.objects.get(pk = pk)
92c7413b 181 e.save()
92c7413b
CR
182 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
183
184@login_required
185def evenement_refuser(request, pk):
186 evenement = Evenement.objects.get(pk = pk)
187 evenement.actif = False
188 evenement.save()
189 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
190
191
5ecd5424
CR
192@login_required
193def json_get (request):
194 uri = request.GET.get ("uri")
195 if uri:
c08a8cb8 196 s = sep.SEP ()
5ecd5424 197 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 198 r = s.get (res)
5ecd5424 199
c08a8cb8 200 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
201 mimetype='application/json')
202
203@login_required
204def json_set (request):
205 data = request.POST.get("data")
206 if data:
207 r = simplejson.loads(data)
c08a8cb8 208 s = sep.SEP ()
5ecd5424
CR
209 s.add (r)
210 return HttpResponse(simplejson.dumps("OK"),
211 mimetype='application/json')