Correctifs + harvest pour une seule source
[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
CR
8from django.core.urlresolvers import reverse
9
0cc5f772 10#import auf_savoirs_en_partage_backend as sep
92c7413b
CR
11
12from lib.recherche import cherche, google_search
13from lib.calendrier import evenements, evenement_info, evenement_publie, combine
d15017b2 14from savoirs import configuration
92c7413b
CR
15from forms import *
16from models import *
17
d15017b2
CR
18
19def index (request):
20 delta = datetime.timedelta (days = 90)
21 oldest = datetime.date.today () - delta
22 articles = Actualite.objects.filter (visible = '1', date__gt = oldest)
23 articles = articles[0:configuration['accueil_actualite']]
92c7413b 24 events = evenements()[0:configuration['accueil_evenement']]
5ecd5424 25 return render_to_response ("savoirs/index.html", \
92c7413b
CR
26 Context ({"articles": articles,
27 "events": events,
28 "caldav_url": configuration['calendrier_publique']}), \
d15017b2
CR
29 context_instance = RequestContext(request))
30
31def recherche (request):
d15017b2
CR
32 q = request.GET.get("q", "")
33 page = int(request.GET.get("page", 0))
34
40a5ebfb 35 r = cherche (page, q)
d15017b2 36
5ecd5424 37 return render_to_response ("savoirs/recherche.html", \
d15017b2
CR
38 Context ({'q': q,
39 'page': page,
40 'data': r}), \
41 context_instance = RequestContext(request))
42
a008c7e5
CR
43def avancee (request):
44 type = request.GET.get("type", "")
45 page = int(request.GET.get("page", 0))
46
cebcab65
CR
47 r = {'results': [], 'last_page': 0, 'more_link': ''}
48
a008c7e5 49 q = request.GET.get("google-q", "")
544b4522 50 f = RechercheAvancee ()
a008c7e5
CR
51
52 if type == 'google':
53 r = cherche (page, q, type)
544b4522
CR
54 q = {'q': q}
55 elif type == 'avancee':
56 f = RechercheAvancee (request.GET)
57 if f.is_valid():
58 q = {}
59 for k in ['creator', 'title', 'description', 'subject']:
60 tmp = f.cleaned_data[k].strip()
61 if len (tmp) > 0:
62 q[k] = tmp
63 q['operator'] = '|'
64 if f.cleaned_data['operator'] == 'and':
65 q['operator'] = "&"
66
67 r = cherche (page, q, type)
a008c7e5
CR
68
69 return render_to_response ("savoirs/avancee.html", \
70 Context ({'type': type,
71 'page': page,
72 'data': r,
544b4522 73 'form': f,
a008c7e5
CR
74 'q': q}),
75 context_instance = RequestContext(request))
76
d15017b2 77def conseils (request):
5ecd5424 78 return render_to_response ("savoirs/conseils.html", \
d15017b2
CR
79 Context (), \
80 context_instance = RequestContext(request))
81
82def a_propos (request):
5ecd5424 83 return render_to_response ("savoirs/a-propos.html", \
92c7413b 84 Context ({'count': len(sep.conf.RESOURCES)}), \
2d10d74b
CR
85 context_instance = RequestContext(request))
86
87def informations (request):
92c7413b
CR
88 s = sep.sep.io.SEP()
89 t = s.logs()
90 resources = copy.deepcopy (sep.conf.RESOURCES)
91 for k in t.keys ():
92 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
2d10d74b 93 return render_to_response ("savoirs/informations.html", \
92c7413b 94 Context ({'r': resources}), \
d15017b2
CR
95 context_instance = RequestContext(request))
96
97def nous_contacter (request):
5ecd5424 98 return render_to_response ("savoirs/contact.html", \
d15017b2
CR
99 Context (), \
100 context_instance = RequestContext(request))
5ecd5424 101
92c7413b
CR
102
103def evenement(request, id):
104 event = evenement_info(id)
105 return render_to_response ("savoirs/evenement.html", \
106 Context ({'event': event.instance.vevent}), \
107 context_instance = RequestContext(request))
108
109def evenement_ajout(request):
110 template = "savoirs/evenement_ajout.html"
111 if request.method == "POST":
112 form = EvenementForm(request.POST)
113 if form.is_valid():
114 form.save()
115 template = "savoirs/evenement_confirmation.html"
116 else:
117 form = EvenementForm()
118 return render_to_response (template, \
119 Context ({'form': form}), \
120 context_instance = RequestContext(request))
121
122@login_required
123def evenement_moderation(request):
124 events = Evenement.objects.filter(approuve = False)
125 return render_to_response ("savoirs/evenement_moderation.html", \
126 Context ({'events': events}), \
127 context_instance = RequestContext(request))
128
129@login_required
130def evenement_accepter(request, pk):
131 e = Evenement.objects.get(pk = pk)
132
133 cal = vobject.iCalendar()
134 cal.add('vevent')
135 cal.vevent.add('summary').value = e.titre
136
137 kw = e.mots_cles.split(",")
138 try:
139 kw.append(e.discipline.nom)
140 kw.append(e.discipline_secondaire.nom)
141 kw.append(e.type)
142 except: pass
143
144 kw = [x.strip() for x in kw if len(x.strip()) > 0]
145 for k in kw:
146 cal.vevent.add('x-auf-keywords').value = k
147
148 if len(kw) > 0:
149 if len(e.description) > 0:
150 e.description += "\n"
151 e.description += u"Mots-cles: " + ", ".join(kw)
152
153 cal.vevent.add('dtstart').value = combine(e.debut, e.fuseau)
154 cal.vevent.add('dtend').value = combine(e.fin, e.fuseau)
155 cal.vevent.add('created').value = combine(datetime.datetime.now(), "UTC")
156 cal.vevent.add('dtstamp').value = combine(datetime.datetime.now(), "UTC")
157 if len(e.description) > 0:
158 cal.vevent.add('description').value = e.description
159 if len(e.contact) > 0:
160 cal.vevent.add('contact').value = e.contact
161 if len(e.url) > 0:
162 cal.vevent.add('url').value = e.url
163 if len(e.lieu) > 0:
164 cal.vevent.add('location').value = e.lieu
165
166 evenement_publie(cal)
167
168 e.actif = False
169 e.save()
170
171 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
172
173@login_required
174def evenement_refuser(request, pk):
175 evenement = Evenement.objects.get(pk = pk)
176 evenement.actif = False
177 evenement.save()
178 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
179
180
5ecd5424
CR
181@login_required
182def json_get (request):
183 uri = request.GET.get ("uri")
184 if uri:
92c7413b 185 s = sep.sep.io.SEP ()
5ecd5424
CR
186 res = s.search ({'uri': uri.encode("utf-8")})
187 if len (res) > 0:
188 r = s.get (res[0])
189
190 return HttpResponse(simplejson.dumps(r),
191 mimetype='application/json')
192
193@login_required
194def json_set (request):
195 data = request.POST.get("data")
196 if data:
197 r = simplejson.loads(data)
f4db9f51 198 print r
92c7413b 199 s = sep.sep.io.SEP ()
5ecd5424
CR
200 s.add (r)
201 return HttpResponse(simplejson.dumps("OK"),
202 mimetype='application/json')