Skip to content

Commit

Permalink
Merge pull request #8 from serge-gaia/export_data
Browse files Browse the repository at this point in the history
Export data
  • Loading branch information
serge-gaia authored Jun 10, 2016
2 parents 9929fbe + fb43bda commit c3aeb20
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 29 deletions.
4 changes: 2 additions & 2 deletions biosys/apps/main/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ProjectAdmin(MainAppAdmin, OSMGeoAdmin):
list_display = ('title', 'id', 'code', 'custodian')
fields = ['title', 'code', 'custodian', 'email', 'objectives', 'methodology',
'funding', 'duration', 'datum', 'extent_lat_min', 'extent_lat_max',
'extent_long_min', 'extent_long_max', 'comments']
'extent_long_min', 'extent_long_max', 'attributes_descriptor', 'attributes', 'comments']
readonly_fields = ['id']
search_fields = ['title', 'code', 'custodian', 'objectives', 'methodology']
modifiable = False
Expand Down Expand Up @@ -119,7 +119,7 @@ class SiteAdmin(MainAppAdmin, GeoModelAdmin):
(None, {
'fields': (
'project', 'site_ID', 'parent_site', 'site_code', 'site_name', 'date_established', 'established_by',
'datum', 'latitude', 'longitude', 'accuracy')
'datum', 'latitude', 'longitude', 'accuracy', 'dataset', 'data')
}),
('Plot orientation and size (for rectangular quadrats)', {
'classes': ('grp-collapse',),
Expand Down
30 changes: 30 additions & 0 deletions biosys/apps/main/migrations/0002_auto_20160609_1716.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-09 09:16
from __future__ import unicode_literals

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('main', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='project',
old_name='data',
new_name='attributes',
),
migrations.RemoveField(
model_name='project',
name='dataset',
),
migrations.AddField(
model_name='project',
name='attributes_descriptor',
field=django.contrib.postgres.fields.jsonb.JSONField(null=True),
),
]
8 changes: 3 additions & 5 deletions biosys/apps/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,8 @@ class Project(models.Model):
geometry = models.GeometryField(srid=MODEL_SRID, spatial_index=True, null=True, blank=True, editable=True,
verbose_name="Extent Geometry", help_text="")
# can't extend AbstractRecord directly because we need to change the related name and possible null
data = JSONField(null=True)
dataset = models.ForeignKey(DataSet, null=True, blank=True,
related_name='data_sets',
related_query_name='data_set')
attributes = JSONField(null=True, blank=True)
attributes_descriptor = JSONField(null=True, blank=True)

class Meta:
pass
Expand Down Expand Up @@ -312,7 +310,7 @@ class Site(models.Model):
verbose_name="Comments", help_text="")
geometry = models.GeometryField(srid=MODEL_SRID, spatial_index=True, null=True, blank=True, editable=True,
verbose_name="Geometry", help_text="")
data = JSONField(null=True)
data = JSONField(null=True, blank=True)
dataset = models.ForeignKey(DataSet, null=True, blank=True)

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion biosys/apps/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

urlpatterns = [
url(r'^dataset/upload/(?P<pk>\d+)/?$', views.UploadDataSetView.as_view(), name='dataset_upload'),
url(r'^dataset/export/(?P<pk>\d+)/?$', views.ExportDataSetView.as_view(), name='dataset_export'),
url(r'^dataset/export/template/(?P<pk>\d+)/?$', views.DataSetTemplateView.as_view(),
name='dataset_export_template'),

# legacy
url(r'^utils/dump_lookups/?$', views.dump_lookup_view, name="dump_lookup"),
url(r'^datasheet/schema/?$', views.datasheet_schema_view, name='datasheet_schema'),
]
5 changes: 3 additions & 2 deletions biosys/apps/main/utils_data_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def get_fk_for_model(self, model_name):
class Exporter:
def __init__(self, dataset, records=None):
self.ds = dataset
self.schema = Schema(dataset)
self.schema = Schema(dataset.schema)
self.headers = self.schema.headers
self.warnings = []
self.errors = []
Expand All @@ -242,7 +242,7 @@ def row_it(self):
for record in self.records:
row = []
for field in self.schema.field_names:
row.append(unicode(record.get(field, '')))
row.append(unicode(record.data.get(field, '')))
yield row

def to_csv(self):
Expand All @@ -261,6 +261,7 @@ def to_worksheet(self, ws):
return ws

def to_workbook(self):
# TODO: implement version in write_only mode.
wb = Workbook()
self.to_worksheet(wb.active)
return wb
4 changes: 0 additions & 4 deletions biosys/apps/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,3 @@ def form_valid(self, form):
return HttpResponseRedirect(error_url)

return super(UploadDataSetView, self).form_valid(form)


class ExportDataSetView(LoginRequiredMixin, View):
pass
4 changes: 4 additions & 0 deletions biosys/apps/publish/static/js/data_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ biosys.view_data = function ($, _, moduleOptions) {
data = options.data,
$tablePanel = $(selectors.tablePanel),
$tabletitle = $(selectors.tableTitle),
$downloadButton = $(selectors.downloadButton),
dataTable,
datasets;

Expand Down Expand Up @@ -59,6 +60,7 @@ biosys.view_data = function ($, _, moduleOptions) {
function clearDataPanel(){
$tablePanel.children().remove();
$tabletitle.text('');
$downloadButton.addClass('hide');
}

function showData(name) {
Expand Down Expand Up @@ -99,6 +101,8 @@ biosys.view_data = function ($, _, moduleOptions) {
});
dataTable = biosys.dataTable.initTable($tableNode, tableOptions, colDefs);
$tabletitle.text(name);
$downloadButton.attr('href', '/publish/export/' + ds.id);
$downloadButton.removeClass('hide');
}
}

Expand Down
27 changes: 15 additions & 12 deletions biosys/apps/publish/templates/data_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,24 @@ <h4 class="panel-title">
</div>
<div class="row">
<div class="col-xs-12">
<ul class="nav nav-pills " id="id-div-nav-panel">
<ul class="nav nav-pills" id="id-div-nav-panel">
</ul>
</div>
</div>

<div class="row" id="id-div-result-header">
<div class="col-md-6 text-center">
<h3 id="id-datatable-title"></h3>
</div>
<div class="col-md-6">
<a href="#" type="button" class="btn btn-primary btn-lg pull-left hide"
id="id-button-download">Download (Excel)</a>
</div>
</div>
<div class="row" id="id-div-result-panel">
<div class="col-xs-12">
<h3 id="id-datatable-title"></h3>
<div id="id-div-datatable"></div>
</div>
{# <div class="row">#}
{# <div class="col-xs-12">#}
{# <div id="id-div-datatable">#}
{# </div>#}
{# </div>#}
{# </div>#}
<div class="col-xs-12">
<div id="id-div-datatable"></div>
</div>
</div>
{% endblock %}

Expand All @@ -115,7 +117,8 @@ <h3 id="id-datatable-title"></h3>
navPanel: '#id-div-nav-panel',
tablePanel: '#id-div-datatable',
tableTitle: '#id-datatable-title',
dataTable: '#data-table'
dataTable: '#data-table',
downloadButton: '#id-button-download'
},
data: {% if dataJSON %} {{ dataJSON|safe }} {% else %} {} {% endif %}
});
Expand Down
3 changes: 2 additions & 1 deletion biosys/apps/publish/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf.urls import url
from apps.publish.views import data_view
from apps.publish.views import data_view, export

urlpatterns = [
url(r'^$', data_view.DataView.as_view(), name='data_view'),
url(r'^data/(?P<pk>\d+)/?$', data_view.JSONDataTableView.as_view(), name='data_json'),
url(r'^export/(?P<pk>\d+)/?$', export.ExportDataSetView.as_view(), name='data_export')
]
25 changes: 25 additions & 0 deletions biosys/apps/publish/views/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime
from django.views.generic import View
from django.shortcuts import get_object_or_404

from main.models import DataSet, GenericRecord, Observation, SpeciesObservation
from main.utils_data_package import Exporter
from main.utils_http import WorkbookResponse


class ExportDataSetView(View):
def get(self, request, *args, **kwargs):
ds = get_object_or_404(DataSet, pk=kwargs.get('pk'))
qs = []
if ds.type == DataSet.TYPE_GENERIC:
qs = GenericRecord.objects.filter(dataset=ds).order_by('id')
elif ds.type == DataSet.TYPE_OBSERVATION:
qs = Observation.objects.filter(dataset=ds).order_by('id')
elif ds.type == DataSet.TYPE_SPECIES_OBSERVATION:
qs = SpeciesObservation.objects.filter(dataset=ds).order_by('id')
exporter = Exporter(ds, qs)
wb = exporter.to_workbook()
now = datetime.datetime.now()
file_name = ds.name + '_' + now.strftime('%Y-%m-%d-%H%M%S') + '.xlsx'
response = WorkbookResponse(wb, file_name)
return response
4 changes: 2 additions & 2 deletions biosys/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# Application definition
# The variables below are added to all responses in biosys/context_processors.py
SITE_TITLE = 'BioSys - WA Biological Survey Database'
APPLICATION_VERSION_NO = '2.0.a'
APPLICATION_VERSION_NO = '2.0.b1'

INSTALLED_APPS = (
'grappelli', # Must be before django.contrib.admin
Expand Down Expand Up @@ -236,7 +236,7 @@
'propagate': False,
},
'import_lci': {
'handlers': ['import_lci', 'console'],
'handlers': ['console'],
'level': 'INFO',
'propagate': False
}
Expand Down

0 comments on commit c3aeb20

Please sign in to comment.