Projects-Grants
This commit is contained in:
		
							parent
							
								
									3572c75eff
								
							
						
					
					
						commit
						52a38951a3
					
				
					 5 changed files with 68 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -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):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										19
									
								
								ccdb/projects/migrations/0005_grant_projects.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								ccdb/projects/migrations/0005_grant_projects.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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'),
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										38
									
								
								ccdb/projects/migrations/0006_project_grant_data.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								ccdb/projects/migrations/0006_project_grant_data.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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),
 | 
			
		||||
    ]
 | 
			
		||||
| 
						 | 
				
			
			@ -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):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue