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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Views of the project
"""
import datetime
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core import serializers
from chimere import settings
from chimere.main.actions import actions
from chimere.main.models import SubCategory, PropertyModel, Marker
from chimere.main.widgets import getMapJS, PointChooserWidget, URL_OSM_JS
from chimere.main.forms import MarkerForm
def index(request):
"""
Main page
"""
subcategories = SubCategory.getAvailable()
# by default all subcategories are checked
for cat, sub_cats in subcategories:
for sub_category in sub_cats:
sub_category.selected = True
extra_js = ""
for url in URL_OSM_JS + ["%smain_map.js" % settings.MEDIA_URL]:
extra_js += '<script src="%s"></script>\n' % url
response_dct = {'actions':actions, 'action_selected':'view',
'error_message':'',
'sub_categories':subcategories,
'extra_head':extra_js + getMapJS(),
'media_path':settings.MEDIA_URL,
}
return render_to_response('main_map.html', response_dct)
def edit(request):
"""
Edition page
"""
# If the form has been submited
if request.method == 'POST':
form = MarkerForm(request.POST, request.FILES)
# All validation rules pass
if form.is_valid():
marker = form.save()
# set the submited status
marker.status = 'S'
marker.save()
return HttpResponseRedirect('/chimere/submited')
else:
# An unbound form
form = MarkerForm()
# get the « manualy » declared_fields. Ie: properties
declared_fields = form.declared_fields.keys()
response_dct = {'actions':actions, 'action_selected':'edit',
'error_message':'',
'media_path':settings.MEDIA_URL,
'form':form,
'extra_head':form.media,
'sub_categories':SubCategory.getAvailable(),
'point_widget':PointChooserWidget().render('point', None),
'properties':declared_fields
}
# manualy populate the custom widget
if 'subcategory' in form.data and form.data['subcategory']:
response_dct['current_category'] = int(form.data['subcategory'])
return render_to_response('edit.html', response_dct)
def submited(request):
"""
Successful submission page
"""
response_dct = {'actions':actions, 'action_selected':'edit',
'media_path':settings.MEDIA_URL,}
return render_to_response('submited.html', response_dct)
def getDetail(request, marker_id):
'''
Get the detail for a marker
'''
try:
marker = Marker.objects.filter(id=int(marker_id), status='A')[0]
except (ValueError, IndexError):
return HttpResponse('no results')
response_dct= {'media_path':settings.MEDIA_URL, 'marker':marker}
return render_to_response('detail.html', response_dct)
def getMarkers(request, category_ids):
'''
Get the JSON for a marker
'''
try:
query = Marker.objects.filter(status='A')
query = query.extra(where=['subcategory_id IN (%s)' % \
",".join(category_ids.split('_'))])
except:
return HttpResponse('no results')
markers = list(query)
if not markers:
return HttpResponse('no results')
data = '{"type": "FeatureCollection", "features":[%s]}' % \
",".join([marker.getGeoJSON() for marker in markers])
return HttpResponse(data)
|