Fix temporaire
[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 ##TEMP
38 r = {'results': [], 'last_page': 0, 'more_link': ''}
39
40 q = request.GET.get("google-q", "")
41
42 if type == 'google':
43 r = cherche (page, q, type)
44
45 return render_to_response ("savoirs/avancee.html", \
46 Context ({'type': type,
47 'page': page,
48 'data': r,
49 'q': q}),
50 context_instance = RequestContext(request))
51
52 def conseils (request):
53 return render_to_response ("savoirs/conseils.html", \
54 Context (), \
55 context_instance = RequestContext(request))
56
57 def a_propos (request):
58 return render_to_response ("savoirs/a-propos.html", \
59 Context (), \
60 context_instance = RequestContext(request))
61
62 def nous_contacter (request):
63 return render_to_response ("savoirs/contact.html", \
64 Context (), \
65 context_instance = RequestContext(request))
66
67 @login_required
68 def json_get (request):
69 uri = request.GET.get ("uri")
70 if uri:
71 s = SEP ()
72 res = s.search ({'uri': uri.encode("utf-8")})
73 if len (res) > 0:
74 r = s.get (res[0])
75
76 return HttpResponse(simplejson.dumps(r),
77 mimetype='application/json')
78
79 @login_required
80 def json_set (request):
81 data = request.POST.get("data")
82 if data:
83 r = simplejson.loads(data)
84 s = SEP ()
85 s.add (r)
86 return HttpResponse(simplejson.dumps("OK"),
87 mimetype='application/json')