gremmie@1: """ gremmie@1: Views for the membermap application. gremmie@1: """ gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext gremmie@1: from django.http import HttpResponse gremmie@1: from django.http import HttpResponseBadRequest gremmie@1: from django.http import HttpResponseForbidden gremmie@1: from django.views.decorators.http import require_POST gremmie@1: gremmie@1: from membermap.models import MapEntry gremmie@1: from membermap.forms import MapEntryForm gremmie@1: gremmie@1: gremmie@1: def index(request): gremmie@1: entry = None gremmie@1: if request.user.is_authenticated(): gremmie@1: try: gremmie@1: entry = MapEntry.objects.get(user=request.user) gremmie@1: except MapEntry.DoesNotExist: gremmie@1: pass gremmie@1: if entry is not None: gremmie@1: form = MapEntryForm(initial={ gremmie@1: 'location': entry.location, gremmie@1: 'message': entry.message}) gremmie@1: else: gremmie@1: form = MapEntryForm() gremmie@1: gremmie@1: return render_to_response('membermap/index.html', { gremmie@1: 'form': form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: def query(request): gremmie@1: """ gremmie@1: This view is called by AJAX. If the user is logged in, return gremmie@1: a JSON object that consists of: gremmie@1: "users" : array of user objects gremmie@1: "recent" : array of usernames recently modified gremmie@1: """ gremmie@1: if request.user.is_authenticated(): gremmie@1: qs = MapEntry.objects.values_list('json', flat=True).order_by('user__username') gremmie@1: s = '{"users":[' + ','.join(qs) + '], "recent":[' gremmie@1: gremmie@1: names = MapEntry.objects.values_list('user__username', flat=True)[:10] gremmie@1: s += ','.join(['"%s"' % name for name in names]) gremmie@1: s += ']}' gremmie@1: return HttpResponse(s, content_type='application/json') gremmie@1: gremmie@1: return HttpResponseForbidden('You must be logged in.') gremmie@1: gremmie@1: gremmie@1: @require_POST gremmie@1: def add(request): gremmie@1: """ gremmie@1: This view is called by AJAX to add/update the user to the map. gremmie@1: It returns the new JSON representation of the user. gremmie@1: """ gremmie@1: if not request.user.is_authenticated(): gremmie@1: return HttpResponseForbidden('You must be logged in.') gremmie@1: gremmie@1: loc = request.POST.get('loc', None) gremmie@1: lat = request.POST.get('lat', None) gremmie@1: lon = request.POST.get('lon', None) gremmie@1: msg = request.POST.get('msg', '') gremmie@1: gremmie@1: if loc is None or lat is None or lon is None: gremmie@1: return HttpResponseBadRequest('Missing parameters') gremmie@1: gremmie@1: try: gremmie@1: lat = float(lat) gremmie@1: lon = float(lon) gremmie@1: except ValueError: gremmie@1: return HttpResponseBadRequest('Invalid lat/lon') gremmie@1: gremmie@1: try: gremmie@1: entry = MapEntry.objects.get(user=request.user) gremmie@1: except MapEntry.DoesNotExist: gremmie@1: entry = MapEntry(user=request.user) gremmie@1: gremmie@1: entry.location = loc gremmie@1: entry.lat = lat gremmie@1: entry.lon = lon gremmie@1: entry.message = msg gremmie@1: entry.save() gremmie@1: gremmie@1: return HttpResponse(entry.json, content_type='application/json') gremmie@1: gremmie@1: gremmie@1: @require_POST gremmie@1: def delete(request): gremmie@1: """ gremmie@1: This view is called by AJAX to delete the user from the map. gremmie@1: """ gremmie@1: if not request.user.is_authenticated(): gremmie@1: return HttpResponseForbidden('You must be logged in.') gremmie@1: gremmie@1: try: gremmie@1: entry = MapEntry.objects.get(user=request.user) gremmie@1: except MapEntry.DoesNotExist: gremmie@1: pass gremmie@1: else: gremmie@1: entry.delete() gremmie@1: gremmie@1: return HttpResponse('') gremmie@1: gremmie@1: # vim: ts=4 sw=4