示例#1
0
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 )
示例#2
0
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

    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 user has a single active UserLayerList
    # from lingcod.layers.models import UserLayerList
    # user = request.user
    # user_layers = False
    # if user.is_authenticated():
    #     try:
    #         UserLayerList.objects.get(user=user.id, active=True)
    #         user_layers = True
    #     except:
    #         pass
            
    # 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
示例#3
0
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

    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 user has a single active UserLayerList
    # from lingcod.layers.models import UserLayerList
    # user = request.user
    # user_layers = False
    # if user.is_authenticated():
    #     try:
    #         UserLayerList.objects.get(user=user.id, active=True)
    #         user_layers = True
    #     except:
    #         pass

    # 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