summaryrefslogtreecommitdiff
path: root/ishtar/furnitures/views.py
blob: 832089ea82b85d7e332869a07c9dceef927c638b (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/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 file_creation_wizard
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))
    limit = 15
    persons = models.Person.objects.filter(query)[:limit]
    data = json.dumps([{'id':person.pk, 'value':unicode(person)}
                                          for person in persons])
    return HttpResponse(data, mimetype='text/plain')

def autocomplete_town(request):
    if not request.GET.get('term'):
        return HttpResponse(mimetype='text/plain')
    q = request.GET.get('term')
    query = Q()
    for q in q.split(' '):
        extra = Q(name__icontains=q)
        if settings.COUNTRY == 'fr':
            extra = extra | (Q(canton__name__istartswith=q) | \
                    Q(canton__arrondissement__name__istartswith=q) | \
                    Q(canton__arrondissement__department__label__istartswith=q))
        query = query & extra
    limit = 15
    towns = models.Town.objects.filter(query)[:limit]
    data = json.dumps([{'id':town.pk, 'value':unicode(town)}
                                          for town in towns])
    return HttpResponse(data, mimetype='text/plain')

def autocomplete_organization(request):
    if not request.GET.get('term'):
        return HttpResponse(mimetype='text/plain')
    q = request.GET.get('term')
    query = Q()
    for q in q.split(' '):
        extra = Q(name__icontains=q)
        query = query & extra
    limit = 15
    organizations = models.Organization.objects.filter(query)[:limit]
    data = json.dumps([{'id':org.pk, 'value':unicode(org)}
                                          for org in organizations])
    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 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))


def file_creation(request, dct, obj_id, *args, **kwargs):
    return file_creation_wizard(request, *args, **kwargs)