Initial grants
This commit is contained in:
		
							parent
							
								
									8552e5f12c
								
							
						
					
					
						commit
						3572c75eff
					
				
					 5 changed files with 94 additions and 12 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
from django.contrib import admin
 | 
			
		||||
 | 
			
		||||
from .models import Project
 | 
			
		||||
from .models import Project, Grant
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
admin.site.register(Project)
 | 
			
		||||
admin.site.register(Grant)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										31
									
								
								ccdb/projects/migrations/0003_initial_grant.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								ccdb/projects/migrations/0003_initial_grant.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,31 @@
 | 
			
		|||
# -*- coding: utf-8 -*-
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
 | 
			
		||||
from django.db import migrations, models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('projects', '0002_project_data'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.CreateModel(
 | 
			
		||||
            name='Grant',
 | 
			
		||||
            fields=[
 | 
			
		||||
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
 | 
			
		||||
                ('title', models.CharField(max_length=200)),
 | 
			
		||||
                ('code', models.CharField(max_length=10, blank=True)),
 | 
			
		||||
                ('description', models.CharField(max_length=255, blank=True)),
 | 
			
		||||
                ('sort_order', models.IntegerField(null=True, blank=True)),
 | 
			
		||||
            ],
 | 
			
		||||
            options={
 | 
			
		||||
                'ordering': ['sort_order'],
 | 
			
		||||
            },
 | 
			
		||||
        ),
 | 
			
		||||
        migrations.AlterUniqueTogether(
 | 
			
		||||
            name='grant',
 | 
			
		||||
            unique_together=set([('title', 'code')]),
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										36
									
								
								ccdb/projects/migrations/0004_grant_data.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								ccdb/projects/migrations/0004_grant_data.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
# -*- coding: utf-8 -*-
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
 | 
			
		||||
import csv
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from django.db import migrations, models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def import_grants(apps, schema_editor):
 | 
			
		||||
    Grant = apps.get_model('projects', 'Grant')
 | 
			
		||||
    filename = 'data/tbl_LU_Grants.csv'
 | 
			
		||||
    if os.path.exists(filename):
 | 
			
		||||
        with open(filename) as f:
 | 
			
		||||
            fieldnames = ['id', 'title', 'code', 'description', 'sort_order']
 | 
			
		||||
            reader = csv.DictReader(f, fieldnames=fieldnames)
 | 
			
		||||
            for r in reader:
 | 
			
		||||
                r['sort_order'] = None
 | 
			
		||||
                p = Grant(**r)
 | 
			
		||||
                p.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def remove_grants(apps, schema_editor):
 | 
			
		||||
    Grant = apps.get_model('projects', 'Grant')
 | 
			
		||||
    Grant.objects.all().delete()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('projects', '0003_initial_grant'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.RunPython(import_grants, remove_grants),
 | 
			
		||||
    ]
 | 
			
		||||
| 
						 | 
				
			
			@ -19,3 +19,17 @@ class Project(models.Model):
 | 
			
		|||
    class Meta:
 | 
			
		||||
        unique_together = ('name', 'code')
 | 
			
		||||
        ordering = ['sort_order']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
    sort_order = models.IntegerField(blank=True, null=True)
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return self.title
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        unique_together = ('title', 'code',)
 | 
			
		||||
        ordering = ['sort_order']
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue