a5bc98ffd0234f9b4dec8a07dd0da8b126c64328
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage_django / savoirs / views.py
1 # -*- encoding: utf-8 -*-
2 import datetime, simplejson
3 from django.shortcuts import render_to_response
4 from django.template import Context, RequestContext
5 from django.http import HttpResponse
6 from django.contrib.auth.decorators import login_required
7 from models import Actualite
8 from savoirs import configuration
9 from recherche import cherche, google_search
10 from auf_savoirs_en_partage_backend.sep.io import SEP
11 from forms import RechercheAvancee
12
13 def 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']]
18 return render_to_response ("savoirs/index.html", \
19 Context ({"articles": articles}), \
20 context_instance = RequestContext(request))
21
22 def recherche (request):
23 q = request.GET.get("q", "")
24 page = int(request.GET.get("page", 0))
25
26 r = cherche (page, q)
27
28 return render_to_response ("savoirs/recherche.html", \
29 Context ({'q': q,
30 'page': page,
31 'data': r}), \
32 context_instance = RequestContext(request))
33
34 def avancee (request):
35 type = request.GET.get("type", "")
36 page = int(request.GET.get("page", 0))
37
38 r = {'results': [], 'last_page': 0, 'more_link': ''}
39
40 q = request.GET.get("google-q", "")
41 f = RechercheAvancee ()
42
43 if type == 'google':
44 r = cherche (page, q, type)
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)
59
60 return render_to_response ("savoirs/avancee.html", \
61 Context ({'type': type,
62 'page': page,
63 'data': r,
64 'form': f,
65 'q': q}),
66 context_instance = RequestContext(request))
67
68 def conseils (request):
69 return render_to_response ("savoirs/conseils.html", \
70 Context (), \
71 context_instance = RequestContext(request))
72
73 def a_propos (request):
74 return render_to_response ("savoirs/a-propos.html", \
75 Context (), \
76 context_instance = RequestContext(request))
77
78 def nous_contacter (request):
79 return render_to_response ("savoirs/contact.html", \
80 Context (), \
81 context_instance = RequestContext(request))
82
83 @login_required
84 def 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
96 def json_set (request):
97 data = request.POST.get("data")
98 if data:
99 r = simplejson.loads(data)
100 print r
101 s = SEP ()
102 s.add (r)
103 return HttpResponse(simplejson.dumps("OK"),
104 mimetype='application/json')