ferme #503, rss agenda et intégration frontend
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
CommitLineData
d15017b2 1# -*- encoding: utf-8 -*-
92c7413b
CR
2import datetime, simplejson, copy, vobject
3
d15017b2
CR
4from django.shortcuts import render_to_response
5from django.template import Context, RequestContext
92c7413b 6from django.http import HttpResponse, HttpResponseRedirect
5ecd5424 7from django.contrib.auth.decorators import login_required
92c7413b 8from django.core.urlresolvers import reverse
e3c3296e 9from django import forms
92c7413b 10from lib.recherche import cherche, google_search
c08a8cb8 11from lib import sep
b7a741ad 12from lib.calendrier import evenements, evenement_info, combine
ecc62927 13from savoirs.globals import configuration
91ff43e9 14import backend_config
92c7413b
CR
15from forms import *
16from models import *
588d6b93 17from chercheurs.models import Chercheur
bab65667 18from sitotheque.models import Site
92c7413b 19
d15017b2
CR
20
21def index (request):
22 delta = datetime.timedelta (days = 90)
23 oldest = datetime.date.today () - delta
24 articles = Actualite.objects.filter (visible = '1', date__gt = oldest)
25 articles = articles[0:configuration['accueil_actualite']]
6fed87c2 26 try:
27 events = evenements()[0:configuration['accueil_evenement']]
28 except:
29 events = []
da091176 30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
5ecd5424 32 return render_to_response ("savoirs/index.html", \
92c7413b
CR
33 Context ({"articles": articles,
34 "events": events,
588d6b93 35 "caldav_url": configuration['calendrier_publique'],
36 "chercheurs":chercheurs,
6e001443 37 "sites":sites,
588d6b93 38 }), \
d15017b2
CR
39 context_instance = RequestContext(request))
40
41def recherche (request):
d15017b2
CR
42 q = request.GET.get("q", "")
43 page = int(request.GET.get("page", 0))
44
40a5ebfb 45 r = cherche (page, q)
d15017b2 46
5ecd5424 47 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
48 Context ({'q': q,
49 'page': page,
50 'data': r}), \
51 context_instance = RequestContext(request))
52
a008c7e5
CR
53def avancee (request):
54 type = request.GET.get("type", "")
55 page = int(request.GET.get("page", 0))
56
cebcab65
CR
57 r = {'results': [], 'last_page': 0, 'more_link': ''}
58
a008c7e5 59 q = request.GET.get("google-q", "")
544b4522 60 f = RechercheAvancee ()
a008c7e5
CR
61
62 if type == 'google':
63 r = cherche (page, q, type)
544b4522
CR
64 q = {'q': q}
65 elif type == 'avancee':
66 f = RechercheAvancee (request.GET)
67 if f.is_valid():
68 q = {}
69 for k in ['creator', 'title', 'description', 'subject']:
70 tmp = f.cleaned_data[k].strip()
71 if len (tmp) > 0:
72 q[k] = tmp
73 q['operator'] = '|'
74 if f.cleaned_data['operator'] == 'and':
75 q['operator'] = "&"
76
77 r = cherche (page, q, type)
a008c7e5
CR
78
79 return render_to_response ("savoirs/avancee.html", \
80 Context ({'type': type,
81 'page': page,
82 'data': r,
544b4522 83 'form': f,
a008c7e5
CR
84 'q': q}),
85 context_instance = RequestContext(request))
86
d15017b2 87def conseils (request):
5ecd5424 88 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
89 Context (), \
90 context_instance = RequestContext(request))
91
92def a_propos (request):
5ecd5424 93 return render_to_response ("savoirs/a-propos.html", \
91ff43e9 94 Context ({'count': len(backend_config.RESOURCES)}), \
2d10d74b
CR
95 context_instance = RequestContext(request))
96
97def informations (request):
c08a8cb8 98 s = sep.SEP()
91ff43e9 99 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 100
72b466ce 101 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 102 for l in logs:
103 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
104
2d10d74b 105 return render_to_response ("savoirs/informations.html", \
92c7413b 106 Context ({'r': resources}), \
d15017b2
CR
107 context_instance = RequestContext(request))
108
109def nous_contacter (request):
5ecd5424 110 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
111 Context (), \
112 context_instance = RequestContext(request))
5ecd5424 113
92c7413b
CR
114
115def evenement(request, id):
116 event = evenement_info(id)
117 return render_to_response ("savoirs/evenement.html", \
118 Context ({'event': event.instance.vevent}), \
119 context_instance = RequestContext(request))
120
121def evenement_ajout(request):
122 template = "savoirs/evenement_ajout.html"
123 if request.method == "POST":
124 form = EvenementForm(request.POST)
125 if form.is_valid():
126 form.save()
127 template = "savoirs/evenement_confirmation.html"
128 else:
129 form = EvenementForm()
130 return render_to_response (template, \
131 Context ({'form': form}), \
132 context_instance = RequestContext(request))
133
134@login_required
135def evenement_moderation(request):
136 events = Evenement.objects.filter(approuve = False)
137 return render_to_response ("savoirs/evenement_moderation.html", \
138 Context ({'events': events}), \
139 context_instance = RequestContext(request))
140
141@login_required
142def evenement_accepter(request, pk):
143 e = Evenement.objects.get(pk = pk)
92c7413b 144 e.save()
92c7413b
CR
145 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
146
147@login_required
148def evenement_refuser(request, pk):
149 evenement = Evenement.objects.get(pk = pk)
150 evenement.actif = False
151 evenement.save()
152 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
153
154
5ecd5424
CR
155@login_required
156def json_get (request):
157 uri = request.GET.get ("uri")
158 if uri:
c08a8cb8 159 s = sep.SEP ()
5ecd5424 160 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 161 r = s.get (res)
5ecd5424 162
c08a8cb8 163 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
164 mimetype='application/json')
165
166@login_required
167def json_set (request):
168 data = request.POST.get("data")
169 if data:
170 r = simplejson.loads(data)
c08a8cb8 171 s = sep.SEP ()
5ecd5424
CR
172 s.add (r)
173 return HttpResponse(simplejson.dumps("OK"),
174 mimetype='application/json')