diff --git a/ccdb/projects/migrations/0004_grant_data.py b/ccdb/projects/migrations/0004_grant_data.py index 082d8f4..480a542 100644 --- a/ccdb/projects/migrations/0004_grant_data.py +++ b/ccdb/projects/migrations/0004_grant_data.py @@ -16,8 +16,8 @@ def import_grants(apps, schema_editor): reader = csv.DictReader(f, fieldnames=fieldnames) for r in reader: r['sort_order'] = None - p = Grant(**r) - p.save() + g = Grant(**r) + g.save() def remove_grants(apps, schema_editor): diff --git a/ccdb/projects/migrations/0005_grant_projects.py b/ccdb/projects/migrations/0005_grant_projects.py new file mode 100644 index 0000000..5bdb7eb --- /dev/null +++ b/ccdb/projects/migrations/0005_grant_projects.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('projects', '0004_grant_data'), + ] + + operations = [ + migrations.AddField( + model_name='grant', + name='projects', + field=models.ManyToManyField(to='projects.Project', related_name='grants'), + ), + ] diff --git a/ccdb/projects/migrations/0006_project_grant_data.py b/ccdb/projects/migrations/0006_project_grant_data.py new file mode 100644 index 0000000..e91b7e1 --- /dev/null +++ b/ccdb/projects/migrations/0006_project_grant_data.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import csv +import os + +from django.db import migrations, models + + +def import_project_grant(apps, schema_editor): + Project = apps.get_model('projects', 'Project') + Grant = apps.get_model('projects', 'Grant') + filename = 'data/tbl_HASH_Project_Grants.csv' + if os.path.exists(filename): + with open(filename) as f: + fieldnames = ['project', 'grant'] + reader = csv.DictReader(f, fieldnames=fieldnames) + for r in reader: + p = Project.objects.get(id=r['project']) + g = Grant.objects.get(id=r['grant']) + p.grants.add(g) + p.save() + + +def remove_project_grant(apps, schema_editor): + Grant = apps.get_model('projects', 'Grant') + Grant.projects.clear() + + +class Migration(migrations.Migration): + + dependencies = [ + ('projects', '0005_grant_projects'), + ] + + operations = [ + migrations.RunPython(import_project_grant, remove_project_grant), + ] diff --git a/ccdb/projects/models.py b/ccdb/projects/models.py index aaa6682..890e4ca 100644 --- a/ccdb/projects/models.py +++ b/ccdb/projects/models.py @@ -25,6 +25,7 @@ class Grant(models.Model): title = models.CharField(max_length=200) code = models.CharField(max_length=10, blank=True) description = models.CharField(max_length=255, blank=True) + projects = models.ManyToManyField(Project, related_name='grants') sort_order = models.IntegerField(blank=True, null=True) def __str__(self): diff --git a/misc/existing.py b/misc/existing.py index e2f887b..8673dc6 100644 --- a/misc/existing.py +++ b/misc/existing.py @@ -267,14 +267,14 @@ class TblHashPeopleSets(models.Model): db_table = 'tbl_HASH_People_Sets' -class TblHashProjectGrants(models.Model): - projectid = models.ForeignKey('TblLuProjects', db_column='ProjectID') # Field name made lowercase. - grantid = models.ForeignKey('TblLuGrants', db_column='GrantID') # Field name made lowercase. - - class Meta: - managed = False - db_table = 'tbl_HASH_Project_Grants' - unique_together = (('ProjectID', 'GrantID'),) +# class TblHashProjectGrants(models.Model): +# projectid = models.ForeignKey('TblLuProjects', db_column='ProjectID') # Field name made lowercase. +# grantid = models.ForeignKey('TblLuGrants', db_column='GrantID') # Field name made lowercase. +# +# class Meta: +# managed = False +# db_table = 'tbl_HASH_Project_Grants' +# unique_together = (('ProjectID', 'GrantID'),) class TblHashTrapSpecies(models.Model):