Add document

This commit is contained in:
2025-02-06 11:24:19 +01:00
parent 4a70d1f571
commit e9476dc495
5 changed files with 256 additions and 87 deletions

View File

@@ -3,6 +3,7 @@ import uuid
from django.contrib.auth.models import User
from django.db import models
from localflavor.br.br_states import STATE_CHOICES
from django.contrib import admin
class UuidModel(models.Model):
@@ -41,92 +42,6 @@ class CreatedBy(models.Model):
abstract = True
class Address(models.Model):
address = models.CharField(
'endereço',
max_length=100,
null=True,
blank=True
)
address_number = models.IntegerField('número', null=True, blank=True)
complement = models.CharField(
'complemento',
max_length=100,
null=True,
blank=True
)
district = models.CharField(
'bairro',
max_length=100,
null=True,
blank=True
)
city = models.CharField('cidade', max_length=100, null=True, blank=True)
uf = models.CharField(
'UF',
max_length=2,
choices=STATE_CHOICES,
null=True,
blank=True
)
cep = models.CharField('CEP', max_length=9, null=True, blank=True)
country = models.CharField(
'país',
max_length=50,
default='Brasil',
null=True,
blank=True
)
class Meta:
abstract = True
def to_dict_base(self):
return {
'address': self.address,
'address_number': self.address_number,
'complement': self.complement,
'district': self.district,
'city': self.city,
'uf': self.uf,
'cep': self.cep,
}
class Document(models.Model):
cpf = models.CharField(
'CPF',
max_length=11,
unique=True,
null=True,
blank=True
)
rg = models.CharField('RG', max_length=11, null=True, blank=True)
cnh = models.CharField('CNH', max_length=20, null=True, blank=True)
class Meta:
abstract = True
def to_dict_base(self):
return {
'cpf': self.cpf,
'rg': self.rg,
'cnh': self.cnh,
}
class Active(models.Model):
active = models.BooleanField('ativo', default=True)
exist_deleted = models.BooleanField(
'existe/deletado',
default=True,
help_text='Se for True o item existe. Se for False o item foi deletado.'
)
class Meta:
abstract = True
class Organization(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
@@ -155,3 +70,57 @@ class Organization(models.Model):
return self.name
class DocumentSegment(models.Model):
SEGMENT_TYPES = (
('title', 'Title'),
('subtitle', 'Subtitle'),
('h1', 'Header 1'),
('h2', 'Header 2'),
('h3', 'Header 3'),
('body', 'Body Text'),
('quote', 'Quote')
)
document = models.ForeignKey('Document', on_delete=models.CASCADE, related_name='segments')
segment_type = models.CharField(max_length=20, choices=SEGMENT_TYPES)
content = models.TextField()
order = models.PositiveIntegerField()
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['order']
def __str__(self):
return f"{self.get_segment_type_display()} - {self.content[:50]}"
class Document(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name='documents')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"Document for {self.organization.name}"
def add_segment(self, segment_type, content, position=None):
"""
Add a new segment at the specified position.
If position is None, append to the end.
"""
if position is None:
# Get the highest order and add 1
last_order = self.segments.aggregate(models.Max('order'))['order__max']
new_order = 1 if last_order is None else last_order + 1
else:
# Move all segments at and after the position up by 1
self.segments.filter(order__gte=position).update(order=models.F('order') + 1)
new_order = position
return self.segments.create(
segment_type=segment_type,
content=content,
order=new_order
)