-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviews.py
178 lines (155 loc) · 6.44 KB
/
views.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from simple_locations.models import Area,Point,AreaType
from django.db import IntegrityError
from django.conf import settings
from django.utils import simplejson
from django.http import HttpResponse
from django.utils.safestring import mark_safe
from forms import LocationForm
from django.http import HttpResponseRedirect
from mptt.exceptions import InvalidMove
from django.views.decorators.cache import cache_control
from django.template.loader import get_template
from django.template.context import Context
import decimal
#firefox likes to aaggressively cache forms set cache control to false to override this
@cache_control(no_cache=True)
def simple_locations(request):
form = LocationForm()
nodes=Area.tree.all()
return render_to_response(
'simple_locations/index.html',
{'form' : form,
'nodes':nodes,
},
context_instance=RequestContext(request))
def add_location(req, parent_id=None):
nodes = Area.tree.all()
if req.method == 'POST':
form = LocationForm(req.POST)
if form.is_valid():
name = form.cleaned_data['name']
code = form.cleaned_data['code']
lat=form.cleaned_data['lat']
lon=form.cleaned_data['lon']
target = form.cleaned_data['target']
position = form.cleaned_data['position']
kind=form.cleaned_data['kind']
area=Area(name=name,code=code)
if lat and lon:
location=Point(latitude=lat,longitude=lon)
location.save()
area.location=location
try:
kind=get_object_or_404(AreaType,pk=int(kind))
area.kind=kind
except ValueError:
pass
area.save()
if form.cleaned_data['move_choice']:
try:
Area.tree.move_node(area, target, position)
except InvalidMove:
pass
form = LocationForm()
return render_to_response(
'simple_locations/location_edit.html'
,{'form': form, 'nodes': nodes},
context_instance=RequestContext(req))
else:
form = LocationForm(req.POST)
return render_to_response(
'simple_locations/location_edit.html'
,{'form': form, 'nodes': nodes},
context_instance=RequestContext(req))
else:
if (parent_id):
default_data = {}
parent = get_object_or_404(Area, pk=parent_id)
default_data['move_choice'] = True
default_data['target'] = parent.pk
default_data['position'] = 'last-child'
form = LocationForm(default_data)
form._errors=''
else:
form = LocationForm()
return render_to_response(
'simple_locations/location_edit.html'
,{'form': form, 'nodes': nodes},
context_instance=RequestContext(req))
def edit_location(req, area_id):
location = get_object_or_404(Area, pk=area_id)
if req.method == 'POST':
form = LocationForm(req.POST)
if form.is_valid():
saved = True
area = Area.objects.get(pk=area_id)
area.name = form.cleaned_data['name']
area.code = form.cleaned_data['code']
lat=form.cleaned_data['lat']
lon=form.cleaned_data['lon']
kind=form.cleaned_data['kind']
try:
kind=get_object_or_404(AreaType,pk=int(kind))
area.kind=kind
except ValueError:
pass
if lat and lon:
point = Point(latitude=lat,longitude=lon)
point.save()
area.location = point
try:
area.save()
except IntegrityError:
form.errors['code'] = 'This code already exists'
saved = False
if form.cleaned_data['move_choice']:
target = form.cleaned_data['target']
position = form.cleaned_data['position']
try:
Area.tree.move_node(area, target, position)
except InvalidMove:
form.errors['position'] = 'This move is invalid'
saved = False
if saved:
form = LocationForm()
return render_to_response("simple_locations/location_edit.html", {"form":form, 'nodes':Area.tree.all()},
context_instance=RequestContext(req))
else:
return render_to_response("simple_locations/location_edit.html",
{"form":form, 'item': location, 'nodes':Area.tree.all()},
context_instance=RequestContext(req))
else:
return render_to_response("simple_locations/location_edit.html",
{ 'form': form, 'item': location },
context_instance=RequestContext(req))
else:
default_data = {}
default_data['pk'] = location.pk
default_data['name'] = location.name
default_data['code'] = location.code
default_data['move_choice'] = False
if location.kind:
default_data['kind'] = location.kind.pk
if location.parent:
default_data['target'] = location.parent
default_data['position'] = 'last-child'
if location.location:
default_data['lat'] = location.location.latitude
default_data['lon'] = location.location.longitude
form = LocationForm(default_data)
return render_to_response("simple_locations/location_edit.html",
{'form':form, 'nodes':Area.tree.all(),'item':location},
context_instance=RequestContext(req))
def delete_location(request, area_id):
node = get_object_or_404(Area, pk=area_id)
if request.method == 'POST':
node.delete()
return HttpResponseRedirect('/simple_locations/render_tree')
@cache_control(no_cache=True)
def render_location(request):
nodes = Area.tree.all()
return render_to_response('simple_locations/treepanel.html',{'nodes':nodes})