Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
show file type for each layer based on file it was in
Browse files Browse the repository at this point in the history
- use GDAL to detect type of every uploaded file
- make a field on UploadFile to store the detected file type
- make a property on UploadLayer to expose the type of containing file
- expose this property from the HTTP API as readonly
- use same from the angular template to correct display of layer items

also don't mess up template formatting when fields are blank, as in rasters.
also don't try to show feature_count if it's None, as in rasters:
either we have an uninformative, unnecessarily alarming "UNKNOWN" or
we have whitespace which bootstrap can't handle, breaking page formatting.
Rather just not have it when it's not actually meaningful to show.
  • Loading branch information
Sasha Hart committed Sep 26, 2016
1 parent 6e8d11f commit 749c956
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 3 additions & 1 deletion osgeo_importer/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import logging
from tastypie.fields import DictField, ListField, CharField, ToManyField, ForeignKey
from tastypie.fields import DictField, ListField, CharField, BooleanField, ToManyField, ForeignKey
from django.contrib.auth import get_user_model
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.resources import ModelResource
Expand Down Expand Up @@ -33,6 +33,8 @@ class UploadedLayerResource(ModelResource):
configuration_options = DictField(attribute='configuration_options', null=True)
fields = ListField(attribute='fields')
status = CharField(attribute='status', readonly=True, null=True)
file_type = CharField(attribute='file_type', readonly=True)
file_name = CharField(attribute='file_name', readonly=True)

class Meta:
queryset = UploadLayer.objects.all()
Expand Down
15 changes: 15 additions & 0 deletions osgeo_importer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def __unicode__(self):
class UploadFile(models.Model):
upload = models.ForeignKey(UploadedData, null=True, blank=True)
file = models.FileField(upload_to="uploads", validators=[validate_file_extension, validate_inspector_can_read])
file_type = models.CharField(max_length=50, null=True, blank=True)
slug = models.SlugField(max_length=250, blank=True)

def __unicode__(self):
Expand Down Expand Up @@ -205,6 +206,20 @@ class UploadLayer(models.Model):
task_id = models.CharField(max_length=36, blank=True, null=True)
feature_count = models.IntegerField(null=True, blank=True)

@property
def file_name(self):
if not self.upload_file:
return None
return self.upload_file.name

@property
def file_type(self):
"""A layer's 'file type' - really the file type of the file it is in.
"""
if not self.upload_file:
return None
return self.upload_file.file_type

@property
def layer_data(self):
"""
Expand Down
10 changes: 6 additions & 4 deletions osgeo_importer/static/osgeo_importer/partials/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@
<!--<div class="light-bold">Layers</div>-->

<div class="col-xs-10 col-sm-10 col-md-10 col-lg-10 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1 layer-in-upload" ng-repeat="layer in upload.layers">
<div class="layer-upload-name"> {{layer.name}}
<div class="layer-upload-name">
<span>{{layer.name || layer.file_name}}</span>
<span ng-show="layer.name && layer.name != layer.file_name"><br>(in {{layer.file_name}})</span>
<span class="pull-right" ng-show="layer.geonode_layer.title"><span style="font-weight: 600">Layer:</span> <a href="{{layer.geonode_layer.url}}" target="_self">{{layer.geonode_layer.title}}</a></span>
</div>
<hr/>
<div class="row">
<div class="layer-upload-details col-md-12">
<div>
<span class="col-md-3 layer-upload-field-name">File Type</span>
<span class="col-md-9">{{upload.file_type}}</span>
<span class="col-md-9">{{layer.file_type || upload.file_type || "UNKNOWN" }}</span>
</div>
<div>
<div ng-show="layer.feature_count">
<span class="col-md-3 layer-upload-field-name">Features</span>
<span class="col-md-9">{{layer.feature_count | number : 0}}</span>
</div>
<div>
<span class="col-md-3 layer-upload-field-name">Status</span>
<span class="col-md-9">{{layer.status}}</span>
<span class="col-md-9">{{layer.status || "UNKNOWN" }}</span>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion osgeo_importer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .models import UploadedData, UploadLayer, UploadFile, DEFAULT_LAYER_CONFIGURATION
from .importers import OSGEO_IMPORTER
from .inspectors import OSGEO_INSPECTOR
from .utils import import_string
from .utils import import_string, NoDataSourceFound
from django.core.files.storage import FileSystemStorage

OSGEO_INSPECTOR = import_string(OSGEO_INSPECTOR)
Expand Down Expand Up @@ -183,6 +183,12 @@ def form_valid(self, form):
upfile = UploadFile.objects.create(upload=upload)
upfiles.append(upfile)
upfile.file.name = each
# Detect and store file type for later reporting, since it is no
# longer true that every upload has only one file type.
try:
upfile.file_type = self.get_file_type(each)
except NoDataSourceFound:
upfile.file_type = None
upfile.save()
upfile_basename = os.path.basename(each)
_, upfile_ext = os.path.splitext(upfile_basename)
Expand Down

0 comments on commit 749c956

Please sign in to comment.