Skip to content

Commit

Permalink
feature: new model for custom status
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJaeger committed Jan 31, 2025
1 parent cdeb223 commit f101a8a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
41 changes: 41 additions & 0 deletions chats/apps/projects/migrations/0021_customstatus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 4.1.2 on 2025-01-31 19:57

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("projects", "0020_project_org"),
]

operations = [
migrations.CreateModel(
name="CustomStatus",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
("is_deleted", models.BooleanField(default=False)),
(
"project",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="custom_statuses",
to="projects.project",
),
),
],
options={
"unique_together": {("name", "project")},
},
),
]
35 changes: 35 additions & 0 deletions chats/apps/projects/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

from .permission_managers import UserPermissionsManager

from django.core.exceptions import ValidationError
from django.db import transaction

User = get_user_model()

# Create your models here.
Expand Down Expand Up @@ -500,3 +503,35 @@ def __str__(self):
@property
def project(self):
return self.flow_start.project


class CustomStatus(models.Model):
name = models.CharField(max_length=255)
project = models.ForeignKey(
"Project", on_delete=models.CASCADE, related_name="custom_statuses"
)
is_deleted = models.BooleanField(default=False)

def delete(self, *args, **kwargs):
self.is_deleted = True
self.save(update_fields=["is_deleted"])

def save(self, *args, **kwargs):
if not self.pk:
with transaction.atomic():
existing_count = (
CustomStatus.objects.select_for_update()
.filter(project=self.project, is_deleted=False)
.count()
)
if existing_count >= 3:
raise ValidationError(
"A project can have a maximum of 3 custom statuses."
)
super().save(*args, **kwargs)

def __str__(self):
return self.name

class Meta:
unique_together = ("name", "project")

0 comments on commit f101a8a

Please sign in to comment.