e837a3215b22279fce7e1a27d22bfe170af44823
[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
12 def index (request):
13 delta = datetime.timedelta (days = 90)
14 oldest = datetime.date.today () - delta
15 articles = Actualite.objects.filter (visible = '1', date__gt = oldest)
16 articles = articles[0:configuration['accueil_actualite']]
17 return render_to_response ("savoirs/index.html", \
18 Context ({"articles": articles}), \
19 context_instance = RequestContext(request))
20
21 def recherche (request):
22 q = request.GET.get("q", "")
23 page = int(request.GET.get("page", 0))
24
25 r = cherche (page, q)
26
27 return render_to_response ("savoirs/recherche.html", \
28 Context ({'q': q,
29 'page': page,
30 'data': r}), \
31 context_instance = RequestContext(request))
32
33 def avancee (request):
34 type = request.GET.get("type", "")
35 page = int(request.GET.get("page", 0))
36
37 q = request.GET.get("google-q", "")
38
39 if type == 'google':
40 r = cherche (page, q, type)
41
42 return render_to_response ("savoirs/avancee.html", \
43 Context ({'type': type,
44 'page': page,
45 'data': r,
46 'q': q}),
47 context_instance = RequestContext(request))
48
49 def conseils (request):
50 return render_to_response ("savoirs/conseils.html", \
51 Context (), \
52 context_instance = RequestContext(request))
53
54 def a_propos (request):
55 return render_to_response ("savoirs/a-propos.html", \
56 Context (), \
57 context_instance = RequestContext(request))
58
59 def nous_contacter (request):
60 return render_to_response ("savoirs/contact.html", \
61 Context (), \
62 context_instance = RequestContext(request))
63
64 @login_required
65 def json_get (request):
66 uri = request.GET.get ("uri")
67 if uri:
68 s = SEP ()
69 res = s.search ({'uri': uri.encode("utf-8")})
70 if len (res) > 0:
71 r = s.get (res[0])
72
73 return HttpResponse(simplejson.dumps(r),
74 mimetype='application/json')
75
76 @login_required
77 def json_set (request):
78 data = request.POST.get("data")
79 if data:
80 r = simplejson.loads(data)
81 s = SEP ()
82 s.add (r)
83 return HttpResponse(simplejson.dumps("OK"),
84 mimetype='application/json')