Skip to content

Commit

Permalink
Use GAIT server for GAIT queries as a separate DB. Add database router.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanjuro Jogdeo committed Dec 17, 2019
1 parent a6047a3 commit 5850196
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
75 changes: 43 additions & 32 deletions dataviz/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q, Count

from django.utils import timezone
from django.utils.timezone import utc

from django.db import models
from django.db.models import Q, Sum, Max, Count
class DBRouter:

def db_for_read(self, model, **hints):
if model._meta.app_label == 'dataviz':
return 'gait'
return None

from django.contrib.auth.models import User

def validate_positive(value):
if value <= 0:
raise ValidationError('%s is not greater than zero' % value)


class DonorCategory(models.Model):
donor_category_id = models.PositiveIntegerField(primary_key=True, db_column="DonorCategoryID", validators=[validate_positive,])
donor_category_id = models.PositiveIntegerField(
primary_key=True, db_column="DonorCategoryID", validators=[validate_positive])
name = models.CharField(db_column="DonorCategory", max_length=255)

class Meta:
Expand All @@ -26,14 +30,15 @@ def __str__(self):


class Donor(models.Model):
donor_id = models.PositiveIntegerField(primary_key=True, db_column="DonorID", validators=[validate_positive,])
donor_id = models.PositiveIntegerField(primary_key=True, db_column="DonorID", validators=[validate_positive])
name = models.CharField(db_column="Donor", max_length=3, null=True)
category = models.ForeignKey(
DonorCategory, db_column="DonorCategoryID", related_name="donors", on_delete=models.PROTECT)

class Meta:
managed = False
db_table = "donortbl"
ordering = ['name',]
ordering = ['name']

@property
def num_of_grants(self):
Expand All @@ -44,62 +49,64 @@ def __str__(self):


class DonorDepartment(models.Model):
department_id = models.PositiveIntegerField(primary_key=True, db_column="DepartmentID", validators=[validate_positive,])
department_id = models.PositiveIntegerField(
primary_key=True, db_column="DepartmentID", validators=[validate_positive])
donor = models.ForeignKey(Donor, db_column="DonorID", on_delete=models.CASCADE)
name = models.CharField(db_column="Department", max_length=255)

class Meta:
managed = False
db_table = 'donordepttbl'
ordering = ['name',]
ordering = ['name']

def __str__(self):
return self.name or ''


class Region(models.Model):
region_id = models.PositiveIntegerField(primary_key=True, db_column="RegionID", validators=[validate_positive,])
region_id = models.PositiveIntegerField(primary_key=True, db_column="RegionID", validators=[validate_positive])
name = models.CharField(db_column="Region", max_length=100)

class Meta:
managed = False
db_table = "regiontbl"
ordering = ['name',]
ordering = ['name']

def __str__(self):
return self.name or ''


class Country(models.Model):
country_id = models.PositiveIntegerField(primary_key=True, db_column="CountryID", validators=[validate_positive,])
country_id = models.PositiveIntegerField(primary_key=True, db_column="CountryID", validators=[validate_positive])
name = models.CharField(db_column="Country", max_length=100)
region = models.ForeignKey(Region, db_column="RegionID", related_name="countries", on_delete=models.CASCADE)
iso2 = models.CharField(db_column="CountryCode", max_length=2)

class Meta:
managed = False
db_table = "countrytbl"
ordering = ['name',]
ordering = ['name']

def __str__(self):
return self.name or ''


class Sector(models.Model):
sector_id = models.PositiveIntegerField(primary_key=True, db_column="SectorID", validators=[validate_positive,])
sector_id = models.PositiveIntegerField(primary_key=True, db_column="SectorID", validators=[validate_positive])
name = models.CharField(db_column="Sector", max_length=100)

class Meta:
managed = False
db_table = "n_sectortbl"
ordering = ['name',]
ordering = ['name']

def __str__(self):
return self.name or ''


class SubSector(models.Model):
subsector_id = models.PositiveIntegerField(primary_key=True, db_column="SubSectorID", validators=[validate_positive,])
subsector_id = models.PositiveIntegerField(
primary_key=True, db_column="SubSectorID", validators=[validate_positive])
name = models.CharField(db_column="SubSector", max_length=50)
sectors = models.ManyToManyField(Sector, through="SectorSubSector")

Expand All @@ -110,6 +117,7 @@ class Meta:
def __str__(self):
return self.name or ''


class SectorSubSector(models.Model):
sector = models.ForeignKey(Sector, db_column="SectorID", on_delete=models.CASCADE)
subsector = models.ForeignKey(SubSector, db_column="SubSectorID", on_delete=models.CASCADE)
Expand All @@ -120,7 +128,7 @@ class Meta:


class SectorType(models.Model):
sector_id = models.PositiveIntegerField(primary_key=True, db_column="SectorTypeID", validators=[validate_positive,])
sector_id = models.PositiveIntegerField(primary_key=True, db_column="SectorTypeID", validators=[validate_positive])
sector_type = models.CharField(db_column="SectorType", max_length=50)

class Meta:
Expand Down Expand Up @@ -154,12 +162,14 @@ class Meta:
def __str__(self):
return self.name or ''


class GrantsWonManager(models.Manager):
def get_queryset(self):
return super(GrantsWonManager, self).get_queryset().filter(
Q(status='Closed')|
Q(status='Funded')|
Q(status='Completed'))
Q(status='Closed') |
Q(status='Funded') |
Q(status='Completed')
)


class GrantsLostManager(models.Manager):
Expand All @@ -171,13 +181,15 @@ def get_queryset(self):
class BothWonLostGrants(models.Manager):
def get_queryset(self):
return super(BothWonLostGrants, self).get_queryset().filter(
Q(status='Closed')|
Q(status='Funded')|
Q(status='Completed')|
Q(status="Rejected"))
Q(status='Closed') |
Q(status='Funded') |
Q(status='Completed') |
Q(status="Rejected")
)


class Grant(models.Model):
grant_id = models.PositiveIntegerField(primary_key=True, db_column="GrantID", validators=[validate_positive,])
grant_id = models.PositiveIntegerField(primary_key=True, db_column="GrantID", validators=[validate_positive])
title = models.CharField(db_column="GrantTitle", max_length=250, null=True)
currency = models.CharField(db_column="Currency", max_length=3, null=True)
amount = models.DecimalField(db_column="Amount", max_digits=20, decimal_places=4, null=True)
Expand All @@ -193,15 +205,15 @@ class Grant(models.Model):
funding_probability = models.CharField(db_column="FundingProbability", max_length=255, null=True)
complex_program = models.BooleanField(db_column="ComplexProgram")
sensitive_data = models.BooleanField(db_column="SensitiveData")
project_length = models.PositiveIntegerField(db_column="ProjectLength", validators=[validate_positive,], null=True)
project_length = models.PositiveIntegerField(db_column="ProjectLength", validators=[validate_positive], null=True)
created = models.DateField(db_column="CreationDate", null=True)
updated = models.DateField(db_column="LastModifiedDate", null=True)
sectors = models.ManyToManyField(Sector, through="GrantSector")
subsectors = models.ManyToManyField(SubSector, through='GrantSector')
themes = models.ManyToManyField(Theme, through='GrantTheme')
methodologies = models.ManyToManyField(Methodology, through='GrantMethodology')
countries = models.ManyToManyField(Country, through="GrantCountry", related_name='grants')
objects = models.Manager() # The default manager.
objects = models.Manager()
won_grants = GrantsWonManager()
lost_grants = GrantsLostManager()
both_won_n_loss_grants = BothWonLostGrants()
Expand All @@ -214,7 +226,7 @@ def __str__(self):
class Meta:
managed = False
db_table = "granttbl"
#ordering = ['title',]


class GrantTheme(models.Model):
grant = models.ForeignKey(Grant, db_column="GrantID", on_delete=models.CASCADE)
Expand All @@ -225,6 +237,7 @@ class Meta:
db_table = 'n_grantthemetbl'
unique_together = (('grant', 'theme'),)


class GrantMethodology(models.Model):
grant = models.ForeignKey(Grant, db_column="GrantID", on_delete=models.CASCADE)
methodology = models.ForeignKey(Methodology, db_column="MethodologyID", on_delete=models.CASCADE)
Expand Down Expand Up @@ -254,5 +267,3 @@ class Meta:
managed = False
db_table = 'grantcountrytbl'
unique_together = (("grant", "country"),)


2 changes: 1 addition & 1 deletion dataviz/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework import serializers
from .models import *

locale.setlocale(locale.LC_ALL, 'en_US.utf8')
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

class GrantSerializerPlain(serializers.ModelSerializer):
gait_id = serializers.SerializerMethodField("get_grant_id")
Expand Down

0 comments on commit 5850196

Please sign in to comment.