frogress/frog_api/models.py

105 lines
3.2 KiB
Python
Raw Normal View History

from datetime import datetime
2022-08-19 10:35:05 +00:00
from django.db import models
2022-08-24 01:38:09 +00:00
from django.utils.crypto import get_random_string
AUTH_KEY_LEN = 10
def gen_auth_key() -> str:
ret = get_random_string(length=AUTH_KEY_LEN)
if Project.objects.filter(auth_key=ret).exists():
return gen_auth_key()
return ret
2022-08-19 10:35:05 +00:00
2022-08-20 03:33:23 +00:00
# Example: OOT
2022-08-19 10:35:05 +00:00
class Project(models.Model):
id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=255, unique=True)
name = models.CharField(max_length=255)
2022-08-24 01:38:09 +00:00
auth_key = models.CharField(max_length=AUTH_KEY_LEN, default=gen_auth_key)
repository = models.URLField(blank=True)
discord = models.URLField(blank=True)
website = models.URLField(blank=True)
2022-08-19 10:35:05 +00:00
2022-08-20 03:33:23 +00:00
def __str__(self) -> str:
return self.slug
2022-08-19 10:35:05 +00:00
2022-08-20 03:33:23 +00:00
# Example: US 1.0
2022-08-19 10:35:05 +00:00
class Version(models.Model):
id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
2022-08-21 11:25:44 +00:00
project = models.ForeignKey(
Project, on_delete=models.CASCADE, related_name="versions"
)
2022-08-19 10:35:05 +00:00
slug = models.SlugField(max_length=255)
name = models.CharField(max_length=255)
2022-08-20 03:33:23 +00:00
def __str__(self) -> str:
return f"{self.project} {self.slug}"
2022-08-19 10:35:05 +00:00
2022-08-20 03:33:23 +00:00
# Example: Actors
class Category(models.Model):
2022-08-19 10:35:05 +00:00
id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
version = models.ForeignKey(Version, on_delete=models.CASCADE)
2022-08-20 03:33:23 +00:00
slug = models.SlugField(max_length=255)
name = models.CharField(max_length=255)
class Meta:
verbose_name_plural = "Categories"
def __str__(self) -> str:
return f"{self.version} {self.slug}"
# A snapshot in time of progress, tied to a Category
class Entry(models.Model):
id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
timestamp = models.IntegerField()
2022-08-19 10:35:05 +00:00
git_hash = models.CharField(max_length=40)
class Meta:
2022-08-20 03:33:23 +00:00
verbose_name_plural = "Entries"
2022-08-19 10:35:05 +00:00
ordering = ["-timestamp"]
constraints = [
models.UniqueConstraint(
fields=["timestamp", "git_hash", "category"], name="unique entry"
)
]
2022-08-20 03:33:23 +00:00
def __str__(self) -> str:
time_string = datetime.utcfromtimestamp(self.timestamp).strftime(
"%Y-%m-%d %H:%M:%S"
)
return f"{self.category} {time_string}"
2022-08-22 02:01:56 +00:00
# A measure (total bytes, bytes matched, functions matched, bytes decompiled, etc) tied to an Entry
class Measure(models.Model):
id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
entry = models.ForeignKey(Entry, on_delete=models.CASCADE, related_name="measures")
type = models.CharField(max_length=255)
value = models.IntegerField()
def __str__(self) -> str:
return f"{self.entry} {self.type}: {self.value}"