Ajout de la liste des sources, closes #187
[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 from auf_savoirs_en_partage_backend.conf import RESOURCES
13
14 def index (request):
15 delta = datetime.timedelta (days = 90)
16 oldest = datetime.date.today () - delta
17 articles = Actualite.objects.filter (visible = '1', date__gt = oldest)
18 articles = articles[0:configuration['accueil_actualite']]
19 return render_to_response ("savoirs/index.html", \
20 Context ({"articles": articles}), \
21 context_instance = RequestContext(request))
22
23 def recherche (request):
24 q = request.GET.get("q", "")
25 page = int(request.GET.get("page", 0))
26
27 r = cherche (page, q)
28
29 return render_to_response ("savoirs/recherche.html", \
30 Context ({'q': q,
31 'page': page,
32 'data': r}), \
33 context_instance = RequestContext(request))
34
35 def avancee (request):
36 type = request.GET.get("type", "")
37 page = int(request.GET.get("page", 0))
38
39 r = {'results': [], 'last_page': 0, 'more_link': ''}
40
41 q = request.GET.get("google-q", "")
42 f = RechercheAvancee ()
43
44 if type == 'google':
45 r = cherche (page, q, type)
46 q = {'q': q}
47 elif type == 'avancee':
48 f = RechercheAvancee (request.GET)
49 if f.is_valid():
50 q = {}
51 for k in ['creator', 'title', 'description', 'subject']:
52 tmp = f.cleaned_data[k].strip()
53 if len (tmp) > 0:
54 q[k] = tmp
55 q['operator'] = '|'
56 if f.cleaned_data['operator'] == 'and':
57 q['operator'] = "&"
58
59 r = cherche (page, q, type)
60
61 return render_to_response ("savoirs/avancee.html", \
62 Context ({'type': type,
63 'page': page,
64 'data': r,
65 'form': f,
66 'q': q}),
67 context_instance = RequestContext(request))
68
69 def conseils (request):
70 return render_to_response ("savoirs/conseils.html", \
71 Context (), \
72 context_instance = RequestContext(request))
73
74 def a_propos (request):
75 return render_to_response ("savoirs/a-propos.html", \
76 Context ({'count': len(RESOURCES)}), \
77 context_instance = RequestContext(request))
78
79 def informations (request):
80 return render_to_response ("savoirs/informations.html", \
81 Context ({'r': RESOURCES}), \
82 context_instance = RequestContext(request))
83
84 def nous_contacter (request):
85 return render_to_response ("savoirs/contact.html", \
86 Context (), \
87 context_instance = RequestContext(request))
88
89 @login_required
90 def json_get (request):
91 uri = request.GET.get ("uri")
92 if uri:
93 s = SEP ()
94 res = s.search ({'uri': uri.encode("utf-8")})
95 if len (res) > 0:
96 r = s.get (res[0])
97
98 return HttpResponse(simplejson.dumps(r),
99 mimetype='application/json')
100
101 @login_required
102 def json_set (request):
103 data = request.POST.get("data")
104 if data:
105 r = simplejson.loads(data)
106 print r
107 s = SEP ()
108 s.add (r)
109 return HttpResponse(simplejson.dumps("OK"),
110 mimetype='application/json')