Mercurial > public > sg101
comparison membermap/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/membermap/views.py@4532ed27bed8 |
children | 89b240fe9297 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the membermap application. | |
3 """ | |
4 from django.shortcuts import render_to_response | |
5 from django.template.loader import render_to_string | |
6 from django.template import RequestContext | |
7 from django.http import HttpResponse | |
8 from django.http import HttpResponseBadRequest | |
9 from django.http import HttpResponseForbidden | |
10 from django.views.decorators.http import require_POST | |
11 import django.utils.simplejson as json | |
12 from django.core.cache import cache | |
13 | |
14 from membermap.models import MapEntry | |
15 from membermap.forms import MapEntryForm | |
16 from bio.models import UserProfile | |
17 | |
18 CACHE_KEY = 'membermap_json' | |
19 CACHE_TIMEOUT = 5 * 60 | |
20 | |
21 | |
22 def index(request): | |
23 entry = None | |
24 if request.user.is_authenticated(): | |
25 try: | |
26 entry = MapEntry.objects.get(user=request.user) | |
27 except MapEntry.DoesNotExist: | |
28 pass | |
29 if entry is not None: | |
30 form = MapEntryForm(initial={ | |
31 'location': entry.location, | |
32 'message': entry.message}) | |
33 else: | |
34 form = MapEntryForm() | |
35 | |
36 return render_to_response('membermap/index.html', { | |
37 'form': form, | |
38 }, | |
39 context_instance = RequestContext(request)) | |
40 | |
41 | |
42 def query(request): | |
43 """ | |
44 This view is called by AJAX. If the user is logged in, return | |
45 a JSON object that consists of: | |
46 "users" : array of user objects | |
47 "recent" : array of usernames recently modified | |
48 """ | |
49 if not request.user.is_authenticated(): | |
50 return HttpResponseForbidden('You must be logged in.') | |
51 | |
52 # Do we have all the JSON cached? | |
53 s = cache.get(CACHE_KEY) | |
54 if s: | |
55 return HttpResponse(s, content_type='application/json') | |
56 | |
57 # Compute JSON for the map | |
58 entries = MapEntry.objects.all().select_related().order_by('user__username') | |
59 users = [] | |
60 user_ids = [] | |
61 recent = [] | |
62 for entry in entries.iterator(): | |
63 users.append(dict(name=entry.user.username, | |
64 lat=entry.lat, | |
65 lon=entry.lon, | |
66 message=entry.html, | |
67 )) | |
68 user_ids.append(entry.user.id) | |
69 recent.append((entry.date_updated, entry.user.username)) | |
70 | |
71 # Get avatars for all users | |
72 profiles = UserProfile.objects.filter(user__in=user_ids).select_related() | |
73 avatars = {} | |
74 for profile in profiles.iterator(): | |
75 if profile.avatar and profile.avatar.url: | |
76 avatars[profile.user.username] = profile.avatar.url | |
77 | |
78 # Render the messages that go in the balloons | |
79 for user in users: | |
80 user['message'] = render_to_string('membermap/balloon.html', | |
81 dict(user=user, avatar_url=avatars.get(user['name']))) | |
82 | |
83 # Produce the list of recent updates | |
84 recent.sort(reverse=True) | |
85 del recent[10:] | |
86 recent = [entry[1] for entry in recent] | |
87 | |
88 # Create the JSON for the map | |
89 result = dict(users=users, recent=recent) | |
90 s = json.dumps(result, ensure_ascii=False) | |
91 | |
92 cache.set(CACHE_KEY, s, CACHE_TIMEOUT) | |
93 return HttpResponse(s, content_type='application/json') | |
94 | |
95 | |
96 @require_POST | |
97 def add(request): | |
98 """ | |
99 This view is called by AJAX to add/update the user to the map. | |
100 It returns the new JSON representation of the user. | |
101 """ | |
102 if not request.user.is_authenticated(): | |
103 return HttpResponseForbidden('You must be logged in.') | |
104 | |
105 loc = request.POST.get('loc', None) | |
106 lat = request.POST.get('lat', None) | |
107 lon = request.POST.get('lon', None) | |
108 msg = request.POST.get('msg', '') | |
109 | |
110 if loc is None or lat is None or lon is None: | |
111 return HttpResponseBadRequest('Missing parameters') | |
112 | |
113 try: | |
114 lat = float(lat) | |
115 lon = float(lon) | |
116 except ValueError: | |
117 return HttpResponseBadRequest('Invalid lat/lon') | |
118 | |
119 try: | |
120 entry = MapEntry.objects.get(user=request.user) | |
121 except MapEntry.DoesNotExist: | |
122 entry = MapEntry(user=request.user) | |
123 | |
124 entry.location = loc | |
125 entry.lat = lat | |
126 entry.lon = lon | |
127 entry.message = msg | |
128 entry.save() | |
129 | |
130 cache.delete(CACHE_KEY) | |
131 | |
132 avatar_url = None | |
133 profile = entry.user.get_profile() | |
134 if profile.avatar and profile.avatar.url: | |
135 avatar_url = profile.avatar.url | |
136 | |
137 u = dict(name=entry.user.username, | |
138 lat=entry.lat, | |
139 lon=entry.lon, | |
140 message=entry.html) | |
141 | |
142 u['message'] = render_to_string('membermap/balloon.html', | |
143 dict(user=u, avatar_url=avatar_url)) | |
144 | |
145 result = json.dumps(u, ensure_ascii=False) | |
146 return HttpResponse(result, content_type='application/json') | |
147 | |
148 | |
149 @require_POST | |
150 def delete(request): | |
151 """ | |
152 This view is called by AJAX to delete the user from the map. | |
153 """ | |
154 if not request.user.is_authenticated(): | |
155 return HttpResponseForbidden('You must be logged in.') | |
156 | |
157 try: | |
158 entry = MapEntry.objects.get(user=request.user) | |
159 except MapEntry.DoesNotExist: | |
160 pass | |
161 else: | |
162 entry.delete() | |
163 cache.delete(CACHE_KEY) | |
164 | |
165 return HttpResponse('') |