01b5b00a26f78be58e0d0e87c7b196cb071c60ee
[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 evenements = Evenement.objects.filter(approuve=1).search(query)
58 search_regexp = build_search_regexp(query)
59 return render_to_response(
60 "savoirs/recherche.html",
61 dict(q=query, ressources=ressources[:5], actualites=actualites[:5],
62 evenements=evenements[:5], total_evenements=len(evenements),
63 total_ressources=len(ressources), total_actualites=len(actualites),
64 search_regexp=search_regexp),
65 context_instance = RequestContext(request)
66 )
67
68 def avancee (request):
69 type = request.GET.get("type", "")
70 page = int(request.GET.get("page", 0))
71
72 r = {'results': [], 'last_page': 0, 'more_link': ''}
73
74 q = request.GET.get("google-q", "")
75 f = RechercheAvancee ()
76
77 if type == 'google':
78 r = cherche (page, q, type)
79 q = {'q': q}
80 elif type == 'avancee':
81 f = RechercheAvancee (request.GET)
82 if f.is_valid():
83 q = {}
84 for k in ['creator', 'title', 'description', 'subject']:
85 tmp = f.cleaned_data[k].strip()
86 if len (tmp) > 0:
87 q[k] = tmp
88 q['operator'] = '|'
89 if f.cleaned_data['operator'] == 'and':
90 q['operator'] = "&"
91
92 r = cherche (page, q, type)
93
94 return render_to_response ("savoirs/avancee.html", \
95 Context ({'type': type,
96 'page': page,
97 'data': r,
98 'form': f,
99 'q': q}),
100 context_instance = RequestContext(request))
101
102 def conseils (request):
103 return render_to_response ("savoirs/conseils.html", \
104 Context (), \
105 context_instance = RequestContext(request))
106
107 # ressources
108 def ressource_index(request):
109 search_form = RecordSearchForm(request.GET)
110 ressources = search_form.get_query_set()
111 nb_resultats = len(ressources)
112 search_regexp = search_form.get_search_regexp()
113 return render_to_response(
114 "savoirs/ressource_index.html",
115 {'search_form': search_form, 'ressources': ressources,
116 'nb_resultats': len(ressources), 'search_regexp': search_regexp},
117 context_instance = RequestContext(request)
118 )
119
120 def ressource_retrieve(request, id):
121 """Notice OAI de la ressource"""
122 ressource = Record.objects.get(id=id)
123 variables = { 'ressource': ressource,
124 }
125 return render_to_response ("savoirs/ressource_retrieve.html", \
126 Context (variables),
127 context_instance = RequestContext(request))
128
129 def informations (request):
130 s = sep.SEP()
131 resources = copy.deepcopy (backend_config.RESOURCES)
132
133 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
134 for l in logs:
135 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
136
137 return render_to_response ("savoirs/informations.html", \
138 Context ({'r': resources}), \
139 context_instance = RequestContext(request))
140
141 # actualités
142 def actualite_index(request):
143 search_form = ActualiteSearchForm(request.GET)
144 actualites = search_form.get_query_set()
145 search_regexp = search_form.get_search_regexp()
146 return render_to_response("savoirs/actualite_index.html",
147 dict(actualites=actualites,
148 search_form=search_form,
149 search_regexp=search_regexp,
150 nb_resultats=len(actualites)),
151 context_instance = RequestContext(request))
152
153 # agenda
154 def evenement_index(request):
155 search_form = EvenementSearchForm(request.GET)
156 evenements = search_form.get_query_set()
157 search_regexp = search_form.get_search_regexp()
158 return render_to_response("savoirs/evenement_index.html",
159 dict(evenements=evenements,
160 search_form=search_form,
161 search_regexp=search_regexp,
162 nb_resultats=len(evenements)),
163 context_instance=RequestContext(request))
164
165 def evenement(request, id):
166 evenement = get_object_or_404(Evenement, pk=id)
167 return render_to_response("savoirs/evenement.html",
168 dict(evenement=evenement),
169 context_instance=RequestContext(request))
170
171 def evenement_ajout(request):
172 template = "savoirs/evenement_ajout.html"
173 if request.method == "POST":
174 form = EvenementForm(request.POST)
175 if form.is_valid():
176 form.save()
177 template = "savoirs/evenement_confirmation.html"
178 else:
179 form = EvenementForm()
180 return render_to_response (template, \
181 Context ({'form': form}), \
182 context_instance = RequestContext(request))
183
184 @login_required
185 def evenement_moderation(request):
186 events = Evenement.objects.filter(approuve = False)
187 return render_to_response ("savoirs/evenement_moderation.html", \
188 Context ({'events': events}), \
189 context_instance = RequestContext(request))
190
191 @login_required
192 def evenement_accepter(request, pk):
193 e = Evenement.objects.get(pk = pk)
194 e.save()
195 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
196
197 @login_required
198 def evenement_refuser(request, pk):
199 evenement = Evenement.objects.get(pk = pk)
200 evenement.actif = False
201 evenement.save()
202 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
203
204
205 @login_required
206 def json_get (request):
207 uri = request.GET.get ("uri")
208 if uri:
209 s = sep.SEP ()
210 res = s.search ({'uri': uri.encode("utf-8")})
211 r = s.get (res)
212
213 return HttpResponse(simplejson.dumps(r[0]),
214 mimetype='application/json')
215
216 @login_required
217 def json_set (request):
218 data = request.POST.get("data")
219 if data:
220 r = simplejson.loads(data)
221 s = sep.SEP ()
222 s.add (r)
223 return HttpResponse(simplejson.dumps("OK"),
224 mimetype='application/json')