Correction de texte
[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
2d10d74b 12from auf_savoirs_en_partage_backend.conf import RESOURCES
d15017b2
CR
13
14def 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']]
5ecd5424 19 return render_to_response ("savoirs/index.html", \
d15017b2
CR
20 Context ({"articles": articles}), \
21 context_instance = RequestContext(request))
22
23def recherche (request):
d15017b2
CR
24 q = request.GET.get("q", "")
25 page = int(request.GET.get("page", 0))
26
40a5ebfb 27 r = cherche (page, q)
d15017b2 28
5ecd5424 29 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
30 Context ({'q': q,
31 'page': page,
32 'data': r}), \
33 context_instance = RequestContext(request))
34
a008c7e5
CR
35def avancee (request):
36 type = request.GET.get("type", "")
37 page = int(request.GET.get("page", 0))
38
cebcab65
CR
39 r = {'results': [], 'last_page': 0, 'more_link': ''}
40
a008c7e5 41 q = request.GET.get("google-q", "")
544b4522 42 f = RechercheAvancee ()
a008c7e5
CR
43
44 if type == 'google':
45 r = cherche (page, q, type)
544b4522
CR
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)
a008c7e5
CR
60
61 return render_to_response ("savoirs/avancee.html", \
62 Context ({'type': type,
63 'page': page,
64 'data': r,
544b4522 65 'form': f,
a008c7e5
CR
66 'q': q}),
67 context_instance = RequestContext(request))
68
d15017b2 69def conseils (request):
5ecd5424 70 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
71 Context (), \
72 context_instance = RequestContext(request))
73
74def a_propos (request):
5ecd5424 75 return render_to_response ("savoirs/a-propos.html", \
2d10d74b
CR
76 Context ({'count': len(RESOURCES)}), \
77 context_instance = RequestContext(request))
78
79def informations (request):
80 return render_to_response ("savoirs/informations.html", \
81 Context ({'r': RESOURCES}), \
d15017b2
CR
82 context_instance = RequestContext(request))
83
84def nous_contacter (request):
5ecd5424 85 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
86 Context (), \
87 context_instance = RequestContext(request))
5ecd5424
CR
88
89@login_required
90def 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
102def json_set (request):
103 data = request.POST.get("data")
104 if data:
105 r = simplejson.loads(data)
f4db9f51 106 print r
5ecd5424
CR
107 s = SEP ()
108 s.add (r)
109 return HttpResponse(simplejson.dumps("OK"),
110 mimetype='application/json')