Mercurial > public > sg101
comparison gpp/membermap/views.py @ 1:dbd703f7d63a
Initial import of sg101 stuff from private repository.
author | gremmie |
---|---|
date | Mon, 06 Apr 2009 02:43:12 +0000 |
parents | |
children | 4532ed27bed8 |
comparison
equal
deleted
inserted
replaced
0:900ba3c7b765 | 1:dbd703f7d63a |
---|---|
1 """ | |
2 Views for the membermap application. | |
3 """ | |
4 from django.shortcuts import render_to_response | |
5 from django.template import RequestContext | |
6 from django.http import HttpResponse | |
7 from django.http import HttpResponseBadRequest | |
8 from django.http import HttpResponseForbidden | |
9 from django.views.decorators.http import require_POST | |
10 | |
11 from membermap.models import MapEntry | |
12 from membermap.forms import MapEntryForm | |
13 | |
14 | |
15 def index(request): | |
16 entry = None | |
17 if request.user.is_authenticated(): | |
18 try: | |
19 entry = MapEntry.objects.get(user=request.user) | |
20 except MapEntry.DoesNotExist: | |
21 pass | |
22 if entry is not None: | |
23 form = MapEntryForm(initial={ | |
24 'location': entry.location, | |
25 'message': entry.message}) | |
26 else: | |
27 form = MapEntryForm() | |
28 | |
29 return render_to_response('membermap/index.html', { | |
30 'form': form, | |
31 }, | |
32 context_instance = RequestContext(request)) | |
33 | |
34 | |
35 def query(request): | |
36 """ | |
37 This view is called by AJAX. If the user is logged in, return | |
38 a JSON object that consists of: | |
39 "users" : array of user objects | |
40 "recent" : array of usernames recently modified | |
41 """ | |
42 if request.user.is_authenticated(): | |
43 qs = MapEntry.objects.values_list('json', flat=True).order_by('user__username') | |
44 s = '{"users":[' + ','.join(qs) + '], "recent":[' | |
45 | |
46 names = MapEntry.objects.values_list('user__username', flat=True)[:10] | |
47 s += ','.join(['"%s"' % name for name in names]) | |
48 s += ']}' | |
49 return HttpResponse(s, content_type='application/json') | |
50 | |
51 return HttpResponseForbidden('You must be logged in.') | |
52 | |
53 | |
54 @require_POST | |
55 def add(request): | |
56 """ | |
57 This view is called by AJAX to add/update the user to the map. | |
58 It returns the new JSON representation of the user. | |
59 """ | |
60 if not request.user.is_authenticated(): | |
61 return HttpResponseForbidden('You must be logged in.') | |
62 | |
63 loc = request.POST.get('loc', None) | |
64 lat = request.POST.get('lat', None) | |
65 lon = request.POST.get('lon', None) | |
66 msg = request.POST.get('msg', '') | |
67 | |
68 if loc is None or lat is None or lon is None: | |
69 return HttpResponseBadRequest('Missing parameters') | |
70 | |
71 try: | |
72 lat = float(lat) | |
73 lon = float(lon) | |
74 except ValueError: | |
75 return HttpResponseBadRequest('Invalid lat/lon') | |
76 | |
77 try: | |
78 entry = MapEntry.objects.get(user=request.user) | |
79 except MapEntry.DoesNotExist: | |
80 entry = MapEntry(user=request.user) | |
81 | |
82 entry.location = loc | |
83 entry.lat = lat | |
84 entry.lon = lon | |
85 entry.message = msg | |
86 entry.save() | |
87 | |
88 return HttpResponse(entry.json, content_type='application/json') | |
89 | |
90 | |
91 @require_POST | |
92 def delete(request): | |
93 """ | |
94 This view is called by AJAX to delete the user from the map. | |
95 """ | |
96 if not request.user.is_authenticated(): | |
97 return HttpResponseForbidden('You must be logged in.') | |
98 | |
99 try: | |
100 entry = MapEntry.objects.get(user=request.user) | |
101 except MapEntry.DoesNotExist: | |
102 pass | |
103 else: | |
104 entry.delete() | |
105 | |
106 return HttpResponse('') | |
107 | |
108 # vim: ts=4 sw=4 |