Corrections, ajout recherche avancée
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage_django / savoirs / views.py
CommitLineData
d15017b2 1# -*- encoding: utf-8 -*-
5ecd5424 2import datetime, simplejson
d15017b2
CR
3from django.shortcuts import render_to_response
4from django.template import Context, RequestContext
5ecd5424
CR
5from django.http import HttpResponse
6from django.contrib.auth.decorators import login_required
d15017b2
CR
7from models import Actualite
8from savoirs import configuration
a008c7e5 9from recherche import cherche, google_search
5ecd5424 10from auf_savoirs_en_partage_backend.sep.io import SEP
544b4522 11from forms import RechercheAvancee
d15017b2
CR
12
13def index (request):
14 delta = datetime.timedelta (days = 90)
15 oldest = datetime.date.today () - delta
16 articles = Actualite.objects.filter (visible = '1', date__gt = oldest)
17 articles = articles[0:configuration['accueil_actualite']]
5ecd5424 18 return render_to_response ("savoirs/index.html", \
d15017b2
CR
19 Context ({"articles": articles}), \
20 context_instance = RequestContext(request))
21
22def recherche (request):
d15017b2
CR
23 q = request.GET.get("q", "")
24 page = int(request.GET.get("page", 0))
25
40a5ebfb 26 r = cherche (page, q)
d15017b2 27
5ecd5424 28 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
29 Context ({'q': q,
30 'page': page,
31 'data': r}), \
32 context_instance = RequestContext(request))
33
a008c7e5
CR
34def avancee (request):
35 type = request.GET.get("type", "")
36 page = int(request.GET.get("page", 0))
37
cebcab65
CR
38 r = {'results': [], 'last_page': 0, 'more_link': ''}
39
a008c7e5 40 q = request.GET.get("google-q", "")
544b4522 41 f = RechercheAvancee ()
a008c7e5
CR
42
43 if type == 'google':
44 r = cherche (page, q, type)
544b4522
CR
45 q = {'q': q}
46 elif type == 'avancee':
47 f = RechercheAvancee (request.GET)
48 if f.is_valid():
49 q = {}
50 for k in ['creator', 'title', 'description', 'subject']:
51 tmp = f.cleaned_data[k].strip()
52 if len (tmp) > 0:
53 q[k] = tmp
54 q['operator'] = '|'
55 if f.cleaned_data['operator'] == 'and':
56 q['operator'] = "&"
57
58 r = cherche (page, q, type)
a008c7e5
CR
59
60 return render_to_response ("savoirs/avancee.html", \
61 Context ({'type': type,
62 'page': page,
63 'data': r,
544b4522 64 'form': f,
a008c7e5
CR
65 'q': q}),
66 context_instance = RequestContext(request))
67
d15017b2 68def conseils (request):
5ecd5424 69 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
70 Context (), \
71 context_instance = RequestContext(request))
72
73def a_propos (request):
5ecd5424 74 return render_to_response ("savoirs/a-propos.html", \
d15017b2
CR
75 Context (), \
76 context_instance = RequestContext(request))
77
78def nous_contacter (request):
5ecd5424 79 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
80 Context (), \
81 context_instance = RequestContext(request))
5ecd5424
CR
82
83@login_required
84def json_get (request):
85 uri = request.GET.get ("uri")
86 if uri:
87 s = SEP ()
88 res = s.search ({'uri': uri.encode("utf-8")})
89 if len (res) > 0:
90 r = s.get (res[0])
91
92 return HttpResponse(simplejson.dumps(r),
93 mimetype='application/json')
94
95@login_required
96def json_set (request):
97 data = request.POST.get("data")
98 if data:
99 r = simplejson.loads(data)
100 s = SEP ()
101 s.add (r)
102 return HttpResponse(simplejson.dumps("OK"),
103 mimetype='application/json')