Merge branch 'master' of ssh://gitosis@git.auf/auf_savoirs_en_partage_django
[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:
65fae967 27 erreur_caldav = False
6fed87c2 28 events = evenements()[0:configuration['accueil_evenement']]
29 except:
65fae967 30 erreur_caldav = u"Problème de connexion à l'agenda"
6fed87c2 31 events = []
da091176 32 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
6e001443 33 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
5ecd5424 34 return render_to_response ("savoirs/index.html", \
92c7413b
CR
35 Context ({"articles": articles,
36 "events": events,
65fae967 37 "erreur_caldav": erreur_caldav,
588d6b93 38 "caldav_url": configuration['calendrier_publique'],
39 "chercheurs":chercheurs,
6e001443 40 "sites":sites,
588d6b93 41 }), \
d15017b2
CR
42 context_instance = RequestContext(request))
43
44def recherche (request):
d15017b2
CR
45 q = request.GET.get("q", "")
46 page = int(request.GET.get("page", 0))
47
40a5ebfb 48 r = cherche (page, q)
d15017b2 49
5ecd5424 50 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
51 Context ({'q': q,
52 'page': page,
53 'data': r}), \
54 context_instance = RequestContext(request))
55
a008c7e5
CR
56def avancee (request):
57 type = request.GET.get("type", "")
58 page = int(request.GET.get("page", 0))
59
cebcab65
CR
60 r = {'results': [], 'last_page': 0, 'more_link': ''}
61
a008c7e5 62 q = request.GET.get("google-q", "")
544b4522 63 f = RechercheAvancee ()
a008c7e5
CR
64
65 if type == 'google':
66 r = cherche (page, q, type)
544b4522
CR
67 q = {'q': q}
68 elif type == 'avancee':
69 f = RechercheAvancee (request.GET)
70 if f.is_valid():
71 q = {}
72 for k in ['creator', 'title', 'description', 'subject']:
73 tmp = f.cleaned_data[k].strip()
74 if len (tmp) > 0:
75 q[k] = tmp
76 q['operator'] = '|'
77 if f.cleaned_data['operator'] == 'and':
78 q['operator'] = "&"
79
80 r = cherche (page, q, type)
a008c7e5
CR
81
82 return render_to_response ("savoirs/avancee.html", \
83 Context ({'type': type,
84 'page': page,
85 'data': r,
544b4522 86 'form': f,
a008c7e5
CR
87 'q': q}),
88 context_instance = RequestContext(request))
89
d15017b2 90def conseils (request):
5ecd5424 91 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
92 Context (), \
93 context_instance = RequestContext(request))
94
95def a_propos (request):
5ecd5424 96 return render_to_response ("savoirs/a-propos.html", \
91ff43e9 97 Context ({'count': len(backend_config.RESOURCES)}), \
2d10d74b
CR
98 context_instance = RequestContext(request))
99
100def informations (request):
c08a8cb8 101 s = sep.SEP()
91ff43e9 102 resources = copy.deepcopy (backend_config.RESOURCES)
80469cb6 103
72b466ce 104 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
80469cb6 105 for l in logs:
106 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
107
2d10d74b 108 return render_to_response ("savoirs/informations.html", \
92c7413b 109 Context ({'r': resources}), \
d15017b2
CR
110 context_instance = RequestContext(request))
111
112def nous_contacter (request):
5ecd5424 113 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
114 Context (), \
115 context_instance = RequestContext(request))
5ecd5424 116
92c7413b
CR
117
118def evenement(request, id):
119 event = evenement_info(id)
120 return render_to_response ("savoirs/evenement.html", \
121 Context ({'event': event.instance.vevent}), \
122 context_instance = RequestContext(request))
123
124def evenement_ajout(request):
125 template = "savoirs/evenement_ajout.html"
126 if request.method == "POST":
127 form = EvenementForm(request.POST)
128 if form.is_valid():
129 form.save()
130 template = "savoirs/evenement_confirmation.html"
131 else:
132 form = EvenementForm()
133 return render_to_response (template, \
134 Context ({'form': form}), \
135 context_instance = RequestContext(request))
136
137@login_required
138def evenement_moderation(request):
139 events = Evenement.objects.filter(approuve = False)
140 return render_to_response ("savoirs/evenement_moderation.html", \
141 Context ({'events': events}), \
142 context_instance = RequestContext(request))
143
144@login_required
145def evenement_accepter(request, pk):
146 e = Evenement.objects.get(pk = pk)
92c7413b 147 e.save()
92c7413b
CR
148 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
149
150@login_required
151def evenement_refuser(request, pk):
152 evenement = Evenement.objects.get(pk = pk)
153 evenement.actif = False
154 evenement.save()
155 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
156
157
5ecd5424
CR
158@login_required
159def json_get (request):
160 uri = request.GET.get ("uri")
161 if uri:
c08a8cb8 162 s = sep.SEP ()
5ecd5424 163 res = s.search ({'uri': uri.encode("utf-8")})
c08a8cb8 164 r = s.get (res)
5ecd5424 165
c08a8cb8 166 return HttpResponse(simplejson.dumps(r[0]),
5ecd5424
CR
167 mimetype='application/json')
168
169@login_required
170def json_set (request):
171 data = request.POST.get("data")
172 if data:
173 r = simplejson.loads(data)
c08a8cb8 174 s = sep.SEP ()
5ecd5424
CR
175 s.add (r)
176 return HttpResponse(simplejson.dumps("OK"),
177 mimetype='application/json')