annotate gpp/membermap/views.py @ 191:046e6ef0ff45

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