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

Source Code for Module madrona.common.views

  1  from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden 
  2  from django.template import RequestContext 
  3  from django.shortcuts import get_object_or_404, render_to_response 
  4  from madrona.common import default_mimetypes as mimetypes 
  5  from madrona.news.models import Entry 
  6  from madrona.common.utils import valid_browser 
  7  from madrona.features import user_sharing_groups 
  8  from madrona.studyregion.models import StudyRegion 
  9  from madrona.layers.models import PublicLayerList, PrivateKml 
 10  from madrona.layers.views import has_privatekml 
 11  from madrona.features.views import has_features 
 12  import datetime 
 13   
 14  from django.conf import settings 
 15   
 16   
17 -def map(request, template_name='common/map_ext.html', extra_context={}):
18 """ 19 Main application window 20 Sets/Checks Cookies to determine if user needs to see the about or news panels 21 """ 22 timeformat = "%d-%b-%Y %H:%M:%S" 23 24 set_news_cookie = False 25 set_viewed_cookie = False 26 show_panel = None 27 28 useragent = request.META['HTTP_USER_AGENT'] 29 enforce_supported = settings.ENFORCE_SUPPORTED_BROWSER 30 if 'supported' in request.REQUEST and request.REQUEST['supported'] == 'false': 31 enforce_supported = False 32 if not valid_browser(useragent) and enforce_supported: 33 from madrona.common import uaparser 34 bp = uaparser.browser_platform(useragent) 35 context = {'useragent':useragent, 36 'browser_platform': bp.__repr__(), 37 'redirect_url': settings.LOGIN_REDIRECT_URL} 38 context.update(extra_context) 39 return render_to_response('common/supported_browsers.html', context) 40 41 if "mm_already_viewed" in request.COOKIES: 42 if "mm_last_checked_news" in request.COOKIES: 43 try: 44 last_checked = datetime.datetime.strptime(request.COOKIES['mm_last_checked_news'], timeformat) 45 try: 46 latest_news = Entry.objects.latest('modified_on').modified_on 47 # if theres new news, show it and reset cookie 48 if last_checked < latest_news: 49 set_news_cookie = True 50 show_panel = "news" 51 except: 52 # no news is good news?? 53 pass 54 55 except: 56 # Datetime cookie is not valid... someone's been messing with the cookies! 57 set_news_cookie = True 58 show_panel = "news" 59 else: 60 # haven't checked the news yet OR cleared the cookie 61 set_news_cookie = True 62 try: 63 latest_news = Entry.objects.latest('modified_on').modified_on 64 show_panel = "news" 65 except: 66 pass 67 else: 68 # Haven't ever visited MM or cleared their cookies 69 set_viewed_cookie = True 70 show_panel = "about" 71 72 # Check if the user is a member of any sharing groups (not including public shares) 73 member_of_sharing_group = False 74 user = request.user 75 if user.is_authenticated() and user_sharing_groups(user): 76 member_of_sharing_group = True 77 78 context = RequestContext(request,{ 79 'api_key':settings.GOOGLE_API_KEY, 80 'session_key': request.session.session_key, 81 'show_panel': show_panel, 82 'member_of_sharing_group': member_of_sharing_group, 83 'is_studyregion': StudyRegion.objects.count() > 0, 84 'is_public_layers': PublicLayerList.objects.filter(active=True).count() > 0, 85 'is_privatekml': has_privatekml(user), 86 'has_features': has_features(user), 87 'camera': parse_camera(request), 88 'publicstate': get_publicstate(request), 89 'bookmarks_as_feature': settings.BOOKMARK_FEATURE, 90 }) 91 92 context.update(extra_context) 93 response = render_to_response(template_name, context) 94 95 if set_news_cookie: 96 now = datetime.datetime.strftime(datetime.datetime.now(), timeformat) 97 response.set_cookie("mm_last_checked_news", now) 98 99 if set_viewed_cookie: 100 max_age = 365 * 24 * 60 * 60 # one year 101 expire_stamp = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") 102 response.set_cookie("mm_already_viewed","True", expires=expire_stamp) 103 104 return response
105
106 -def forbidden(request, *args, **kwargs):
107 return HttpResponse('Access denied', status=403)
108
109 -def parse_camera(request):
110 camera_params = ["Latitude", "Longitude", "Altitude", "Heading", "Tilt", "Roll", "AltitudeMode"] 111 camera = {} 112 for p in camera_params: 113 try: 114 camera[p] = float(request.REQUEST[p]) 115 except KeyError: 116 pass 117 118 if len(camera.keys()) == 0: 119 return None 120 return camera
121
122 -def get_publicstate(request):
123 try: 124 s = request.REQUEST['publicstate'] 125 except KeyError: 126 s = None 127 return s
128
129 -def launch(request, template_name='common/launch.html', extra_context={}):
130 """ 131 Launch screen / Home page for application 132 """ 133 context = {} 134 context.update(extra_context) 135 return render_to_response(template_name, context)
136