comparison gpp/membermap/views.py @ 266:4532ed27bed8

Fixing #112. Rework member map to untangle user profile and avatar cache from the membermap (since it wasn't really working anyway).
author Brian Neal <bgneal@gmail.com>
date Sat, 25 Sep 2010 18:04:44 +0000
parents dbd703f7d63a
children
comparison
equal deleted inserted replaced
265:1ba2c6bf6eb7 266:4532ed27bed8
1 """ 1 """
2 Views for the membermap application. 2 Views for the membermap application.
3 """ 3 """
4 from django.shortcuts import render_to_response 4 from django.shortcuts import render_to_response
5 from django.template.loader import render_to_string
5 from django.template import RequestContext 6 from django.template import RequestContext
6 from django.http import HttpResponse 7 from django.http import HttpResponse
7 from django.http import HttpResponseBadRequest 8 from django.http import HttpResponseBadRequest
8 from django.http import HttpResponseForbidden 9 from django.http import HttpResponseForbidden
9 from django.views.decorators.http import require_POST 10 from django.views.decorators.http import require_POST
11 import django.utils.simplejson as json
12 from django.core.cache import cache
10 13
11 from membermap.models import MapEntry 14 from membermap.models import MapEntry
12 from membermap.forms import MapEntryForm 15 from membermap.forms import MapEntryForm
16 from bio.models import UserProfile
17
18 CACHE_KEY = 'membermap_json'
19 CACHE_TIMEOUT = 5 * 60
13 20
14 21
15 def index(request): 22 def index(request):
16 entry = None 23 entry = None
17 if request.user.is_authenticated(): 24 if request.user.is_authenticated():
37 This view is called by AJAX. If the user is logged in, return 44 This view is called by AJAX. If the user is logged in, return
38 a JSON object that consists of: 45 a JSON object that consists of:
39 "users" : array of user objects 46 "users" : array of user objects
40 "recent" : array of usernames recently modified 47 "recent" : array of usernames recently modified
41 """ 48 """
42 if request.user.is_authenticated(): 49 if not request.user.is_authenticated():
43 qs = MapEntry.objects.values_list('json', flat=True).order_by('user__username') 50 return HttpResponseForbidden('You must be logged in.')
44 s = '{"users":[' + ','.join(qs) + '], "recent":['
45 51
46 names = MapEntry.objects.values_list('user__username', flat=True)[:10] 52 # Do we have all the JSON cached?
47 s += ','.join(['"%s"' % name for name in names]) 53 s = cache.get(CACHE_KEY)
48 s += ']}' 54 if s:
49 return HttpResponse(s, content_type='application/json') 55 return HttpResponse(s, content_type='application/json')
50 56
51 return HttpResponseForbidden('You must be logged in.') 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')
52 94
53 95
54 @require_POST 96 @require_POST
55 def add(request): 97 def add(request):
56 """ 98 """
83 entry.lat = lat 125 entry.lat = lat
84 entry.lon = lon 126 entry.lon = lon
85 entry.message = msg 127 entry.message = msg
86 entry.save() 128 entry.save()
87 129
88 return HttpResponse(entry.json, content_type='application/json') 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')
89 147
90 148
91 @require_POST 149 @require_POST
92 def delete(request): 150 def delete(request):
93 """ 151 """
100 entry = MapEntry.objects.get(user=request.user) 158 entry = MapEntry.objects.get(user=request.user)
101 except MapEntry.DoesNotExist: 159 except MapEntry.DoesNotExist:
102 pass 160 pass
103 else: 161 else:
104 entry.delete() 162 entry.delete()
163 cache.delete(CACHE_KEY)
105 164
106 return HttpResponse('') 165 return HttpResponse('')
107
108 # vim: ts=4 sw=4