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