This commit refactors the models in the 'models' app, specifically 'clients' and 'services' modules. It includes the PhoneNumberField in the Client model and adds a new Credential model to the services module. The Service model has been modified to include new fields and relationships.
This commit is contained in:
1 parent
27ca456212
commit
8b50ff5412
24 files changed
+1483
-49
No files matched your search
@@ -1,5 +1,3 @@
|
||||
"""generated with djinit"""
|
||||
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -0,0 +1,9 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class TimeStampedModel(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
@@ -0,0 +1,61 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from phonenumber_field.modelfields import PhoneNumberField
|
||||
|
||||
from src.models.base import TimeStampedModel
|
||||
|
||||
|
||||
class Client(TimeStampedModel):
|
||||
name = models.CharField(max_length=255, db_index=True)
|
||||
short_code = models.CharField(max_length=6, unique=True, blank=True)
|
||||
email = models.EmailField()
|
||||
phone = PhoneNumberField()
|
||||
address = models.TextField()
|
||||
company_name = models.CharField(max_length=255, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
indexes = [
|
||||
models.Index(fields=["name"]),
|
||||
models.Index(fields=["short_code"]),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
# Optional validation for short_code length
|
||||
if self.short_code and len(self.short_code) > 6:
|
||||
raise ValidationError("Short code cannot be more than 6 characters.")
|
||||
|
||||
def generate_short_code(self):
|
||||
"""
|
||||
Generates a unique 6-character code:
|
||||
First 3 letters of name + incremental counter.
|
||||
Example: SAN001, SAN002, etc.
|
||||
Ensures uniqueness even if multiple clients share the same prefix.
|
||||
"""
|
||||
base = self.name[:3].upper()
|
||||
|
||||
# Find existing codes with same prefix
|
||||
existing = Client.objects.filter(short_code__startswith=base).values_list(
|
||||
"short_code", flat=True
|
||||
)
|
||||
|
||||
# Extract available next number
|
||||
numbers = []
|
||||
for code in existing:
|
||||
try:
|
||||
number = int(code[3:])
|
||||
numbers.append(number)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
next_num = max(numbers, default=0) + 1
|
||||
return f"{base}{next_num:03d}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.short_code:
|
||||
self.short_code = self.generate_short_code()
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.short_code})"
|
||||
@@ -0,0 +1,83 @@
|
||||
from django.db import models
|
||||
|
||||
from src.models.base import TimeStampedModel
|
||||
from src.models.clients import Client
|
||||
from src.models.projects import Project
|
||||
from src.models.services import Service
|
||||
|
||||
|
||||
class Invoice(TimeStampedModel):
|
||||
STATUS_CHOICES = [
|
||||
("DRAFT", "Draft"),
|
||||
("SENT", "Sent"),
|
||||
("PAID", "Paid"),
|
||||
("OVERDUE", "Overdue"),
|
||||
("CANCELLED", "Cancelled"),
|
||||
]
|
||||
client = models.ForeignKey(
|
||||
Client, on_delete=models.CASCADE, related_name="invoices"
|
||||
)
|
||||
project = models.ForeignKey(
|
||||
Project, on_delete=models.CASCADE, related_name="invoices"
|
||||
)
|
||||
invoice_number = models.CharField(max_length=20, unique=True, editable=False)
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
date_issued = models.DateField(auto_now_add=True)
|
||||
due_date = models.DateField(auto_now=True)
|
||||
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="DRAFT")
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.id:
|
||||
client_code = self.client.short_code # e.g., "NEX001"
|
||||
|
||||
# Find last invoice for this client
|
||||
last_invoice = (
|
||||
Invoice.objects.filter(invoice_number__startswith=f"{client_code}-")
|
||||
.order_by("-invoice_number")
|
||||
.first()
|
||||
)
|
||||
|
||||
if last_invoice:
|
||||
last_number = int(last_invoice.invoice_number.split("-")[1])
|
||||
new_number = last_number + 1
|
||||
else:
|
||||
new_number = 1
|
||||
|
||||
# final format → NEX001-0001
|
||||
self.invoice_number = f"{client_code}-{new_number:04d}"
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"Invoice {self.invoice_number}"
|
||||
|
||||
|
||||
class Expense(models.Model):
|
||||
service = models.ForeignKey(
|
||||
Service,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="expenses",
|
||||
)
|
||||
description = models.CharField(max_length=255)
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
date = models.DateField()
|
||||
category = models.CharField(max_length=100, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.description} - {self.amount}"
|
||||
|
||||
|
||||
class Payment(models.Model):
|
||||
invoice = models.ForeignKey(
|
||||
Invoice, on_delete=models.CASCADE, related_name="payments"
|
||||
)
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
date = models.DateField()
|
||||
method = models.CharField(max_length=50, blank=True)
|
||||
transaction_id = models.CharField(max_length=100, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"Payment of {self.amount} for {self.invoice}"
|
||||
@@ -0,0 +1,65 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
from src.models.projects import Project, Task
|
||||
|
||||
|
||||
class Note(models.Model):
|
||||
title = models.CharField(max_length=255, db_index=True)
|
||||
content = models.TextField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def __str__(self):
|
||||
return self.title[:50] # avoid super long titles
|
||||
|
||||
|
||||
class TimeEntry(models.Model):
|
||||
project = models.ForeignKey(
|
||||
Project,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="time_entries",
|
||||
)
|
||||
task = models.ForeignKey(
|
||||
Task,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="time_entries",
|
||||
)
|
||||
description = models.CharField(max_length=255)
|
||||
start_time = models.DateTimeField()
|
||||
end_time = models.DateTimeField(null=True, blank=True)
|
||||
duration = models.DurationField(null=True, blank=True, editable=False)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-start_time"]
|
||||
indexes = [
|
||||
models.Index(fields=["start_time"]),
|
||||
models.Index(fields=["end_time"]),
|
||||
models.Index(fields=["project"]),
|
||||
models.Index(fields=["task"]),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
if self.end_time and self.start_time and self.end_time < self.start_time:
|
||||
raise ValidationError("End time cannot be earlier than start time.")
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.start_time and self.end_time:
|
||||
self.duration = self.end_time - self.start_time
|
||||
else:
|
||||
self.duration = None
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
duration_str = (
|
||||
str(self.duration).split(".")[0] if self.duration else "In Progress"
|
||||
)
|
||||
return f"{self.description} — {duration_str}"
|
||||
@@ -0,0 +1,93 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
from src.models.base import TimeStampedModel
|
||||
from src.models.clients import Client
|
||||
|
||||
|
||||
class Project(TimeStampedModel):
|
||||
STATUS_CHOICES = [
|
||||
("PLANNING", "Planning"),
|
||||
("IN_PROGRESS", "In Progress"),
|
||||
("ON_HOLD", "On Hold"),
|
||||
("COMPLETED", "Completed"),
|
||||
("CANCELLED", "Cancelled"),
|
||||
]
|
||||
|
||||
client = models.ForeignKey(
|
||||
Client, on_delete=models.CASCADE, related_name="projects"
|
||||
)
|
||||
name = models.CharField(max_length=255, db_index=True)
|
||||
description = models.TextField()
|
||||
start_date = models.DateField(null=True, blank=True)
|
||||
deadline = models.DateField(null=True, blank=True)
|
||||
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="PLANNING")
|
||||
budget = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
indexes = [
|
||||
models.Index(fields=["status"]),
|
||||
models.Index(fields=["client"]),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
if self.start_date and self.deadline and self.deadline < self.start_date:
|
||||
raise ValidationError("Deadline cannot be earlier than start date.")
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.get_status_display()})"
|
||||
|
||||
|
||||
class Milestone(TimeStampedModel):
|
||||
project = models.ForeignKey(
|
||||
Project, on_delete=models.CASCADE, related_name="milestones"
|
||||
)
|
||||
title = models.CharField(max_length=255)
|
||||
due_date = models.DateField(null=True, blank=True)
|
||||
is_completed = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
ordering = ["due_date"]
|
||||
indexes = [
|
||||
models.Index(fields=["project"]),
|
||||
models.Index(fields=["due_date"]),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
status = "✓" if self.is_completed else "✗"
|
||||
return f"{self.title} [{status}]"
|
||||
|
||||
|
||||
class Task(TimeStampedModel):
|
||||
STATUS_CHOICES = [
|
||||
("TODO", "To Do"),
|
||||
("IN_PROGRESS", "In Progress"),
|
||||
("REVIEW", "Review"),
|
||||
("DONE", "Done"),
|
||||
]
|
||||
|
||||
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name="tasks")
|
||||
title = models.CharField(max_length=255, db_index=True)
|
||||
description = models.TextField(blank=True)
|
||||
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="TODO")
|
||||
due_date = models.DateField(null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["status", "due_date"]
|
||||
indexes = [
|
||||
models.Index(fields=["project"]),
|
||||
models.Index(fields=["status"]),
|
||||
models.Index(fields=["due_date"]),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
today = timezone.localdate()
|
||||
|
||||
# Ensure due_date is not in the past
|
||||
if self.due_date and self.due_date < today:
|
||||
raise ValidationError("Due date cannot be in the past.")
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.title} — {self.get_status_display()}"
|
||||
@@ -0,0 +1,72 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
from src.models.base import TimeStampedModel
|
||||
from src.models.clients import Client
|
||||
|
||||
|
||||
class Service(TimeStampedModel):
|
||||
TYPE_CHOICES = [
|
||||
("DOMAIN", "Domain"),
|
||||
("HOSTING", "Hosting"),
|
||||
("VPS", "VPS"),
|
||||
("SSL", "SSL"),
|
||||
("OTHER", "Other"),
|
||||
]
|
||||
|
||||
client = models.ForeignKey(
|
||||
Client, on_delete=models.CASCADE, related_name="services"
|
||||
)
|
||||
service_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
|
||||
name = models.CharField(
|
||||
max_length=255, help_text="e.g. example.com or VPS - 4GB", db_index=True
|
||||
)
|
||||
provider = models.CharField(max_length=255, blank=True)
|
||||
cost = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
|
||||
renewal_price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
|
||||
start_date = models.DateField(null=True, blank=True)
|
||||
expiry_date = models.DateField(null=True, blank=True)
|
||||
auto_renew = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
indexes = [
|
||||
models.Index(fields=["service_type"]),
|
||||
models.Index(fields=["client"]),
|
||||
models.Index(fields=["expiry_date"]),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
# ensure expiry_date is not earlier than start_date
|
||||
if self.start_date and self.expiry_date and self.expiry_date < self.start_date:
|
||||
raise ValidationError("Expiry date cannot be earlier than start date.")
|
||||
|
||||
# optional: prevent expiry date in the past for new services
|
||||
today = timezone.localdate()
|
||||
if self.expiry_date and self.expiry_date < today:
|
||||
raise ValidationError("Expiry date cannot be in the past.")
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.get_service_type_display()} – {self.name}"
|
||||
|
||||
|
||||
class Credential(models.Model):
|
||||
service = models.ForeignKey(
|
||||
Service, on_delete=models.CASCADE, related_name="credentials"
|
||||
)
|
||||
url = models.URLField(blank=True)
|
||||
username = models.CharField(max_length=255, blank=True)
|
||||
password = models.CharField(max_length=255, blank=True)
|
||||
notes = models.TextField(blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["service"]
|
||||
indexes = [
|
||||
models.Index(fields=["service"]),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
# Avoid showing passwords / sensitive info
|
||||
cred_name = self.username or "No Username"
|
||||
return f"Credential ({cred_name}) for {self.service.name}"
|
||||
Reference in new issue
Block a user