1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
"""
Furnitures views
"""
import json
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.db.models import Q
from django.core import serializers
from ishtar import settings
from menus import menu
from forms import Step, FileForm1, FileForm2, FileWizard
import models
def index(request):
"""
Main page
"""
dct = {}
return render_to_response('index.html', dct,
context_instance=RequestContext(request))
def check_permission(request, action_slug, obj_id=None):
if obj_id:
return menu.items[action_slug].is_available(request.user, obj_id)
return menu.items[action_slug].can_be_available(request.user)
def autocomplete_person(request):
if not request.user.has_perm('furnitures.view_person'):
return HttpResponse(mimetype='text/plain')
if not request.GET.get('term'):
return HttpResponse(mimetype='text/plain')
q = request.GET.get('term')
limit = request.GET.get('limit', 15)
try:
limit = int(limit)
except ValueError:
return HttpResponseBadRequest()
query = Q()
for q in q.split(' '):
query = query | Q(name__istartswith=q) | Q(surname__istartswith=q) | \
Q(email__icontains=q)
persons = models.Person.objects.filter(query)[:limit]
data = json.dumps([{'id':person.pk,
'value':"%s %s - %s" % (person.name, person.surname, person.email)}
for person in persons])
return HttpResponse(data, mimetype='text/plain')
def action(request, action_slug, obj_id=None, *args, **kwargs):
"""
Action management
"""
if not check_permission(request, action_slug, obj_id):
not_permitted_msg = ugettext(u"Operation not permitted.")
return HttpResponse(not_permitted_msg)
request.session['CURRENT_ACTION'] = action_slug
associated_wizard = action_slug + '_wizard'
dct = {}
globals_dct = globals()
if associated_wizard in globals_dct:
wizard = globals_dct[associated_wizard]
if ('slug' not in kwargs or not kwargs['slug']):
current_step = None
if wizard.id in request.session \
and 'current_step' in request.session[wizard.id] \
and request.session[wizard.id]['current_step']:
current_step = request.session[wizard.id]['current_step'].slug
else:
current_step = wizard.base_steps[0].slug
return redirect('action-form', action_slug, current_step)
elif wizard.id in request.session:
for step in wizard.base_steps:
if step.slug == kwargs['slug']:
request.session[wizard.id]['current_step'] = step
if action_slug in globals_dct:
return globals_dct[action_slug](request, dct, obj_id, *args, **kwargs)
return render_to_response('index.html', dct,
context_instance=RequestContext(request))
file_creation_wizard = FileWizard([
Step('general', _(u"General"), FileForm1),
Step('localisation', _(u"Localisation"), FileForm2)])
def file_creation(request, dct, obj_id, *args, **kwargs):
return file_creation_wizard(request, *args, **kwargs)
|