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