renommage du module sites pour eviter conflit avec django.sites
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / views.py
1 # -*- encoding: utf-8 -*-
2 import datetime, simplejson, copy, vobject
3
4 from django.shortcuts import render_to_response
5 from django.template import Context, RequestContext
6 from django.http import HttpResponse, HttpResponseRedirect
7 from django.contrib.auth.decorators import login_required
8 from django.core.urlresolvers import reverse
9
10
11 from lib.recherche import cherche, google_search
12 from lib import sep
13 from lib.calendrier import evenements, evenement_info, evenement_publie, combine
14 from savoirs.globals import configuration
15 import backend_config
16 from forms import *
17 from models import *
18 from chercheurs.models import Chercheur
19 from sitotheque.models import Site
20
21
22 def 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']]
27 try:
28 events = evenements()[0:configuration['accueil_evenement']]
29 except:
30 events = []
31 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
32 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
33 return render_to_response ("savoirs/index.html", \
34 Context ({"articles": articles,
35 "events": events,
36 "caldav_url": configuration['calendrier_publique'],
37 "chercheurs":chercheurs,
38 "sites":sites,
39 }), \
40 context_instance = RequestContext(request))
41
42 def recherche (request):
43 q = request.GET.get("q", "")
44 page = int(request.GET.get("page", 0))
45
46 r = cherche (page, q)
47
48 return render_to_response ("savoirs/recherche.html", \
49 Context ({'q': q,
50 'page': page,
51 'data': r}), \
52 context_instance = RequestContext(request))
53
54 def avancee (request):
55 type = request.GET.get("type", "")
56 page = int(request.GET.get("page", 0))
57
58 r = {'results': [], 'last_page': 0, 'more_link': ''}
59
60 q = request.GET.get("google-q", "")
61 f = RechercheAvancee ()
62
63 if type == 'google':
64 r = cherche (page, q, type)
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)
79
80 return render_to_response ("savoirs/avancee.html", \
81 Context ({'type': type,
82 'page': page,
83 'data': r,
84 'form': f,
85 'q': q}),
86 context_instance = RequestContext(request))
87
88 def conseils (request):
89 return render_to_response ("savoirs/conseils.html", \
90 Context (), \
91 context_instance = RequestContext(request))
92
93 def a_propos (request):
94 return render_to_response ("savoirs/a-propos.html", \
95 Context ({'count': len(backend_config.RESOURCES)}), \
96 context_instance = RequestContext(request))
97
98 def informations (request):
99 s = sep.SEP()
100 t = s.logs()
101 resources = copy.deepcopy (backend_config.RESOURCES)
102 for k in t.keys ():
103 try:
104 resources[k]['logs'] = { 'date': t[k][0], 'count': t[k][1] }
105 except: pass
106 return render_to_response ("savoirs/informations.html", \
107 Context ({'r': resources}), \
108 context_instance = RequestContext(request))
109
110 def nous_contacter (request):
111 return render_to_response ("savoirs/contact.html", \
112 Context (), \
113 context_instance = RequestContext(request))
114
115
116 def 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
122 def 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
136 def 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
143 def 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
187 def 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
194 @login_required
195 def json_get (request):
196 uri = request.GET.get ("uri")
197 if uri:
198 s = sep.SEP ()
199 res = s.search ({'uri': uri.encode("utf-8")})
200 r = s.get (res)
201
202 return HttpResponse(simplejson.dumps(r[0]),
203 mimetype='application/json')
204
205 @login_required
206 def json_set (request):
207 data = request.POST.get("data")
208 if data:
209 r = simplejson.loads(data)
210 s = sep.SEP ()
211 s.add (r)
212 return HttpResponse(simplejson.dumps("OK"),
213 mimetype='application/json')