feat(time_entries): add time_entries app's basic structure and endpoints

This commit is contained in:
2026-03-11 19:46:45 +08:00
parent 4d66293804
commit 720adbe8a3
11 changed files with 490 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
from django.core.exceptions import ValidationError
from django.conf import settings
from django.db import models
from django.db.models import Q
from core.models.base import BaseModel
from apps.workspaces.models import Workspace
from apps.projects.models import Project
from apps.tags.models import Tag
User = settings.AUTH_USER_MODEL
class TimeEntry(BaseModel):
workspace = models.ForeignKey(
Workspace,
on_delete=models.CASCADE,
related_name="time_entries",
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name="time_entries",
)
project = models.ForeignKey(
Project,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="time_entries",
)
description = models.TextField(blank=True)
start_time = models.DateTimeField()
end_time = models.DateTimeField(null=True, blank=True)
duration = models.DurationField(null=True, blank=True)
tags = models.ManyToManyField(
Tag,
blank=True,
related_name="time_entries",
)
is_billable = models.BooleanField(default=False)
hourly_rate = models.DecimalField(
max_digits=10,
decimal_places=2,
null=True,
blank=True,
)
currency = models.CharField(
max_length=3,
default="USD",
)
class Meta:
db_table = "time_entry"
ordering = ("-updated_at", "-created_at")
indexes = [
models.Index(fields=["workspace"], name="time_entry_workspace_idx"),
models.Index(fields=["user"], name="time_entry_user_idx"),
models.Index(fields=["project"], name="time_entry_project_idx"),
models.Index(fields=["start_time"], name="time_entry_start_idx"),
models.Index(fields=["workspace", "start_time"], name="time_entry_workspace_start_idx"),
]
constraints = [
models.UniqueConstraint(
fields=["workspace", "user"],
condition=Q(end_time__isnull=True, is_deleted=False),
name="unique_running_timer_per_user",
)
]
def __str__(self):
return f"{self.user} - {self.start_time}"
def clean(self):
if self.project and self.project.workspace_id != self.workspace_id:
raise ValidationError("Project must belong to the same workspace.")
for tag in self.tags.all():
if tag.workspace_id != self.workspace_id:
raise ValidationError("Tags must belong to the same workspace.")