Last active
April 18, 2020 18:29
-
-
Save bismarck4lves/868ee07f77a02309e2cb8e34f1cabd56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| MODELS | |
| class Horas (models.Model): | |
| id = models.AutoField(primary_key=True) | |
| hora = models.TimeField() | |
| status = models.BooleanField(default=True) | |
| #class Meta... | |
| class Agendas (models.Model): | |
| id = models.AutoField(primary_key=True) | |
| dia = models.DateField() | |
| horario = models.ManyToManyField('Horas') | |
| #class Meta... | |
| def to_representation(self, instance): | |
| data = super(AgendasSerializer, self).to_representation(instance) | |
| def valida_datas(dia, hora, agenda_id): | |
| horario = Horas.objects.get(hora=hora) | |
| expirado = Validators().data_expirada(dia, hora) | |
| if expirado: | |
| return False | |
| em_uso = Validators().consulta_disponivel(horario.pk, agenda_id) | |
| if em_uso: | |
| return False | |
| return True | |
| data['horario'] = list( | |
| filter(lambda hora: valida_datas(data['dia'], hora, data['id']), | |
| data['horario']) | |
| ) | |
| return data | |
| SERIALIZERS | |
| lass AgendasSerializer(serializers.ModelSerializer): | |
| horario = serializers.SlugRelatedField( | |
| many=True, | |
| read_only=True, | |
| slug_field='hora', | |
| ) | |
| RESPOSTA exemplo | |
| [ | |
| { | |
| "id": 1, | |
| "dia": "2020-04-19", | |
| "horario": [ | |
| "14:10:52", | |
| "12:00:00", | |
| ] | |
| }, | |
| { | |
| "id": #mesma estrutura | |
| } | |
| ] | |
| diamos que a hora 12:00:00 para a agenda 1 esteja em uso ficar assim: | |
| { | |
| "id": 1, | |
| "dia": "2020-04-19", | |
| "horario": [ | |
| "14:10:52", | |
| ] | |
| }, | |
| isso eu consigo fazer com o to_representation. | |
| porém,nese caso, se 12:00:00 e 14:10:52 estiverem em uso devo remover da listagem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment