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