CRUD evenement + admin
[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 from django import forms
10 from lib.recherche import cherche, google_search
11 from lib import sep
12 from lib.calendrier import evenements, evenement_info, combine
13 from savoirs.globals import configuration
14 import backend_config
15 from forms import *
16 from models import *
17 from chercheurs.models import Chercheur
18 from sitotheque.models import Site
19
20
21 def 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']]
26 try:
27 events = evenements()[0:configuration['accueil_evenement']]
28 except:
29 events = []
30 chercheurs = Chercheur.objects.all().order_by('?')[:configuration['accueil_chercheur']]
31 sites = Site.objects.all().order_by('?')[:configuration['accueil_sites']]
32 return render_to_response ("savoirs/index.html", \
33 Context ({"articles": articles,
34 "events": events,
35 "caldav_url": configuration['calendrier_publique'],
36 "chercheurs":chercheurs,
37 "sites":sites,
38 }), \
39 context_instance = RequestContext(request))
40
41 def recherche (request):
42 q = request.GET.get("q", "")
43 page = int(request.GET.get("page", 0))
44
45 r = cherche (page, q)
46
47 return render_to_response ("savoirs/recherche.html", \
48 Context ({'q': q,
49 'page': page,
50 'data': r}), \
51 context_instance = RequestContext(request))
52
53 def avancee (request):
54 type = request.GET.get("type", "")
55 page = int(request.GET.get("page", 0))
56
57 r = {'results': [], 'last_page': 0, 'more_link': ''}
58
59 q = request.GET.get("google-q", "")
60 f = RechercheAvancee ()
61
62 if type == 'google':
63 r = cherche (page, q, type)
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)
78
79 return render_to_response ("savoirs/avancee.html", \
80 Context ({'type': type,
81 'page': page,
82 'data': r,
83 'form': f,
84 'q': q}),
85 context_instance = RequestContext(request))
86
87 def conseils (request):
88 return render_to_response ("savoirs/conseils.html", \
89 Context (), \
90 context_instance = RequestContext(request))
91
92 def a_propos (request):
93 return render_to_response ("savoirs/a-propos.html", \
94 Context ({'count': len(backend_config.RESOURCES)}), \
95 context_instance = RequestContext(request))
96
97 def informations (request):
98 s = sep.SEP()
99 resources = copy.deepcopy (backend_config.RESOURCES)
100
101 logs = [l for l in HarvestLog.objects.filter(context = 'moisson').order_by('date') if l.name in resources.keys()]
102 for l in logs:
103 resources[l.name]['logs'] = {'date' : l.date, 'count': l.processed}
104
105 return render_to_response ("savoirs/informations.html", \
106 Context ({'r': resources}), \
107 context_instance = RequestContext(request))
108
109 def nous_contacter (request):
110 return render_to_response ("savoirs/contact.html", \
111 Context (), \
112 context_instance = RequestContext(request))
113
114
115 def 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
121 def 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
135 def 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
142 def evenement_accepter(request, pk):
143 e = Evenement.objects.get(pk = pk)
144 e.save()
145 return HttpResponseRedirect(reverse('savoirs.views.evenement_moderation'))
146
147 @login_required
148 def 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
155 @login_required
156 def json_get (request):
157 uri = request.GET.get ("uri")
158 if uri:
159 s = sep.SEP ()
160 res = s.search ({'uri': uri.encode("utf-8")})
161 r = s.get (res)
162
163 return HttpResponse(simplejson.dumps(r[0]),
164 mimetype='application/json')
165
166 @login_required
167 def json_set (request):
168 data = request.POST.get("data")
169 if data:
170 r = simplejson.loads(data)
171 s = sep.SEP ()
172 s.add (r)
173 return HttpResponse(simplejson.dumps("OK"),
174 mimetype='application/json')