def file_upload(filename, name=None, user=None, title=None, abstract=None, keywords=None, category=None, regions=None, date=None, skip=True, overwrite=False, charset='UTF-8', metadata_uploaded_preserve=False):
 if (keywords is None):
  keywords = []
 if (regions is None):
  regions = []
 theuser = get_valid_user(user)
 upload_session = UploadSession.objects.create(user=theuser)
 files = get_files(filename)
 if (title is None):
  basename = os.path.splitext(os.path.basename(filename))[0]
  title = basename.title().replace('_', ' ')
 if (name is None):
  name = slugify(title).replace('-', '_')
 if (category is not None):
  categories = TopicCategory.objects.filter((Q(identifier__iexact=category) | Q(gn_description__iexact=category)))
  if (len(categories) == 1):
   category = categories[0]
  else:
   category = None
 valid_name = get_valid_layer_name(name, overwrite)
 assigned_name = None
 for (type_name, fn) in files.items():
  with open(fn, 'rb') as f:
   upload_session.layerfile_set.create(name=type_name, file=File(f, name=('%s.%s' % ((assigned_name or valid_name), type_name))))
   if (not assigned_name):
    the_file = upload_session.layerfile_set.all()[0].file.name
    assigned_name = os.path.splitext(os.path.basename(the_file))[0]
 (bbox_x0, bbox_x1, bbox_y0, bbox_y1) = get_bbox(filename)
 is_published = True
 if settings.RESOURCE_PUBLISHING:
  is_published = False
 defaults = {'upload_session': upload_session, 'title': title, 'abstract': abstract, 'owner': user, 'charset': charset, 'bbox_x0': bbox_x0, 'bbox_x1': bbox_x1, 'bbox_y0': bbox_y0, 'bbox_y1': bbox_y1, 'is_published': is_published, 'category': category}
 if ('xml' in files):
  with open(files['xml']) as f:
   xml_file = f.read()
  defaults['metadata_uploaded'] = True
  defaults['metadata_uploaded_preserve'] = metadata_uploaded_preserve
  (identifier, vals, regions, keywords) = set_metadata(xml_file)
  if defaults['metadata_uploaded_preserve']:
   defaults['metadata_xml'] = xml_file
   defaults['uuid'] = identifier
  for (key, value) in vals.items():
   if (key == 'spatial_representation_type'):
    value = SpatialRepresentationType(identifier=value)
   elif (key == 'topic_category'):
    (value, created) = TopicCategory.objects.get_or_create(identifier=value.lower(), defaults={'description': '', 'gn_description': value})
    key = 'category'
    defaults[key] = value
   else:
    defaults[key] = value
 (regions_resolved, regions_unresolved) = resolve_regions(regions)
 keywords.extend(regions_unresolved)
 if getattr(settings, 'NLP_ENABLED', False):
  try:
   from geonode.contrib.nlp.utils import nlp_extract_metadata_dict
   nlp_metadata = nlp_extract_metadata_dict({'title': defaults.get('title', None), 'abstract': defaults.get('abstract', None), 'purpose': defaults.get('purpose', None)})
   if nlp_metadata:
    regions_resolved.extend(nlp_metadata.get('regions', []))
    keywords.extend(nlp_metadata.get('keywords', []))
  except:
   print 'NLP extraction failed.'
 if is_vector(filename):
  defaults['storeType'] = 'dataStore'
 if is_raster(filename):
  defaults['storeType'] = 'coverageStore'
 (layer, created) = Layer.objects.get_or_create(name=valid_name, defaults=defaults)
 if ((not created) and overwrite):
  if layer.upload_session:
   layer.upload_session.layerfile_set.all().delete()
  layer.upload_session = upload_session
  layer.overwrite = overwrite
  layer.save()
 keywords = list(set(keywords))
 if keywords:
  if (len(keywords) > 0):
   layer.keywords.add(*keywords)
 regions_resolved = list(set(regions_resolved))
 if regions_resolved:
  if (len(regions_resolved) > 0):
   layer.regions.add(*regions_resolved)
 if (date is not None):
  layer.date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
  layer.save()
 return layer

def file_upload ( filename , name = None , user = None , title = None , abstract = None , keywords = None , category = None , regions = None , date = None , skip = True , overwrite = False , charset = 'UTF-8' , metadata_uploaded_preserve = False ) : if ( keywords is None ) : keywords = [ ] if ( regions is None ) : regions = [ ] theuser = get_valid_user ( user ) upload_session = UploadSession . objects . create ( user = theuser ) files = get_files ( filename ) if ( title is None ) : basename = os . path . splitext ( os . path . basename ( filename ) ) [ 0 ] title = basename . title ( ) . replace ( '_' , ' ' ) if ( name is None ) : name = slugify ( title ) . replace ( '-' , '_' ) if ( category is not None ) : categories = TopicCategory . objects . filter ( ( Q ( identifier__iexact = category ) | Q ( gn_description__iexact = category ) ) ) if ( len ( categories ) == 1 ) : category = categories [ 0 ] else : category = None valid_name = get_valid_layer_name ( name , overwrite ) assigned_name = None for ( type_name , fn ) in files . items ( ) : with open ( fn , 'rb' ) as f : upload_session . layerfile_set . create ( name = type_name , file = File ( f , name = ( '%s.%s' % ( ( assigned_name or valid_name ) , type_name ) ) ) ) if ( not assigned_name ) : the_file = upload_session . layerfile_set . all ( ) [ 0 ] . file . name assigned_name = os . path . splitext ( os . path . basename ( the_file ) ) [ 0 ] ( bbox_x0 , bbox_x1 , bbox_y0 , bbox_y1 ) = get_bbox ( filename ) is_published = True if settings . RESOURCE_PUBLISHING : is_published = False defaults = { 'upload_session' : upload_session , 'title' : title , 'abstract' : abstract , 'owner' : user , 'charset' : charset , 'bbox_x0' : bbox_x0 , 'bbox_x1' : bbox_x1 , 'bbox_y0' : bbox_y0 , 'bbox_y1' : bbox_y1 , 'is_published' : is_published , 'category' : category } if ( 'xml' in files ) : with open ( files [ 'xml' ] ) as f : xml_file = f . read ( ) defaults [ 'metadata_uploaded' ] = True defaults [ 'metadata_uploaded_preserve' ] = metadata_uploaded_preserve ( identifier , vals , regions , keywords ) = set_metadata ( xml_file ) if defaults [ 'metadata_uploaded_preserve' ] : defaults [ 'metadata_xml' ] = xml_file defaults [ 'uuid' ] = identifier for ( key , value ) in vals . items ( ) : if ( key == 'spatial_representation_type' ) : value = SpatialRepresentationType ( identifier = value ) elif ( key == 'topic_category' ) : ( value , created ) = TopicCategory . objects . get_or_create ( identifier = value . lower ( ) , defaults = { 'description' : '' , 'gn_description' : value } ) key = 'category' defaults [ key ] = value else : defaults [ key ] = value ( regions_resolved , regions_unresolved ) = resolve_regions ( regions ) keywords . extend ( regions_unresolved ) if getattr ( settings , 'NLP_ENABLED' , False ) : try : from geonode . contrib . nlp . utils import nlp_extract_metadata_dict nlp_metadata = nlp_extract_metadata_dict ( { 'title' : defaults . get ( 'title' , None ) , 'abstract' : defaults . get ( 'abstract' , None ) , 'purpose' : defaults . get ( 'purpose' , None ) } ) if nlp_metadata : regions_resolved . extend ( nlp_metadata . get ( 'regions' , [ ] ) ) keywords . extend ( nlp_metadata . get ( 'keywords' , [ ] ) ) except : print 'NLP extraction failed.' if is_vector ( filename ) : defaults [ 'storeType' ] = 'dataStore' if is_raster ( filename ) : defaults [ 'storeType' ] = 'coverageStore' ( layer , created ) = Layer . objects . get_or_create ( name = valid_name , defaults = defaults ) if ( ( not created ) and overwrite ) : if layer . upload_session : layer . upload_session . layerfile_set . all ( ) . delete ( ) layer . upload_session = upload_session layer . overwrite = overwrite layer . save ( ) keywords = list ( set ( keywords ) ) if keywords : if ( len ( keywords ) > 0 ) : layer . keywords . add ( * keywords ) regions_resolved = list ( set ( regions_resolved ) ) if regions_resolved : if ( len ( regions_resolved ) > 0 ) : layer . regions . add ( * regions_resolved ) if ( date is not None ) : layer . date = datetime . strptime ( date , '%Y-%m-%d %H:%M:%S' ) layer . save ( ) return layer
new file form .