<s> from tempfile import NamedTemporaryFile <EOL> import json <EOL> from django . contrib . gis . geos import GEOSGeometry <EOL> import pandas <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . views . generic import View <EOL> from django . contrib . auth . models import User <EOL> from ga_resources . drivers . spatialite import SpatialiteDriver <EOL> from ga_resources . models import DataResource <EOL> from mezzanine . pages . models import Page <EOL> from tastypie . models import ApiKey <EOL> from ga_resources import dispatch <EOL> from ga_resources . utils import authorize , get_data_page_for_user , json_or_jsonp <EOL> def get_user ( request ) : <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> api_key = ApiKey . objects . get ( key = request . REQUEST [ '<STR_LIT>' ] ) <EOL> return api_key . user <EOL> elif request . user . is_authenticated ( ) : <EOL> return User . objects . get ( pk = request . user . pk ) <EOL> else : <EOL> return request . user <EOL> def create_dataset ( request ) : <EOL> user = authorize ( request ) <EOL> title = request . REQUEST . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> srid = int ( request . REQUEST . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> geometry_type = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> columns_definitions = json . loads ( request . REQUEST . get ( '<STR_LIT>' , "<STR_LIT:{}>" ) ) <EOL> columns_definitions = ( ( key , value ) for key , value in columns_definitions . items ( ) ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> parent = Page . objects . get ( slug = request . REQUEST [ '<STR_LIT>' ] ) <EOL> authorize ( request , parent , add = True ) <EOL> else : <EOL> parent = get_data_page_for_user ( request . user ) <EOL> ds = SpatialiteDriver . create_dataset ( <EOL> title = title , <EOL> parent = parent , <EOL> srid = srid , <EOL> geometry_type = geometry_type , <EOL> columns_definitions = columns_definitions , <EOL> owner = request . user <EOL> ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . dataset_created . send ( sender = DataResource , instance = ds , user = user ) <EOL> return json_or_jsonp ( request , { '<STR_LIT:path>' : ds . slug } , code = <NUM_LIT> ) <EOL> def derive_dataset ( request , slug ) : <EOL> user = authorize ( request ) <EOL> title = request . REQUEST . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> parent_dataresource = slug <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> parent_page = Page . objects . get ( slug = request . REQUEST [ '<STR_LIT>' ] ) <EOL> authorize ( request , parent_page , add = True ) <EOL> else : <EOL> parent_page = get_data_page_for_user ( request . user ) <EOL> parent_dataresource = DataResource . objects . get ( slug = parent_dataresource ) <EOL> authorize ( request , parent_dataresource , view = True ) <EOL> ds = SpatialiteDriver . derive_dataset ( <EOL> title = title , <EOL> parent_page = parent_page , <EOL> parent_dataresource = parent_dataresource , <EOL> owner = request . user <EOL> ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . dataset_created . send ( sender = DataResource , instance = ds , user = user ) <EOL> return json_or_jsonp ( request , { '<STR_LIT:path>' : ds . slug } , code = <NUM_LIT> ) <EOL> def create_dataset_with_parent_geometry ( request , slug ) : <EOL> user = authorize ( request ) <EOL> title = request . REQUEST . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> parent_dataresource = slug <EOL> srid = int ( request . REQUEST . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> geometry_type = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> columns_definitions = json . loads ( request . REQUEST . get ( '<STR_LIT>' , "<STR_LIT:{}>" ) ) <EOL> columns_definitions = ( ( key , value ) for key , value in columns_definitions . items ( ) ) <EOL> parent_key = request . REQUEST . get ( '<STR_LIT>' , None ) <EOL> child_key = request . REQUEST . get ( '<STR_LIT>' , None ) <EOL> csv = None <EOL> if len ( request . FILES . keys ( ) ) > <NUM_LIT:0> : <EOL> csvfile = NamedTemporaryFile ( suffix = '<STR_LIT>' ) <EOL> csvfile . write ( request . FILES [ request . FILES . keys ( ) . next ( ) ] . read ( ) ) <EOL> csvfile . flush ( ) <EOL> csv = pandas . DataFrame . from_csv ( csvfile . name ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> parent_page = Page . objects . get ( slug = request . REQUEST [ '<STR_LIT>' ] ) <EOL> authorize ( request , parent_page , add = True ) <EOL> else : <EOL> parent_page = get_data_page_for_user ( request . user ) <EOL> parent_dataresource = DataResource . objects . get ( slug = parent_dataresource ) <EOL> authorize ( request , parent_page , view = True ) <EOL> if csv : <EOL> ds = SpatialiteDriver . join_data_with_existing_geometry ( <EOL> title = title , <EOL> parent = parent_page , <EOL> new_data = csv , <EOL> join_field_in_existing_data = parent_key , <EOL> join_field_in_new_data = child_key , <EOL> parent_dataresource = parent_dataresource , <EOL> srid = srid , <EOL> geometry_type = geometry_type , <EOL> owner = request . user <EOL> ) <EOL> else : <EOL> ds = SpatialiteDriver . create_dataset_with_parent_geometry ( <EOL> title = title , <EOL> parent = parent_page , <EOL> parent_dataresource = parent_dataresource , <EOL> srid = srid , <EOL> columns_definitions = columns_definitions , <EOL> geometry_type = geometry_type , <EOL> owner = request . user <EOL> ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . dataset_created . send ( sender = ds , user = user ) <EOL> return json_or_jsonp ( request , { '<STR_LIT:path>' : ds . slug } , code = <NUM_LIT> ) <EOL> def schema ( request , slug = None , * args , ** kwargs ) : <EOL> s = get_object_or_404 ( DataResource , slug = slug ) <EOL> user = authorize ( request , s , view = True ) <EOL> r = [ { '<STR_LIT:name>' : n } for n in s . driver_instance . schema ( ) ] <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = s , user = user ) <EOL> return json_or_jsonp ( request , r ) <EOL> @ csrf_exempt <EOL> def add_column ( request , slug = None , * args , ** kwargs ) : <EOL> field_name = request . REQUEST [ '<STR_LIT:name>' ] <EOL> field_type = request . REQUEST . get ( '<STR_LIT:type>' , '<STR_LIT:text>' ) <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> user = authorize ( request , ds , edit = True ) <EOL> ds . driver_instance . add_column ( field_name , field_type ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . dataset_column_added . send ( sender = DataResource , instance = ds , user = user ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> @ csrf_exempt <EOL> def add_row ( request , slug = None , * args , ** kwargs ) : <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> user = authorize ( request , ds , edit = True ) <EOL> schema = { k for k in ds . driver_instance . schema ( ) } <EOL> row = { k : v for k , v in request . REQUEST . items ( ) if k in schema } <EOL> try : <EOL> payload = json . loads ( request . body ) <EOL> for k in [ x for x in payload if x in schema ] : <EOL> row [ k ] = payload [ k ] <EOL> except : <EOL> pass <EOL> new_rec = ds . driver_instance . add_row ( ** row ) <EOL> bbox = GEOSGeometry ( row [ '<STR_LIT>' ] ) . envelope <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . features_created . send ( sender = DataResource , instance = ds , user = user , count = <NUM_LIT:1> , bbox = bbox ) <EOL> return json_or_jsonp ( request , new_rec , code = <NUM_LIT> ) <EOL> @ csrf_exempt <EOL> def update_row ( request , slug = None , ogc_fid = None , * args , ** kwargs ) : <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> user = authorize ( request , ds , edit = True ) <EOL> schema = { k for k in ds . driver_instance . schema ( ) } <EOL> row = { k : v for k , v in request . REQUEST . items ( ) if k in schema } <EOL> try : <EOL> payload = json . loads ( request . body ) <EOL> for k in [ x for x in payload if x in schema ] : <EOL> row [ k ] = payload [ k ] <EOL> except : <EOL> pass <EOL> if ogc_fid is None : <EOL> ogc_fid = row [ '<STR_LIT>' ] <EOL> bbox = GEOSGeometry ( ds . driver_instance . get_row ( int ( ogc_fid ) , geometry_format = '<STR_LIT>' ) [ '<STR_LIT>' ] ) . envelope <EOL> result = ds . driver_instance . update_row ( int ( ogc_fid ) , ** row ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . features_updated . send ( sender = DataResource , instance = ds , user = user , count = <NUM_LIT:1> , fid = ogc_fid , bbox = bbox ) <EOL> return json_or_jsonp ( request , result ) <EOL> @ csrf_exempt <EOL> def delete_row ( request , slug = None , ogc_fid = None , * args , ** kwargs ) : <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> bbox = GEOSGeometry ( ds . driver_instance . get_row ( int ( ogc_fid ) , geometry_format = '<STR_LIT>' ) [ '<STR_LIT>' ] ) . envelope <EOL> ds . driver_instance . delete_row ( int ( ogc_fid ) ) <EOL> user = authorize ( request , ds , edit = True ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . features_deleted . send ( sender = DataResource , instance = ds , user = user , count = <NUM_LIT:1> , fid = ogc_fid , bbox = bbox ) <EOL> return HttpResponse ( ) <EOL> def get_row ( request , slug = None , ogc_fid = None , * args , ** kwargs ) : <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> ds . driver_instance . ready_data_resource ( ) <EOL> user = authorize ( request , ds , view = True ) <EOL> format = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> row = ds . driver_instance . get_row ( int ( ogc_fid ) , geometry_format = format ) <EOL> except : <EOL> row = None <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . features_retrieved . send ( sender = DataResource , instance = ds , user = user , count = <NUM_LIT:1> , fid = ogc_fid ) <EOL> return json_or_jsonp ( request , row ) <EOL> def get_rows ( request , slug = None , ogc_fid_start = None , ogc_fid_end = None , limit = None , * args , ** kwargs ) : <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> user = authorize ( request , ds , view = True ) <EOL> ds . driver_instance . ready_data_resource ( ) <EOL> format = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ogc_fid_end : <EOL> rows = ds . driver_instance . get_rows ( int ( ogc_fid_start ) , int ( ogc_fid_end ) , geometry_format = format ) <EOL> elif limit : <EOL> rows = ds . driver_instance . get_rows ( int ( ogc_fid_start ) , limit = int ( limit ) , geometry_format = format ) <EOL> else : <EOL> rows = ds . driver_instance . get_rows ( int ( ogc_fid_start ) , geometry_format = format ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . features_retrieved . send ( sender = DataResource , instance = ds , user = user , count = len ( rows ) ) <EOL> return json_or_jsonp ( request , rows ) <EOL> def query ( request , slug = None , ** kwargs ) : <EOL> ds = get_object_or_404 ( DataResource , slug = slug ) <EOL> user = authorize ( request , ds , view = True ) <EOL> ds . driver_instance . ready_data_resource ( ) <EOL> maybeint = lambda x : int ( x ) if x else None <EOL> geometry_mbr = [ float ( kwargs [ k ] ) for k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] if '<STR_LIT>' in kwargs else None <EOL> srid = kwargs [ '<STR_LIT>' ] if '<STR_LIT>' in kwargs else None <EOL> geometry = request . REQUEST . get ( '<STR_LIT:g>' , None ) <EOL> geometry_format = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> geometry_operator = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> limit = maybeint ( request . REQUEST . get ( '<STR_LIT>' , None ) ) <EOL> start = maybeint ( request . REQUEST . get ( '<STR_LIT:start>' , None ) ) <EOL> end = maybeint ( request . REQUEST . get ( '<STR_LIT:end>' , None ) ) <EOL> only = request . REQUEST . get ( '<STR_LIT>' , None ) <EOL> if only : <EOL> only = only . split ( '<STR_LIT:U+002C>' ) <EOL> rest = { k : v for k , v in request . REQUEST . items ( ) if <EOL> k not in { '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT:end>' , '<STR_LIT>' , '<STR_LIT:g>' , '<STR_LIT>' , '<STR_LIT>' } } <EOL> rows = ds . driver_instance . query ( <EOL> query_mbr = geometry_mbr , <EOL> query_geometry = geometry , <EOL> geometry_format = geometry_format , <EOL> geometry_operator = geometry_operator , <EOL> query_geometry_srid = srid , <EOL> limit = limit , <EOL> start = start , <EOL> end = end , <EOL> only = only , <EOL> ** rest <EOL> ) <EOL> dispatch . api_accessed . send ( sender = DataResource , instance = ds , user = user ) <EOL> dispatch . features_retrieved . send ( sender = DataResource , instance = ds , user = user , count = len ( rows ) ) <EOL> return json_or_jsonp ( request , rows ) <EOL> class CRUDView ( View ) : <EOL> @ method_decorator ( csrf_exempt ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( CRUDView , self ) . dispatch ( * args , ** kwargs ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> return get_row ( request , slug = kwargs [ '<STR_LIT>' ] , ogc_fid = kwargs [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT>' in kwargs : <EOL> return get_rows ( <EOL> request , <EOL> slug = kwargs [ '<STR_LIT>' ] , <EOL> ogc_fid_start = kwargs [ '<STR_LIT>' ] , <EOL> ogc_fid_end = kwargs . get ( '<STR_LIT>' , None ) , <EOL> limit = kwargs . get ( '<STR_LIT>' , None ) , <EOL> ) <EOL> else : <EOL> return query ( <EOL> request , <EOL> slug = kwargs [ '<STR_LIT>' ] , <EOL> ** request . GET <EOL> ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return add_row ( request , kwargs [ '<STR_LIT>' ] ) <EOL> def put ( self , request , * args , ** kwargs ) : <EOL> return update_row ( request , kwargs [ '<STR_LIT>' ] , kwargs . get ( '<STR_LIT>' , None ) ) <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> return delete_row ( request , kwargs [ '<STR_LIT>' ] , kwargs . get ( '<STR_LIT>' , None ) ) </s>
