def valid_browser(ua): """ Returns boolean depending on whether we support their browser based on their HTTP_USER_AGENT Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10 Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b7) Gecko/20100101 Firefox/4.0b7 """ supported_browsers = [ ('Firefox', 3, 5, 'Mac'), ('Firefox', 4, 0, 'Mac'), ('Safari', 3, 1, 'Mac'), ('Chrome', 6, 0, 'Mac'), ('Firefox', 3, 5, 'Windows'), ('Firefox', 4, 0, 'Windows'), ('Chrome', 1, 0, 'Windows'), ('IE', 8, 0, 'Windows'), ] from madrona.common import uaparser bp = uaparser.browser_platform(ua) if not bp.platform: log.warn("Platform is None: UA String is '%s'" % ua) for sb in supported_browsers: if bp.family == sb[0] and \ ((bp.v1 == sb[1] and bp.v2 >= sb[2]) or bp.v1 > sb[1]) and \ bp.platform == sb[3]: return True return False
from madrona.common import uaparser fh = open('top50.txt', 'r') for ua in [x.strip() for x in fh.readlines()]: print uaparser.browser_platform(ua)
def map(request, template_name='common/map_ext.html', extra_context={}): """ Main application window Sets/Checks Cookies to determine if user needs to see the about or news panels """ timeformat = "%d-%b-%Y %H:%M:%S" set_news_cookie = False set_viewed_cookie = False show_panel = None useragent = request.META['HTTP_USER_AGENT'] enforce_supported = settings.ENFORCE_SUPPORTED_BROWSER if 'supported' in request.REQUEST and request.REQUEST[ 'supported'] == 'false': enforce_supported = False if enforce_supported and not valid_browser(useragent): from madrona.common import uaparser bp = uaparser.browser_platform(useragent) context = { 'useragent': useragent, 'browser_platform': bp.__repr__(), 'redirect_url': settings.LOGIN_REDIRECT_URL } context.update(extra_context) return render_to_response('common/supported_browsers.html', context) if "mm_already_viewed" in request.COOKIES: if "mm_last_checked_news" in request.COOKIES: try: last_checked = datetime.datetime.strptime( request.COOKIES['mm_last_checked_news'], timeformat) try: latest_news = Entry.objects.latest( 'modified_on').modified_on # if theres new news, show it and reset cookie if last_checked < latest_news: set_news_cookie = True show_panel = "news" except: # no news is good news?? pass except: # Datetime cookie is not valid... someone's been messing with the cookies! set_news_cookie = True show_panel = "news" else: # haven't checked the news yet OR cleared the cookie set_news_cookie = True try: latest_news = Entry.objects.latest('modified_on').modified_on show_panel = "news" except: pass else: # Haven't ever visited MM or cleared their cookies set_viewed_cookie = True show_panel = "about" # Check if the user is a member of any sharing groups (not including public shares) member_of_sharing_group = False user = request.user if user.is_authenticated() and user_sharing_groups(user): member_of_sharing_group = True context = RequestContext( request, { 'api_key': settings.GOOGLE_API_KEY, 'session_key': request.session.session_key, 'show_panel': show_panel, 'member_of_sharing_group': member_of_sharing_group, 'is_studyregion': StudyRegion.objects.count() > 0, 'is_public_layers': PublicLayerList.objects.filter(active=True).count() > 0, 'is_privatekml': has_privatekml(user), 'has_features': has_features(user), 'camera': parse_camera(request), 'publicstate': get_publicstate(request), 'bookmarks_as_feature': settings.BOOKMARK_FEATURE, }) context.update(extra_context) response = render_to_response(template_name, context) if set_news_cookie: now = datetime.datetime.strftime(datetime.datetime.now(), timeformat) response.set_cookie("mm_last_checked_news", now) if set_viewed_cookie: max_age = 365 * 24 * 60 * 60 # one year expire_stamp = datetime.datetime.strftime( datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie("mm_already_viewed", "True", expires=expire_stamp) return response
def map(request, template_name='common/map_ext.html', extra_context={}): """ Main application window Sets/Checks Cookies to determine if user needs to see the about or news panels """ timeformat = "%d-%b-%Y %H:%M:%S" set_news_cookie = False set_viewed_cookie = False show_panel = None useragent = request.META['HTTP_USER_AGENT'] enforce_supported = settings.ENFORCE_SUPPORTED_BROWSER if 'supported' in request.REQUEST and request.REQUEST['supported'] == 'false': enforce_supported = False if not valid_browser(useragent) and enforce_supported: from madrona.common import uaparser bp = uaparser.browser_platform(useragent) context = {'useragent':useragent, 'browser_platform': bp.__repr__(), 'redirect_url': settings.LOGIN_REDIRECT_URL} context.update(extra_context) return render_to_response('common/supported_browsers.html', context) if "mm_already_viewed" in request.COOKIES: if "mm_last_checked_news" in request.COOKIES: try: last_checked = datetime.datetime.strptime(request.COOKIES['mm_last_checked_news'], timeformat) try: latest_news = Entry.objects.latest('modified_on').modified_on # if theres new news, show it and reset cookie if last_checked < latest_news: set_news_cookie = True show_panel = "news" except: # no news is good news?? pass except: # Datetime cookie is not valid... someone's been messing with the cookies! set_news_cookie = True show_panel = "news" else: # haven't checked the news yet OR cleared the cookie set_news_cookie = True try: latest_news = Entry.objects.latest('modified_on').modified_on show_panel = "news" except: pass else: # Haven't ever visited MM or cleared their cookies set_viewed_cookie = True show_panel = "about" # Check if the user is a member of any sharing groups (not including public shares) member_of_sharing_group = False user = request.user if user.is_authenticated() and user_sharing_groups(user): member_of_sharing_group = True context = RequestContext(request,{ 'api_key':settings.GOOGLE_API_KEY, 'session_key': request.session.session_key, 'show_panel': show_panel, 'member_of_sharing_group': member_of_sharing_group, 'is_studyregion': StudyRegion.objects.count() > 0, 'is_public_layers': PublicLayerList.objects.filter(active=True).count() > 0, 'is_privatekml': has_privatekml(user), 'has_features': has_features(user), 'camera': parse_camera(request), 'publicstate': get_publicstate(request), 'bookmarks_as_feature': settings.BOOKMARK_FEATURE, }) context.update(extra_context) response = render_to_response(template_name, context) if set_news_cookie: now = datetime.datetime.strftime(datetime.datetime.now(), timeformat) response.set_cookie("mm_last_checked_news", now) if set_viewed_cookie: max_age = 365 * 24 * 60 * 60 # one year expire_stamp = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie("mm_already_viewed","True", expires=expire_stamp) return response
from madrona.common import uaparser fh = open('top50.txt','r') for ua in [x.strip() for x in fh.readlines()]: print uaparser.browser_platform(ua)