add collection-species to data import and tests
This commit is contained in:
parent
1b5cf2ebea
commit
067448943e
3 changed files with 73 additions and 5 deletions
41
ccdb/species/migrations/0005_DATA_species_collection.py
Normal file
41
ccdb/species/migrations/0005_DATA_species_collection.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
from django.db import migrations
|
||||
from django.forms import modelform_factory
|
||||
|
||||
from ccdb.utils.data import get_data_sources
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
def migrate(apps, schema_editor):
|
||||
sources = get_data_sources()
|
||||
if not sources:
|
||||
return
|
||||
|
||||
c = sources['db0']
|
||||
|
||||
CollectionSpecies = apps.get_model('species', 'CollectionSpecies')
|
||||
|
||||
CollectionSpecies.objects.all().delete()
|
||||
|
||||
CollectionSpeciesForm = modelform_factory(CollectionSpecies, fields='__all__')
|
||||
|
||||
for r in c.execute('SELECT * FROM tbl_hash_collection_species;'):
|
||||
form = CollectionSpeciesForm(dict(collection=r[0], species=r[1], sex=r[2],
|
||||
count=r[3], count_estimated=r[4]))
|
||||
if form.is_valid():
|
||||
# No PK in Andre's file
|
||||
form.save()
|
||||
else:
|
||||
print('collection species', r[0:], form.errors.as_data())
|
||||
|
||||
def rollback(apps, schema_editor):
|
||||
CollectionSpecies = apps.get_model('species', 'CollectionSpecies')
|
||||
CollectionSpecies.objects.all().delete()
|
||||
|
||||
dependencies = [
|
||||
('species', '0004_DATA_initial'),
|
||||
('collections_ccdb', '0005_DATA_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(migrate, rollback),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue