Beispiel #1
0
def copy_design(request, uid):
    try:
        design_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404
       
    #check permissions
    viewable, response = design_obj.is_viewable(request.user)
    if not viewable:
        return response
        
    design_obj.pk = None
    design_obj.user = request.user
    design_obj.save()
    
    json = []
    json.append({
        'id': design_obj.id,
        'uid': design_obj.uid,
        'name': design_obj.name,
        'description': design_obj.description,
        'attributes': design_obj.serialize_attributes
    })
    
    return HttpResponse(dumps(json), status=200)
Beispiel #2
0
def overlap_geotiff(collection_uids, user=None):
    collections = [get_feature_by_uid(x) for x in collection_uids.split(',')]
    collections = [
        x for x in collections if x.__class__ in get_collection_models()
    ]
    if len(collections) < 1:
        raise Http404

    filenames = []
    for collection in collections:
        viewable, response = collection.is_viewable(user)
        if user and not viewable:
            return response

        fs = collection.feature_set(recurse=True)
        poly_fs = [x for x in fs if isinstance(x, PolygonFeature)]
        unique_types = list(set([x.__class__ for x in poly_fs]))
        for model in unique_types:
            responder = ShpResponder(
                model.objects.filter(
                    pk__in=[x.pk for x in poly_fs if x.__class__ == model]))
            responder.geo_field = 'geometry_final'
            fn = responder('return_file_not_response')
            filenames.append(fn)

    temp_geotiff = create_heatmap(filenames)
    return temp_geotiff
Beispiel #3
0
def copy_design(request, uid):
    try:
        design_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404

    #check permissions
    viewable, response = design_obj.is_viewable(request.user)
    if not viewable:
        return response

    design_obj.pk = None
    design_obj.user = request.user
    design_obj.save()

    json = []
    json.append({
        'id': design_obj.id,
        'uid': design_obj.uid,
        'name': design_obj.name,
        'description': design_obj.description,
        'attributes': design_obj.serialize_attributes
    })

    return HttpResponse(dumps(json), status=200)
Beispiel #4
0
def get_object_for_editing(request, uid, target_klass=None):
    """
    Return the specified instance by uid for editing.
    If a target_klass is provided, uid will be checked for consistency.
    If the request has no logged-in user, a 401 Response will be returned. If 
    the item is not found, a 404 Response will be returned. If the user is 
    not authorized to edit the item (not the owner or a staff user), a 403 Not
    Authorized Response will be returned.

    usage:

    instance = get_object_for_editing(request, 'mlpa_mpa_12', target_klass=Mpa)
    if isinstance(instance, HttpResponse):
        return instance

    """
    if target_klass and not target_klass.model_uid() in uid:
        return HttpResponse(
            "Target class %s doesn't match the provided uid %s" %
            (target_klass, uid),
            status=401)
    try:
        instance = get_feature_by_uid(uid)
    except ValueError:
        return HttpResponse("Uid not valid: %s" % uid, status=401)
    except:
        return HttpResponse("Feature not found - %s" % uid, status=404)

    if not request.user.is_authenticated():
        return HttpResponse('You must be logged in.', status=401)
    # Check that user owns the object or is staff
    if not request.user.is_staff and request.user != instance.user:
        return HttpResponseForbidden(
            'You do not have permission to modify this object.')
    return instance
Beispiel #5
0
def get_object_for_editing(request, uid, target_klass=None):
    """
    Return the specified instance by uid for editing.
    If a target_klass is provided, uid will be checked for consistency.
    If the request has no logged-in user, a 401 Response will be returned. If 
    the item is not found, a 404 Response will be returned. If the user is 
    not authorized to edit the item (not the owner or a staff user), a 403 Not
    Authorized Response will be returned.

    usage:

    instance = get_object_for_editing(request, 'mlpa_mpa_12', target_klass=Mpa)
    if isinstance(instance, HttpResponse):
        return instance

    """
    if target_klass and not target_klass.model_uid() in uid:
        return HttpResponse("Target class %s doesn't match the provided uid %s" % 
                            (target_klass, uid),
                            status=401)
    try:
        instance = get_feature_by_uid(uid)
    except ValueError:
        return HttpResponse("Uid not valid: %s" % uid, status=401)
    except:
        return HttpResponse("Feature not found - %s" % uid, status=404)

    if not request.user.is_authenticated():
        return HttpResponse('You must be logged in.', status=401)
    # Check that user owns the object or is staff
    if not request.user.is_staff and request.user != instance.user:
        return HttpResponseForbidden(
            'You do not have permission to modify this object.')
    return instance
Beispiel #6
0
def get_data_for_feature(user, uid):
    try:
        f = get_feature_by_uid(uid)
    except:
        return False, HttpResponse("Feature %s does not exist" % uid,
                                   status=404)

    viewable, response = f.is_viewable(user)
    if not viewable:
        return viewable, response

    features = []
    collections = []

    if isinstance(f, FeatureCollection):
        obj_id = f.pk
        ct = ContentType.objects.get_for_model(f.__class__)
        for fmodel in get_feature_models():
            unattached = list(
                fmodel.objects.filter(content_type=ct, object_id=obj_id))
            features.extend(unattached)

        for cmodel in get_collection_models():
            collections_top = list(
                cmodel.objects.filter(content_type=ct, object_id=obj_id))
            collections.extend(collections_top)
    elif isinstance(f, Feature):
        features.append(f)

    return features, collections
Beispiel #7
0
def get_object_for_viewing(request, uid, target_klass=None):
    """
    Return the specified instance by uid for viewing.
    If a target_klass is provided, uid will be checked for consistency.
    If the request has no authenticated user, a 401 Response will be returned.
    If the item is not found, a 404 Response will be returned. If the user is 
    not authorized to view the item (not the owner or part of a group the item
    is shared with), a 403 Not Authorized Response will be returned.

    usage:

    instance = get_object_for_viewing(request, 'mlpa_mpa_12', target_klass=Mpa)
    if isinstance(instance, HttpResponse):
        return instance

    """
    if target_klass and not target_klass.model_uid() in uid:
        return HttpResponse("Target class %s doesn't match the provided uid %s" % 
                            (target_klass, uid),
                            status=401)
    try:
        instance = get_feature_by_uid(uid)
    except ValueError:
        return HttpResponse("Uid not valid: %s" % uid, status=401)
    except:
        return HttpResponse("Feature not found - %s" % uid, status=404)

    viewable, response = instance.is_viewable(request.user) 
    if viewable:
        return instance
    else:
        return response
Beispiel #8
0
def get_data_for_feature(user, uid):
    try:
        f = get_feature_by_uid(uid)
    except:
        return False, HttpResponse("Feature %s does not exist" % uid, status=404)

    viewable, response = f.is_viewable(user)
    if not viewable:
        return viewable, response

    features = []
    collections = []

    if isinstance(f, FeatureCollection):
        obj_id = f.pk
        ct = ContentType.objects.get_for_model(f.__class__)
        for fmodel in get_feature_models():
            unattached = list(fmodel.objects.filter(content_type=ct,object_id=obj_id))
            features.extend(unattached)

        for cmodel in get_collection_models():
            collections_top = list(cmodel.objects.filter(content_type=ct,object_id=obj_id))
            collections.extend(collections_top)
    elif isinstance(f, Feature):
        features.append(f)

    return features, collections
Beispiel #9
0
def get_object_for_viewing(request, uid, target_klass=None):
    """
    Return the specified instance by uid for viewing.
    If a target_klass is provided, uid will be checked for consistency.
    If the request has no authenticated user, a 401 Response will be returned.
    If the item is not found, a 404 Response will be returned. If the user is 
    not authorized to view the item (not the owner or part of a group the item
    is shared with), a 403 Not Authorized Response will be returned.

    usage:

    instance = get_object_for_viewing(request, 'mlpa_mpa_12', target_klass=Mpa)
    if isinstance(instance, HttpResponse):
        return instance

    """
    if target_klass and not target_klass.model_uid() in uid:
        return HttpResponse(
            "Target class %s doesn't match the provided uid %s" %
            (target_klass, uid),
            status=401)
    try:
        instance = get_feature_by_uid(uid)
    except ValueError:
        return HttpResponse("Uid not valid: %s" % uid, status=401)
    except:
        return HttpResponse("Feature not found - %s" % uid, status=404)

    viewable, response = instance.is_viewable(request.user)
    if viewable:
        return instance
    else:
        return response
Beispiel #10
0
def bioconnect(request, uid):
    try:
        feature = get_feature_by_uid(uid)
    except:
        raise Http404("UID %s does not exist" % uid)

    fgeom = feature.output_geom
    if not fgeom.valid:
        fgeom = fgeom.buffer(0)

    hits = []

    for mb in MyBioregion.objects.filter(output_geom__bboverlaps=fgeom):
        mbgeom = mb.output_geom
        if not mbgeom.valid:
            mbgeom = mbgeom.buffer(0)
        if mbgeom.intersects(fgeom):
            hits.append(mb)

    for d in DrawnBioregion.objects.filter(geometry_final__bboverlaps=fgeom):
        dgeom = d.output_geom
        if not dgeom.valid:
            dgeom = dgeom.buffer(0)
        if dgeom.intersects(fgeom):
            hits.append(d)

    context = {"hits": hits}
    return render_to_response("bioconnect.html", RequestContext(request, context))
Beispiel #11
0
def bioconnect(request, uid):
    try:
        feature = get_feature_by_uid(uid)
    except:
        raise Http404('UID %s does not exist' % uid)

    fgeom = feature.output_geom
    if not fgeom.valid:
        fgeom = fgeom.buffer(0)

    hits = []

    for mb in MyBioregion.objects.filter(output_geom__bboverlaps=fgeom):
        mbgeom = mb.output_geom
        if not mbgeom.valid:
            mbgeom = mbgeom.buffer(0)
        if mbgeom.intersects(fgeom):
            hits.append(mb)

    for d in DrawnBioregion.objects.filter(geometry_final__bboverlaps=fgeom):
        dgeom = d.output_geom
        if not dgeom.valid:
            dgeom = dgeom.buffer(0)
        if dgeom.intersects(fgeom):
            hits.append(d)

    context = {
        'hits': hits,
    }
    return render_to_response('bioconnect.html',
                              RequestContext(request, context))
Beispiel #12
0
def get_bioregion(uid):
    '''
    Gets the bioregions instance for a given uid
    '''
    try:
        return get_feature_by_uid(uid) 
    except:
        raise Http404('UID %s does not exist' % uid)
Beispiel #13
0
def get_bioregion(uid):
    '''
    Gets the bioregions instance for a given uid
    '''
    try:
        return get_feature_by_uid(uid)
    except:
        raise Http404('UID %s does not exist' % uid)
Beispiel #14
0
def get_features(uids, user):
    """ 
    Returns list of tuples representing mapnik layers
    Tuple => (model_class, [pks])
    Note: currently just a single pk per 'layer' which is
    incredibly inefficient but the only way to ensure 
    proper layer ordering (??).
        features = [ (Mpa, [49, 50]),
                    (Pipeline, [32, 31]),
                    (Shipwreck, [32, 31])
                ]
    """
    feature_models = get_feature_models()
    collection_models = get_collection_models()
    features = []  # list of tuples => (model, [pks])
    for uid in uids:
        log.debug("processing uid %s" % uid)
        applabel, modelname, pk = uid.split('_')
        model = get_model_by_uid("%s_%s" % (applabel, modelname))
        feature = get_feature_by_uid(uid)

        if user:
            viewable, response = feature.is_viewable(user)
            if not viewable:
                continue

        if model in collection_models:
            collection = get_feature_by_uid(uid)
            if user:
                viewable, response = collection.is_viewable(user)
                if not viewable:
                    continue
            all_children = collection.feature_set(recurse=True)
            children = [
                x for x in all_children if x.__class__ in feature_models
            ]
            for child in children:
                features.append((child.__class__, [child.pk]))
        else:
            features.append((model, [int(pk)]))

    return features
Beispiel #15
0
def get_attributes(request, uid):
    try:
        scenario_obj = get_feature_by_uid(uid)
    except Scenario.DoesNotExist:
        raise Http404

    #check permissions
    viewable, response = scenario_obj.is_viewable(request.user)
    if not viewable:
        return response

    return HttpResponse(dumps(scenario_obj.serialize_attributes))
Beispiel #16
0
def get_attributes(request, uid):
    try:
        scenario_obj = get_feature_by_uid(uid)
    except Scenario.DoesNotExist:
        raise Http404
    
    #check permissions
    viewable, response = scenario_obj.is_viewable(request.user)
    if not viewable:
        return response
    
    return HttpResponse(dumps(scenario_obj.serialize_attributes))
Beispiel #17
0
def get_features(uids,user):
    """ 
    Returns list of tuples representing mapnik layers
    Tuple => (model_class, [pks])
    Note: currently just a single pk per 'layer' which is
    incredibly inefficient but the only way to ensure 
    proper layer ordering (??).
        features = [ (Mpa, [49, 50]),
                    (Pipeline, [32, 31]),
                    (Shipwreck, [32, 31])
                ]
    """
    feature_models = get_feature_models()
    collection_models = get_collection_models()
    features = [] # list of tuples => (model, [pks])
    for uid in uids:
        log.debug("processing uid %s" % uid)
        applabel, modelname, pk = uid.split('_')
        model = get_model_by_uid("%s_%s" % (applabel,modelname))
        feature = get_feature_by_uid(uid)

        if user:
            viewable, response = feature.is_viewable(user)
            if not viewable:
                continue

        if model in collection_models:
            collection = get_feature_by_uid(uid)
            if user:
                viewable, response = collection.is_viewable(user)
                if not viewable:
                    continue
            all_children = collection.feature_set(recurse=True)
            children = [x for x in all_children if x.__class__ in feature_models]
            for child in children:
                features.append((child.__class__,[child.pk]))
        else:
            features.append((model,[int(pk)]))

    return features
Beispiel #18
0
def remove_bookmark(request): 
    try:
        bookmark_uid = request.POST['uid']
        bookmark = get_feature_by_uid(bookmark_uid)
        
        viewable, response = bookmark.is_viewable(request.user)
        if not viewable:
            return response
        
        bookmark.delete()
        return HttpResponse(status=200)
    except:
        return HttpResponse(status=304)
Beispiel #19
0
def delete_drawing(request, uid):
    try:
        drawing_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404

    #check permissions
    viewable, response = drawing_obj.is_viewable(request.user)
    if not viewable:
        return response

    drawing_obj.delete()

    return HttpResponse("", status=200)
Beispiel #20
0
def delete_drawing(request, uid):
    try:
        drawing_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404
    
    #check permissions
    viewable, response = drawing_obj.is_viewable(request.user)
    if not viewable:
        return response
        
    drawing_obj.delete()
    
    return HttpResponse("", status=200)
Beispiel #21
0
def share_design(request):
    from django.contrib.auth.models import Group
    group_names = request.POST.getlist('groups[]')
    design_uid = request.POST['scenario']
    design = get_feature_by_uid(design_uid)
    viewable, response = design.is_viewable(request.user)
    if not viewable:
        return response
    #remove previously shared with groups, before sharing with new list
    design.share_with(None)
    groups = []
    for group_name in group_names:
        groups.append(Group.objects.get(name=group_name))
    design.share_with(groups, append=False)
    return HttpResponse("", status=200)
Beispiel #22
0
def delete_scenario(request, uid):
    try:
        scenario_obj = get_feature_by_uid(uid)
    except Scenario.DoesNotExist:
        raise Http404
    
    #check permissions
    viewable, response = scenario_obj.is_viewable(request.user)
    if not viewable:
        return response
        
    scenario_obj.active = False
    scenario_obj.save(rerun=False)
    
    return HttpResponse("", status=200)
Beispiel #23
0
def share_design(request):
    from django.contrib.auth.models import Group
    group_names = request.POST.getlist('groups[]')
    design_uid = request.POST['scenario']
    design = get_feature_by_uid(design_uid)
    viewable, response = design.is_viewable(request.user)
    if not viewable:
        return response
    #remove previously shared with groups, before sharing with new list
    design.share_with(None)
    groups = []
    for group_name in group_names:
        groups.append(Group.objects.get(name=group_name))
    design.share_with(groups, append=False)
    return HttpResponse("", status=200)
Beispiel #24
0
def delete_design(request, uid):
    try:
        design_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404
    
    #check permissions
    viewable, response = design_obj.is_viewable(request.user)
    if not viewable:
        return response
        
    design_obj.delete()
    #design_obj.active = False
    #design_obj.save(rerun=False)
    
    return HttpResponse("", status=200)
Beispiel #25
0
def delete_design(request, uid):
    try:
        design_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404

    #check permissions
    viewable, response = design_obj.is_viewable(request.user)
    if not viewable:
        return response

    design_obj.delete()
    #design_obj.active = False
    #design_obj.save(rerun=False)

    return HttpResponse("", status=200)
Beispiel #26
0
def copy_design(request, uid):
    try:
        design_obj = get_feature_by_uid(uid)
    except Feature.DoesNotExist:
        raise Http404
       
    #check permissions
    viewable, response = design_obj.is_viewable(request.user)
    if not viewable:
        return response
        
    design_obj.pk = None
    design_obj.user = request.user
    design_obj.save()
    
    return HttpResponse("", status=200)
Beispiel #27
0
def storyconnect(request, uid):
    try:
        feature = get_feature_by_uid(uid)
    except:
        raise Http404("UID %s does not exist" % uid)

    fgeom = feature.output_geom
    if not fgeom.valid:
        fgeom = fgeom.buffer(0)

    hits = []

    for s in StoryPoint.objects.filter(geometry_final__bboverlaps=fgeom):
        sgeom = s.geometry_final
        if sgeom.intersects(fgeom):
            hits.append(s)

    context = {"hits": hits}
    return render_to_response("storyconnect.html", RequestContext(request, context))
Beispiel #28
0
def share_bookmark(request):
    group_names = request.POST.getlist('groups[]')
    bookmark_uid = request.POST['bookmark']
    bookmark = get_feature_by_uid(bookmark_uid)
    
    viewable, response = bookmark.is_viewable(request.user)
    if not viewable:
        return response
        
    #remove previously shared with groups, before sharing with new list
    bookmark.share_with(None)
    
    groups = []
    for group_name in group_names:
        groups.append(Group.objects.get(name=group_name))
        
    bookmark.share_with(groups, append=False)
    
    return HttpResponse("", status=200)
Beispiel #29
0
def storyconnect(request, uid):
    try:
        feature = get_feature_by_uid(uid)
    except:
        raise Http404('UID %s does not exist' % uid)

    fgeom = feature.output_geom
    if not fgeom.valid:
        fgeom = fgeom.buffer(0)

    hits = []

    for s in StoryPoint.objects.filter(geometry_final__bboverlaps=fgeom):
        sgeom = s.geometry_final
        if sgeom.intersects(fgeom):
            hits.append(s)

    context = {
        'hits': hits,
    }
    return render_to_response('storyconnect.html',
                              RequestContext(request, context))
Beispiel #30
0
def overlap_geotiff(collection_uids, user=None):
    collections = [get_feature_by_uid(x) for x in collection_uids.split(',')]
    collections = [x for x in collections if x.__class__ in get_collection_models()]
    if len(collections) < 1:
        raise Http404

    filenames = []
    for collection in collections:
        viewable, response = collection.is_viewable(user)
        if user and not viewable:
            return response

        fs = collection.feature_set(recurse=True)
        poly_fs = [x for x in fs if isinstance(x,PolygonFeature)]
        unique_types = list(set([x.__class__ for x in poly_fs]))
        for model in unique_types:
            responder = ShpResponder(model.objects.filter(pk__in=[x.pk for x in poly_fs if x.__class__ == model]))
            responder.geo_field = 'geometry_final'
            fn = responder('return_file_not_response')
            filenames.append(fn)

    temp_geotiff = create_heatmap(filenames)
    return temp_geotiff