From 16eb034b03e14ae77d9d3431dd55e69bccf851b5 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 23 Mar 2021 17:11:11 -0400 Subject: [PATCH] Add Member table from membershipworks database --- member_paperwork/settings/base.py | 2 +- paperwork/admin.py | 17 +++++++++++++++-- paperwork/models.py | 19 ++++++++++++++++++- paperwork/routers.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/member_paperwork/settings/base.py b/member_paperwork/settings/base.py index af22ebc..33a4ef5 100644 --- a/member_paperwork/settings/base.py +++ b/member_paperwork/settings/base.py @@ -62,7 +62,7 @@ TEMPLATES = [ WSGI_APPLICATION = 'member_paperwork.wsgi.application' -DATABASE_ROUTERS = ['paperwork.routers.PaperworkRouter'] +DATABASE_ROUTERS = ['paperwork.routers.MembershipWorksRouter', 'paperwork.routers.PaperworkRouter'] # Password validation diff --git a/paperwork/admin.py b/paperwork/admin.py index 2658089..d43ec70 100644 --- a/paperwork/admin.py +++ b/paperwork/admin.py @@ -1,7 +1,9 @@ from django.contrib import admin -from .models import (CmsRedRiverVeteransScholarship, CertificationDefinition, - Certification, InstructorOrVendor, SpecialProgram, Waiver) +from .models import (CmsRedRiverVeteransScholarship, + CertificationDefinition, Certification, + InstructorOrVendor, SpecialProgram, Waiver, + Member) class CertificationDefinitionAdmin(admin.ModelAdmin): @@ -24,9 +26,20 @@ class WaiverAdmin(admin.ModelAdmin): search_fields = ['name'] +class MemberAdmin(admin.ModelAdmin): + readonly_fields = [field.name for field in Member._meta.get_fields() if field.concrete] + + def has_add_permission(self, request, obj=None): + return False + + def has_delete_permission(self, request, obj=None): + return False + + admin.site.register(CmsRedRiverVeteransScholarship) admin.site.register(CertificationDefinition, CertificationDefinitionAdmin) admin.site.register(Certification, CertificationAdmin) admin.site.register(InstructorOrVendor, InstructorOrVendorAdmin) admin.site.register(SpecialProgram, SpecialProgramAdmin) admin.site.register(Waiver, WaiverAdmin) +admin.site.register(Member, MemberAdmin) diff --git a/paperwork/models.py b/paperwork/models.py index 35ec46d..9b421ce 100644 --- a/paperwork/models.py +++ b/paperwork/models.py @@ -1,6 +1,22 @@ from django.db import models +class Member(models.Model): + # TODO: this is only a partial set of fields, may want to expand + uid = models.CharField(primary_key=True, max_length=24) + year_of_birth = models.TextField(db_column='Year of Birth', blank=True, null=True) + account_name = models.TextField(db_column='Account Name', blank=True, null=True) + first_name = models.TextField(db_column='First Name', blank=True, null=True) + last_name = models.TextField(db_column='Last Name', blank=True, null=True) + + def __str__(self): + return f"{self.account_name}" + + class Meta: + managed = False + db_table = 'members' + + class CmsRedRiverVeteransScholarship(models.Model): serial = models.AutoField(primary_key=True) program_name = models.CharField(db_column='Program Name', max_length=255) @@ -38,7 +54,8 @@ class Certification(models.Model): number = models.AutoField(db_column='Number', primary_key=True) certification = models.ForeignKey(CertificationDefinition, on_delete=models.PROTECT, db_column='Certification') name = models.CharField(db_column='Name', max_length=255) - uid = models.CharField(max_length=24, blank=True, null=True) + # TODO: this should be a fk to the membershipworks.members table + member = models.ForeignKey(Member, on_delete=models.PROTECT, to_field='uid', db_column='uid', blank=True, null=True) certified_by = models.CharField(db_column='Certified_By', max_length=255, blank=True, null=True) date = models.DateField(db_column='Date', blank=True, null=True) version = models.CharField(db_column='Version', max_length=255, blank=True, null=True) diff --git a/paperwork/routers.py b/paperwork/routers.py index 9c13406..e532f1f 100644 --- a/paperwork/routers.py +++ b/paperwork/routers.py @@ -1,3 +1,6 @@ +from .models import Member + + class PaperworkRouter: app_label = 'paperwork' db = 'cms' @@ -12,6 +15,32 @@ class PaperworkRouter: return self.db return None + def allow_relation(self, obj1, obj2, **hints): + if (obj1 == Member and obj2._meta.app_label == self.app_label) or \ + (obj2 == Member and obj1._meta.app_label == self.app_label): + return True + return None + + def allow_migrate(self, db, app_label, model_name=None, **hints): + if db == self.db: + return False + return None + + +# TODO: this is pretty hacky/special cased +class MembershipWorksRouter: + db = 'membershipworks' + + def db_for_read(self, model, **hints): + if model == Member: + return self.db + return None + + def db_for_write(self, model, **hints): + if model == Member: + return self.db + return None + def allow_relation(self, obj1, obj2, **hints): return None