240 lines
11 KiB
Python
240 lines
11 KiB
Python
import uuid
|
|
from urllib.parse import urlsplit
|
|
from django.conf import settings
|
|
from django.core.validators import RegexValidator
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import models
|
|
from django.db.models import Q
|
|
|
|
|
|
class Profile(models.Model):
|
|
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
display_name = models.CharField(max_length=150, blank=True)
|
|
avatar_url = models.URLField(blank=True)
|
|
timezone = models.CharField(max_length=64, default='Europe/Amsterdam')
|
|
language = models.CharField(max_length=16, default='en')
|
|
theme = models.CharField(max_length=16, default='system')
|
|
|
|
|
|
class OAuthIdentity(models.Model):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='oauth_identities')
|
|
issuer = models.URLField()
|
|
subject = models.CharField(max_length=255)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['issuer', 'subject'], name='unique_oidc_identity')]
|
|
|
|
|
|
class Project(models.Model):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='owned_projects')
|
|
name = models.CharField(max_length=200)
|
|
slug = models.SlugField(max_length=220)
|
|
description = models.TextField(blank=True)
|
|
git_repository_url = models.URLField(blank=True)
|
|
archived = models.BooleanField(default=False)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['owner', 'slug'], name='unique_owner_project_slug'), models.UniqueConstraint(fields=['owner', 'name'], name='unique_owner_project_name')]
|
|
|
|
def clean(self):
|
|
if self.git_repository_url:
|
|
try:
|
|
parsed = urlsplit(self.git_repository_url)
|
|
_ = parsed.port
|
|
valid = parsed.scheme == 'https' and bool(parsed.hostname)
|
|
except ValueError:
|
|
valid = False
|
|
parsed = None
|
|
if (not valid or parsed.username or parsed.password or parsed.query or parsed.fragment or
|
|
'\\' in self.git_repository_url or any(c.isspace() for c in self.git_repository_url)):
|
|
raise ValidationError({'git_repository_url': 'Use an HTTPS Git URL without credentials or query parameters.'})
|
|
|
|
|
|
class DocumentationSync(models.Model):
|
|
project = models.OneToOneField(Project, on_delete=models.CASCADE, related_name='documentation_sync')
|
|
branch = models.CharField(max_length=255, blank=True)
|
|
indexed_repository_url = models.URLField(blank=True)
|
|
indexed_commit = models.CharField(max_length=64, blank=True)
|
|
remote_head = models.CharField(max_length=64, blank=True)
|
|
commit_time = models.DateTimeField(null=True, blank=True)
|
|
last_success_at = models.DateTimeField(null=True, blank=True)
|
|
last_attempt_at = models.DateTimeField(null=True, blank=True)
|
|
last_error = models.CharField(max_length=300, blank=True)
|
|
sync_token = models.CharField(max_length=36, blank=True)
|
|
sync_started_at = models.DateTimeField(null=True, blank=True)
|
|
audit_state = models.JSONField(default=dict, blank=True)
|
|
|
|
|
|
class ProjectDocument(models.Model):
|
|
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='documents')
|
|
path = models.CharField(max_length=500)
|
|
title = models.CharField(max_length=255)
|
|
headings = models.JSONField(default=list)
|
|
markdown = models.TextField()
|
|
content_hash = models.CharField(max_length=64)
|
|
source_commit = models.CharField(max_length=64)
|
|
|
|
class Meta:
|
|
ordering = ['path']
|
|
constraints = [models.UniqueConstraint(fields=['project', 'path'], name='unique_project_document_path')]
|
|
indexes = [models.Index(fields=['project', 'path'])]
|
|
|
|
|
|
class Membership(models.Model):
|
|
ROLE = [('editor', 'Editor'), ('viewer', 'Viewer')]
|
|
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='memberships')
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
role = models.CharField(max_length=10, choices=ROLE)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['project', 'user'], name='unique_project_member')]
|
|
|
|
|
|
class Board(models.Model):
|
|
project = models.OneToOneField(Project, on_delete=models.CASCADE, related_name='board')
|
|
|
|
|
|
class Column(models.Model):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='columns')
|
|
name = models.CharField(max_length=100)
|
|
color = models.CharField(max_length=7, default='#315d4d', validators=[RegexValidator(r'^#[0-9a-fA-F]{6}$')])
|
|
position = models.PositiveIntegerField(default=0)
|
|
is_default = models.BooleanField(default=False)
|
|
is_done = models.BooleanField(default=False)
|
|
archived = models.BooleanField(default=False)
|
|
|
|
class Meta:
|
|
ordering = ['position', 'id']
|
|
constraints = [models.UniqueConstraint(fields=['board'], condition=Q(is_default=True, archived=False), name='one_active_default_column')]
|
|
|
|
|
|
class Tag(models.Model):
|
|
project = models.ForeignKey(Project, on_delete=models.CASCADE)
|
|
name = models.CharField(max_length=60)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['project', 'name'], name='unique_project_tag')]
|
|
|
|
|
|
class Card(models.Model):
|
|
PRIORITY = [('low', 'Low'), ('medium', 'Medium'), ('high', 'High')]
|
|
TYPE = [(x, x.title()) for x in ('feature', 'bug', 'task', 'research', 'idea')]
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='cards')
|
|
column = models.ForeignKey(Column, on_delete=models.PROTECT, related_name='cards')
|
|
title = models.CharField(max_length=250)
|
|
description_markdown = models.TextField(blank=True)
|
|
priority = models.CharField(max_length=6, choices=PRIORITY, default='medium')
|
|
card_type = models.CharField(max_length=8, choices=TYPE, default='task')
|
|
tags = models.ManyToManyField(Tag, blank=True)
|
|
assignee = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name='assigned_cards')
|
|
due_date = models.DateField(null=True, blank=True)
|
|
position = models.PositiveIntegerField(default=0)
|
|
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name='created_cards')
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
completed_at = models.DateTimeField(null=True, blank=True)
|
|
archived = models.BooleanField(default=False)
|
|
revision = models.PositiveIntegerField(default=1)
|
|
is_blocked = models.BooleanField(default=False)
|
|
blocked_reason = models.TextField(blank=True)
|
|
|
|
class Meta:
|
|
ordering = ['position', 'created_at']
|
|
indexes = [models.Index(fields=['project', 'archived', 'priority']), models.Index(fields=['column', 'position'])]
|
|
|
|
|
|
class CardRevision(models.Model):
|
|
card = models.ForeignKey(Card, on_delete=models.CASCADE, related_name='revisions')
|
|
revision = models.PositiveIntegerField()
|
|
markdown = models.TextField()
|
|
actor = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['card', 'revision'], name='unique_card_revision')]
|
|
|
|
|
|
class CardComment(models.Model):
|
|
card = models.ForeignKey(Card, on_delete=models.CASCADE, related_name='comments')
|
|
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
|
|
body = models.TextField()
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
class CardAttachment(models.Model):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
card = models.ForeignKey(Card, on_delete=models.CASCADE, related_name='attachments')
|
|
comment = models.ForeignKey(CardComment, null=True, blank=True, on_delete=models.CASCADE, related_name='attachments')
|
|
file = models.FileField(upload_to='card-attachments/%Y/%m/')
|
|
original_filename = models.CharField(max_length=255)
|
|
title = models.CharField(max_length=255)
|
|
mime_type = models.CharField(max_length=100)
|
|
byte_size = models.PositiveIntegerField()
|
|
sha256 = models.CharField(max_length=64)
|
|
description = models.TextField(blank=True)
|
|
uploaded_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
|
|
source = models.CharField(max_length=16, default='web')
|
|
source_path = models.CharField(max_length=500, blank=True)
|
|
source_commit = models.CharField(max_length=64, blank=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ['created_at', 'id']
|
|
indexes = [models.Index(fields=['card', 'comment', 'created_at'])]
|
|
|
|
def clean(self):
|
|
if self.comment_id and not CardComment.objects.filter(pk=self.comment_id, card_id=self.card_id).exists():
|
|
raise ValidationError({'comment': 'Progress note must belong to this card.'})
|
|
|
|
|
|
class CreationKey(models.Model):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
key = models.CharField(max_length=128)
|
|
payload_hash = models.CharField(max_length=64)
|
|
card = models.ForeignKey(Card, on_delete=models.CASCADE)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['user', 'key'], name='unique_card_creation_key')]
|
|
|
|
|
|
class BulkUpdateKey(models.Model):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
key = models.CharField(max_length=128)
|
|
payload_hash = models.CharField(max_length=64)
|
|
result = models.JSONField()
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['user', 'key'], name='unique_bulk_update_key')]
|
|
|
|
|
|
class ProjectMoveKey(models.Model):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
key = models.CharField(max_length=128)
|
|
payload_hash = models.CharField(max_length=64)
|
|
result = models.JSONField()
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [models.UniqueConstraint(fields=['user', 'key'], name='unique_project_move_key')]
|
|
|
|
|
|
class Activity(models.Model):
|
|
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='activity')
|
|
card = models.ForeignKey(Card, null=True, blank=True, on_delete=models.CASCADE, related_name='activity')
|
|
actor = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
|
|
action = models.CharField(max_length=60)
|
|
detail = models.JSONField(default=dict)
|
|
origin = models.CharField(max_length=8, default='web')
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ['-created_at']
|