Package madrona :: Package bookmarks :: Module views
[hide private]

Source Code for Module madrona.bookmarks.views

 1  from django.http import HttpResponse 
 2  from madrona.common.views import map 
 3  from madrona.bookmarks.models import Bookmark 
 4  from django.contrib.auth.models import User 
 5  from django.core.urlresolvers import reverse 
 6  from django.contrib.sites.models import Site 
 7  from django.conf import settings 
 8  from madrona.common.utils import get_logger 
 9  import datetime 
10  import urlparse 
11   
12  log = get_logger() 
13   
14 -def lower_first(s):
15 return s[0].lower() + s[1:]
16 17 domain = "http://%s" % Site.objects.get_current().domain 18
19 -def show_bookmark(request, bookmark_id):
20 try: 21 b = Bookmark.objects.get(pk=bookmark_id) 22 except Bookmark.DoesNotExist: 23 return HttpResponse("<h3> Unable to find <em>bookmark %s</em>... please check URL </h3>" % bookmark_id, status=404) 24 25 get = request.GET.copy() 26 camera_params = ["Latitude", "Longitude", "Altitude", "Heading", "Tilt", "Roll", "AltitudeMode", 'publicstate'] 27 for p in camera_params: 28 get[p] = b.__dict__[lower_first(p)] 29 request.GET = get 30 return map(request)
31
32 -def bookmark_state_json(request, bookmark_id):
33 try: 34 b = Bookmark.objects.get(pk=bookmark_id) 35 except Bookmark.DoesNotExist: 36 return HttpResponse("<h3> Unable to find <em>bookmark %s</em>... please check URL </h3>" % bookmark_id, status=404) 37 38 state = b.publicstate 39 response = HttpResponse(state, status=200) 40 response.ContentType = "application/json" 41 return response
42
43 -def save_tool_bookmark(request):
44 u, created = User.objects.get_or_create(username=settings.BOOKMARK_ANON_USERNAME) 45 p = request.POST 46 try: 47 ip = request.META['HTTP_X_FORWARDED_FOR'] 48 except: 49 ip = request.META['REMOTE_ADDR'] 50 51 # see if they've met the limit for this IP address 52 if len(Bookmark.objects.filter(user=u, 53 date_created__gt=datetime.datetime.now() - settings.BOOKMARK_ANON_LIMIT[1], 54 ip=ip)) >= settings.BOOKMARK_ANON_LIMIT[0]: 55 log.error(ip + " has exceeded the anonymous bookmark limit") 56 return HttpResponse("Bookmark Limit Exceeded. Try again later.", status=404) 57 58 b = Bookmark( 59 user=u, 60 name="", 61 ip=ip, 62 description="", 63 latitude=p['Latitude'], 64 longitude=p['Longitude'], 65 altitude=p['Altitude'], 66 heading=p['Heading'], 67 tilt=p['Tilt'], 68 roll=p['Roll'], 69 altitudeMode=p['AltitudeMode'], 70 publicstate=p['publicstate']) 71 b.save() 72 73 url = reverse('bookmark', args=[b.pk]) 74 absurl = urlparse.urljoin(domain, url) 75 76 return HttpResponse(absurl, status=200)
77