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
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
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
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
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
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