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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Views of the project
"""
import datetime
from django.shortcuts import render_to_response
from django.template import loader
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, News
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
extra_js += '<script src="/chimere/jsi18n/"></script>\n'
# show the welcome page
today = datetime.date.today().strftime('%y-%m-%d')
display_welcome = None
if not 'last_visit' in request.session or \
request.session['last_visit'] != today:
request.session['last_visit'] = today
display_welcome = True
response_dct = {'actions':actions, 'action_selected':'view',
'error_message':'',
'sub_categories':subcategories,
'extra_head':extra_js + getMapJS(),
'media_path':settings.MEDIA_URL,
'welcome':welcome(request, display_welcome),
}
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 welcome(request, display=None):
"""
Welcome string
"""
response_dct = {'display':display}
response_dct['news_lst'] = News.objects.filter(available=True)
return loader.render_to_string('welcome.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)
|