2023-04-10 13:12:45 -04:00
|
|
|
import datetime
|
2022-11-07 13:49:39 -05:00
|
|
|
import re
|
2023-04-10 14:29:12 -04:00
|
|
|
from typing import TypedDict, TYPE_CHECKING, Optional
|
2023-01-18 21:29:36 -05:00
|
|
|
|
2023-01-31 13:16:36 -05:00
|
|
|
from semver import VersionInfo
|
2023-01-18 21:29:36 -05:00
|
|
|
from django.db import models
|
2023-12-01 13:05:27 -05:00
|
|
|
from django.db.models import Q, Window
|
|
|
|
from django.db.models.functions import FirstValue
|
2023-04-10 13:12:45 -04:00
|
|
|
from django.conf import settings
|
2023-01-18 21:29:36 -05:00
|
|
|
from django.core.validators import RegexValidator
|
2023-04-10 14:29:12 -04:00
|
|
|
from django_stubs_ext import WithAnnotations
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-01-18 21:29:36 -05:00
|
|
|
from membershipworks.models import Member, Flag as MembershipWorksFlag
|
2021-03-23 17:11:11 -04:00
|
|
|
|
|
|
|
|
2023-04-10 13:12:45 -04:00
|
|
|
class AbstractAudit(models.Model):
|
|
|
|
date = models.DateField(default=datetime.date.today, unique=True)
|
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
|
|
|
|
good = models.BooleanField(default=False)
|
|
|
|
notes = models.CharField(max_length=255, blank=True)
|
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2023-04-10 13:12:45 -04:00
|
|
|
return f"{'Good' if self.good else 'Bad'} audit at {self.date} by {self.user}"
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ["date"]
|
|
|
|
get_latest_by = ["date"]
|
|
|
|
|
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
class CmsRedRiverVeteransScholarship(models.Model):
|
|
|
|
serial = models.AutoField(primary_key=True)
|
2022-02-11 13:48:47 -05:00
|
|
|
program_name = models.CharField(db_column="Program Name", max_length=255)
|
|
|
|
member_name = models.CharField(
|
|
|
|
db_column="Member Name", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
discount_percent = models.DecimalField(
|
|
|
|
db_column="Discount Percent",
|
|
|
|
max_digits=16,
|
|
|
|
decimal_places=0,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
|
|
|
discount_code = models.CharField(
|
|
|
|
db_column="Discount Code", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
membership_code = models.CharField(
|
|
|
|
db_column="Membership Code", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
start_date = models.DateField(db_column="Start Date", blank=True, null=True)
|
|
|
|
end_date = models.DateField(db_column="End Date", blank=True, null=True)
|
|
|
|
program_amount = models.DecimalField(
|
|
|
|
db_column="Program Amount",
|
|
|
|
max_digits=16,
|
|
|
|
decimal_places=0,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
|
|
|
program_status = models.CharField(
|
|
|
|
db_column="Program Status", max_length=16, blank=True, null=True
|
|
|
|
)
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2021-03-23 17:08:00 -04:00
|
|
|
return f"{self.program_name} {self.member_name}"
|
|
|
|
|
|
|
|
class Meta:
|
2022-02-11 13:48:47 -05:00
|
|
|
db_table = "CMS Red River Veterans Scholarship"
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
2023-04-25 23:23:27 -04:00
|
|
|
class DepartmentQuerySet(models.QuerySet):
|
|
|
|
def filter_by_shop_lead(self, member: Member) -> models.QuerySet["Department"]:
|
|
|
|
"""Get departments for which `member` is a shop lead"""
|
|
|
|
# TODO: could be a lot simpler if membershipworks was in the same database
|
|
|
|
# TODO: should also select children
|
|
|
|
member_flags = list(member.flags.all().values_list("pk", flat=True))
|
|
|
|
return self.prefetch_related("shop_lead_flag__members").filter(
|
|
|
|
shop_lead_flag__in=member_flags
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-18 21:29:36 -05:00
|
|
|
class Department(models.Model):
|
2023-04-25 23:23:27 -04:00
|
|
|
objects = DepartmentQuerySet.as_manager()
|
|
|
|
|
2023-01-18 21:29:36 -05:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=64,
|
|
|
|
validators=[RegexValidator("^[-_ A-Za-z0-9]*$")],
|
|
|
|
help_text="This will also be used to generate the mailing list name",
|
|
|
|
)
|
|
|
|
parent = models.ForeignKey(
|
|
|
|
"self", on_delete=models.PROTECT, related_name="children", blank=True, null=True
|
|
|
|
)
|
2023-01-19 16:02:17 -05:00
|
|
|
has_mailing_list = models.BooleanField(default=False)
|
2023-01-23 21:06:06 -05:00
|
|
|
shop_lead_flag = models.ForeignKey(
|
2023-01-18 21:29:36 -05:00
|
|
|
MembershipWorksFlag,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
db_constraint=False,
|
2023-01-23 21:06:06 -05:00
|
|
|
help_text="This will also be used to set the moderators for the mailing list",
|
2023-01-18 21:29:36 -05:00
|
|
|
)
|
|
|
|
list_reply_to_address = models.EmailField(max_length=254, blank=True)
|
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2023-01-18 21:29:36 -05:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
@property
|
2023-04-10 14:29:12 -04:00
|
|
|
def list_name(self) -> Optional[str]:
|
2023-02-02 14:51:39 -05:00
|
|
|
if self.has_mailing_list:
|
|
|
|
return self.name.replace(" ", "_") + "-info"
|
|
|
|
else:
|
|
|
|
return None
|
2023-01-18 21:29:36 -05:00
|
|
|
|
2023-02-02 15:08:48 -05:00
|
|
|
@property
|
2023-04-10 14:29:12 -04:00
|
|
|
def list_address(self) -> Optional[str]:
|
|
|
|
if self.list_name:
|
2023-02-02 15:08:48 -05:00
|
|
|
return self.list_name + "@claremontmakerspace.org"
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2023-01-18 21:29:36 -05:00
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
class CertificationDefinition(models.Model):
|
2023-04-10 14:51:43 -04:00
|
|
|
name = models.CharField(max_length=255)
|
2023-01-18 21:29:36 -05:00
|
|
|
department = models.ForeignKey(Department, models.PROTECT)
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2023-04-10 14:51:43 -04:00
|
|
|
return f"{self.name} <{self.department}>"
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
class Meta:
|
2022-02-11 13:48:47 -05:00
|
|
|
db_table = "Certification Definitions"
|
2023-04-10 14:51:43 -04:00
|
|
|
ordering = ("name", "department")
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-11-07 13:49:39 -05:00
|
|
|
def latest_version(self) -> "CertificationVersion":
|
2023-02-01 00:11:28 -05:00
|
|
|
return self.certificationversion_set.latest()
|
|
|
|
|
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
class CertificationVersionAnnotations(TypedDict):
|
|
|
|
is_latest: bool
|
|
|
|
is_current: bool
|
|
|
|
|
|
|
|
|
|
|
|
class CertificationVersionManager(models.Manager["CertificationVersion"]):
|
|
|
|
def get_queryset(self) -> models.QuerySet["CertificationVersion"]:
|
2023-12-01 13:05:27 -05:00
|
|
|
window = {
|
|
|
|
"partition_by": "definition",
|
|
|
|
"order_by": [
|
|
|
|
"-major",
|
|
|
|
"-minor",
|
|
|
|
"-patch",
|
|
|
|
"-prerelease",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
super()
|
|
|
|
.get_queryset()
|
|
|
|
.annotate(
|
|
|
|
is_latest=Q(pk=Window(FirstValue("pk"), **window)),
|
|
|
|
# TODO: should do a more correct comparison than just major version
|
|
|
|
is_current=Q(major=Window(FirstValue("major"), **window)),
|
|
|
|
)
|
2023-02-01 00:11:28 -05:00
|
|
|
)
|
2022-11-07 13:49:39 -05:00
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-02-04 16:30:52 -05:00
|
|
|
class CertificationVersion(models.Model):
|
2023-02-01 00:11:28 -05:00
|
|
|
objects = CertificationVersionManager()
|
|
|
|
|
2023-04-10 14:51:43 -04:00
|
|
|
definition = models.ForeignKey(CertificationDefinition, on_delete=models.PROTECT)
|
2023-01-31 18:53:36 -05:00
|
|
|
major = models.PositiveSmallIntegerField()
|
|
|
|
minor = models.PositiveSmallIntegerField()
|
|
|
|
patch = models.PositiveSmallIntegerField()
|
|
|
|
prerelease = models.CharField(max_length=255, blank=True)
|
|
|
|
approval_date = models.DateField(blank=True, null=True)
|
2022-02-04 16:30:52 -05:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2023-01-31 18:53:36 -05:00
|
|
|
return f"{self.definition} [{self.semantic_version()}]"
|
2022-02-04 16:30:52 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
2023-01-31 18:53:36 -05:00
|
|
|
fields=["definition", "major", "minor", "patch", "prerelease"],
|
|
|
|
name="unique_certification_version",
|
2022-02-04 16:30:52 -05:00
|
|
|
)
|
|
|
|
]
|
2023-03-30 12:27:51 -04:00
|
|
|
ordering = (
|
|
|
|
"definition",
|
|
|
|
"major",
|
|
|
|
"minor",
|
|
|
|
"patch",
|
|
|
|
"prerelease",
|
|
|
|
"approval_date",
|
|
|
|
)
|
2023-02-01 00:11:28 -05:00
|
|
|
get_latest_by = ("major", "minor", "patch", "prerelease", "approval_date")
|
|
|
|
base_manager_name = "objects"
|
2022-02-04 16:30:52 -05:00
|
|
|
|
2023-01-31 13:16:36 -05:00
|
|
|
def semantic_version(self) -> VersionInfo:
|
2023-01-31 18:53:36 -05:00
|
|
|
return VersionInfo(
|
|
|
|
self.major or 0,
|
|
|
|
self.minor or 0,
|
|
|
|
self.patch or 0,
|
|
|
|
re.sub(r"[^.a-zA-Z0-9]", "-", self.prerelease),
|
|
|
|
self.approval_date.isoformat() if self.approval_date is not None else None,
|
|
|
|
)
|
2022-11-07 13:49:39 -05:00
|
|
|
|
2022-02-04 16:30:52 -05:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
CertificationVersionAnnotated = WithAnnotations[
|
|
|
|
CertificationVersion, CertificationVersionAnnotations
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
CertificationVersionAnnotated = WithAnnotations[CertificationVersion]
|
|
|
|
|
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
class Certification(models.Model):
|
2023-04-10 14:51:43 -04:00
|
|
|
number = models.AutoField(primary_key=True)
|
2022-02-11 13:48:47 -05:00
|
|
|
certification_version = models.ForeignKey(
|
|
|
|
CertificationVersion, on_delete=models.PROTECT
|
|
|
|
)
|
2023-04-10 14:51:43 -04:00
|
|
|
name = models.CharField(max_length=255)
|
2022-02-11 13:48:47 -05:00
|
|
|
member = models.ForeignKey(
|
|
|
|
Member,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
to_field="uid",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
db_constraint=False,
|
|
|
|
)
|
2023-04-10 14:51:43 -04:00
|
|
|
certified_by = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
date = models.DateField(blank=True, null=True)
|
|
|
|
shop_lead_notified = models.DateTimeField(blank=True, null=True)
|
|
|
|
notes = models.CharField(max_length=255, blank=True, null=True)
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2022-02-04 16:30:52 -05:00
|
|
|
return f"{self.name} - {self.certification_version}"
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
class Meta:
|
2022-02-11 13:48:47 -05:00
|
|
|
db_table = "Certifications"
|
2022-02-28 16:36:29 -05:00
|
|
|
permissions = [
|
|
|
|
(
|
|
|
|
"receive_certification_emails",
|
|
|
|
"Receives notifications of all new certifications",
|
|
|
|
),
|
|
|
|
]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
2023-04-10 13:12:45 -04:00
|
|
|
class CertificationAudit(AbstractAudit):
|
|
|
|
certification = models.ForeignKey(
|
|
|
|
Certification, on_delete=models.CASCADE, related_name="audits"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
class InstructorOrVendor(models.Model):
|
|
|
|
serial = models.AutoField(primary_key=True)
|
2022-02-11 13:48:47 -05:00
|
|
|
name = models.CharField(db_column="Name", max_length=255)
|
|
|
|
instructor_agreement_date = models.DateField(
|
|
|
|
db_column="Instructor Agreement Date", blank=True, null=True
|
|
|
|
)
|
|
|
|
w9_date = models.DateField(db_column="W9 date", blank=True, null=True)
|
2021-03-23 17:08:00 -04:00
|
|
|
phone = models.CharField(max_length=255, blank=True, null=True)
|
2022-02-11 13:48:47 -05:00
|
|
|
email_address = models.CharField(
|
|
|
|
db_column="email address", max_length=255, blank=True, null=True
|
|
|
|
)
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2021-03-23 17:08:00 -04:00
|
|
|
return f"{self.name}"
|
|
|
|
|
|
|
|
class Meta:
|
2022-02-11 13:48:47 -05:00
|
|
|
db_table = "Instructors and Vendors"
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SpecialProgram(models.Model):
|
2022-02-11 13:48:47 -05:00
|
|
|
program_name = models.CharField(
|
|
|
|
db_column="Program Name", primary_key=True, max_length=255
|
|
|
|
)
|
|
|
|
discount_percent = models.DecimalField(
|
|
|
|
db_column="Discount Percent",
|
|
|
|
max_digits=16,
|
|
|
|
decimal_places=0,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
|
|
|
discount_code = models.CharField(
|
|
|
|
db_column="Discount Code", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
membership_code = models.CharField(
|
|
|
|
db_column="Membership Code", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
start_date = models.DateField(db_column="Start Date", blank=True, null=True)
|
|
|
|
end_date = models.DateField(db_column="End Date", blank=True, null=True)
|
|
|
|
program_amount = models.DecimalField(
|
|
|
|
db_column="Program Amount",
|
|
|
|
max_digits=16,
|
|
|
|
decimal_places=0,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
|
|
|
program_status = models.CharField(
|
|
|
|
db_column="Program Status", max_length=16, blank=True, null=True
|
|
|
|
)
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2021-03-23 17:08:00 -04:00
|
|
|
return self.program_name
|
|
|
|
|
|
|
|
class Meta:
|
2022-02-11 13:48:47 -05:00
|
|
|
db_table = "Special_Programs"
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Waiver(models.Model):
|
2022-02-11 13:48:47 -05:00
|
|
|
number = models.AutoField(db_column="Number", primary_key=True)
|
|
|
|
name = models.CharField(db_column="Name", max_length=255)
|
|
|
|
date = models.DateField(db_column="Date")
|
|
|
|
emergency_contact_name = models.CharField(
|
|
|
|
db_column="Emergency Contact Name", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
emergency_contact_number = models.CharField(
|
|
|
|
db_column="Emergency Contact Number", max_length=25, blank=True, null=True
|
|
|
|
)
|
|
|
|
waiver_version = models.CharField(db_column="Waiver version", max_length=64)
|
|
|
|
guardian_name = models.CharField(
|
|
|
|
db_column="Guardian Name", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
guardian_relation = models.CharField(
|
|
|
|
db_column="Guardian Relation", max_length=255, blank=True, null=True
|
|
|
|
)
|
|
|
|
guardian_date = models.DateField(db_column="Guardian Date", blank=True, null=True)
|
2023-04-10 21:37:07 -04:00
|
|
|
notes = models.CharField(max_length=255, blank=True, null=True)
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2023-04-10 14:29:12 -04:00
|
|
|
def __str__(self) -> str:
|
2021-03-23 17:08:00 -04:00
|
|
|
return f"{self.name} {self.date}"
|
|
|
|
|
|
|
|
class Meta:
|
2022-02-11 13:48:47 -05:00
|
|
|
db_table = "Waivers"
|
2023-04-10 13:12:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
class WaiverAudit(AbstractAudit):
|
|
|
|
waiver = models.ForeignKey(Waiver, on_delete=models.CASCADE, related_name="audits")
|