Last active
February 7, 2023 03:05
-
-
Save carlosdelfino/e7d54cf9e1e0ca6f045baa5c6eb5949a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.db import models | |
from datetime import datetime | |
#from carteira.models.corretora import Corretora | |
class AgentesCorretoras(models.Model): | |
corretora = models.ForeignKey('carteira.Corretora', on_delete=models.CASCADE) | |
agente = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) | |
data_contratacao = models.DateField('Data de contratação do Agente', | |
null=False, default=datetime.now, blank=True) | |
data_demissao = models.DateField('Data de demissão do Agente', | |
null=True, default=datetime.now, blank=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.conf import settings | |
from datetime import datetime | |
class Carteira(models.Model): | |
""" | |
A carteira agrupa os movimentos de investimento realizados. | |
Criação é data que a carteira teve seu primeiro movimento, portanto uma carteira só pode ser criada com a compra de um ativo para ela. | |
Fechamento, o fechamento da carteira ocorre quando todos os ativos forem vendidos, e o fechamento for solicitado. | |
""" | |
investidor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False, | |
related_name="carteiras") | |
criacao = models.DateField("Data e hora criação da carteira", | |
null=False, default=datetime.now, blank=True) | |
fechamento = models.DateField("Data e hora de fechamento da carteira", null=True, blank=True) | |
nome = models.CharField('Nome da Carteira', max_length=80, null=False, unique=False) | |
descricao = models.TextField('Descrição da Carteira', null=False) | |
def __str__(self) -> str: | |
return "{} - ({} - {})".format(self.nome, self.criacao, self.fechamento) | |
class Meta: | |
constraints = [models.UniqueConstraint( | |
name='unique_carteira_investidor', fields=['investidor', 'nome'], | |
deferrable=models.Deferrable.DEFERRED)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from carteira.models import Carteira | |
from .user import UserSerializer | |
class CarteiraSerializer(serializers.HyperlinkedModelSerializer): | |
investidor = UserSerializer() | |
class Meta: | |
model = Carteira | |
fields = ('url', | |
'id', | |
'investidor', | |
'criacao', | |
'fechamento', | |
'nome', | |
'descricao', | |
) | |
extra_kwargs = { | |
'url': {'view_name': 'carteira-detail-restful', 'lookup_field': 'pk'}, | |
'investidor': {'required': True}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.db import models | |
from datetime import date | |
from carteira.models.agentes_corretoras import AgentesCorretoras | |
class Corretora(models.Model): | |
nome = models.CharField("Nome da Corretora", null=False, max_length=80) | |
telefone = models.CharField("Telefone", null=True, max_length=12) | |
email = models.EmailField("E-mail principal", null=True) | |
agentes = models.ManyToManyField( | |
settings.AUTH_USER_MODEL, | |
related_name='corretoras', | |
through=AgentesCorretoras, | |
through_fields=('corretora', 'agente'), | |
) | |
def __str__(self) -> str: | |
return self.nome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from carteira.models import Corretora | |
from .user import UserSerializer | |
class CorretoraSerializer(serializers.HyperlinkedModelSerializer): | |
agentes = UserSerializer(many=True) | |
class Meta: | |
model = Corretora | |
fields = ('url', | |
'id', | |
'nome', | |
'telefone', | |
'email', | |
'agentes' | |
) | |
extra_kwargs = { | |
'url': {'view_name': 'corretora-detail-restful', 'lookup_field': 'pk'}, | |
'agentes': {'required': False}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from django.conf import settings | |
from django.contrib.auth import models | |
class UserSerializer(serializers.HyperlinkedModelSerializer): | |
class Meta: | |
#model = settings.AUTH_USER_MODEL | |
model = models.User | |
fields = ( | |
'url', | |
'pk', | |
'username', | |
'corretoras', | |
'carteiras', | |
) | |
extra_kwargs = { | |
'url': {'view_name': 'user-detail-restful', 'lookup_field': 'pk'}, | |
'corretoras': {'view_name': 'corretora-detail-restful', 'lookup_field': 'pk'}, | |
'carteiras': {'view_name': 'carteira-detail-restful', 'lookup_field': 'pk'}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment