def map(request, template_name='common/map_ext.html', extra_context={}): """ Main application window Replaces madrona.common.views.map """ from trees.models import CarbonGroup member_of_sharing_group = False user = request.user if user.is_authenticated() and user_sharing_groups(user): member_of_sharing_group = True if user.is_authenticated(): manager_of_carbongroup = CarbonGroup.objects.filter(user=user).count() > 0 else: manager_of_carbongroup = False context = RequestContext(request,{ 'session_key': request.session.session_key, 'member_of_sharing_group': member_of_sharing_group, 'manager_of_carbongroup': manager_of_carbongroup, 'show_help': True }) context.update(extra_context) response = render_to_response(template_name, context) return response
def get_sharing_groups(request): from madrona.features import user_sharing_groups try: from functools import cmp_to_key except: cmp_to_key = lambda x: x import locale locale.setlocale(locale.LC_ALL, "en_US.UTF-8") json = [] sharing_groups = user_sharing_groups(request.user) for group in sharing_groups: members = [] for user in group.user_set.all(): if user.first_name.replace(" ", "") != "" and user.last_name.replace(" ", "") != "": members.append(user.first_name + " " + user.last_name) else: members.append(user.username) sorted_members = sorted(members, key=cmp_to_key(locale.strcoll)) json.append( {"group_name": group.name, "group_slug": slugify(group.name) + "-sharing", "members": sorted_members} ) return HttpResponse(dumps(json))
def get_sharing_groups(request): from madrona.features import user_sharing_groups try: from functools import cmp_to_key except: cmp_to_key = lambda x: x import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') json = [] sharing_groups = user_sharing_groups(request.user) for group in sharing_groups: members = [] for user in group.user_set.all(): if user.first_name.replace(' ', '') != '' and user.last_name.replace(' ', '') != '': members.append(user.first_name + ' ' + user.last_name) else: members.append(user.username) sorted_members = sorted(members, key=cmp_to_key(locale.strcoll)) json.append({ 'group_name': group.name, 'group_slug': slugify(group.name)+'-sharing', 'members': sorted_members }) return HttpResponse(dumps(json))
def share_form(request,model=None, uid=None): """ Generic view for showing the sharing form for an object POST: Update the sharing status of an object GET: Provide an html form for selecting groups to which the feature will be shared. """ if model is None: return HttpResponse('Model not specified in feature urls', status=500) if uid is None: return HttpResponse('Instance UID not specified', status=500) obj = get_object_for_editing(request, uid, target_klass=model) if isinstance(obj, HttpResponse): return obj if not isinstance(obj, Feature): return HttpResponse('Instance is not a Feature', status=500) obj_type_verbose = obj._meta.verbose_name if request.method == 'GET': # Display the form # Which groups is this object already shared to? already_shared_groups = obj.sharing_groups.all() # Get a list of user's groups that have sharing permissions groups = user_sharing_groups(request.user) return render_to_response('sharing/share_form.html', {'groups': groups, 'already_shared_groups': already_shared_groups, 'obj': obj, 'obj_type_verbose': obj_type_verbose, 'user':request.user, 'MEDIA_URL': settings.MEDIA_URL, 'action': request.build_absolute_uri()}) elif request.method == 'POST': group_ids = [int(x) for x in request.POST.getlist('sharing_groups')] groups = Group.objects.filter(pk__in=group_ids) try: obj.share_with(groups) return to_response( status=200, select=obj, parent=obj.collection, ) except Exception as e: return HttpResponse( 'Unable to share objects with those specified groups: %r.' % e, status=500) else: return HttpResponse("Received unexpected " + request.method + " request.", status=400)
def get_sharing_groups(request): from madrona.features import user_sharing_groups from functools import cmp_to_key import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') json = [] sharing_groups = user_sharing_groups(request.user) for group in sharing_groups: members = [] for user in group.user_set.all(): if user.first_name.replace(' ', '') != '' and user.last_name.replace(' ', '') != '': members.append(user.first_name + ' ' + user.last_name) else: members.append(user.username) sorted_members = sorted(members, key=cmp_to_key(locale.strcoll)) json.append({ 'group_name': group.name, 'group_slug': slugify(group.name)+'-sharing', 'members': sorted_members }) return HttpResponse(dumps(json))
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
def share_form(request, model=None, uid=None): """ Generic view for showing the sharing form for an object POST: Update the sharing status of an object GET: Provide an html form for selecting groups to which the feature will be shared. """ if model is None: return HttpResponse('Model not specified in feature urls', status=500) if uid is None: return HttpResponse('Instance UID not specified', status=500) obj = get_object_for_editing(request, uid, target_klass=model) if isinstance(obj, HttpResponse): return obj if not isinstance(obj, Feature): return HttpResponse('Instance is not a Feature', status=500) obj_type_verbose = obj._meta.verbose_name if request.method == 'GET': # Display the form # Which groups is this object already shared to? already_shared_groups = obj.sharing_groups.all() # Get a list of user's groups that have sharing permissions groups = user_sharing_groups(request.user) return render_to_response( 'sharing/share_form.html', { 'groups': groups, 'already_shared_groups': already_shared_groups, 'obj': obj, 'obj_type_verbose': obj_type_verbose, 'user': request.user, 'MEDIA_URL': settings.MEDIA_URL, 'action': request.build_absolute_uri() }) elif request.method == 'POST': group_ids = [int(x) for x in request.POST.getlist('sharing_groups')] groups = Group.objects.filter(pk__in=group_ids) try: obj.share_with(groups) return to_response( status=200, select=obj, parent=obj.collection, ) except Exception as e: return HttpResponse( 'Unable to share objects with those specified groups: %r.' % e, status=500) else: return HttpResponse("Received unexpected " + request.method + " request.", status=400)