From 88ed299e56f2cd39f57c7c0fec883dc4bfd14bbf Mon Sep 17 00:00:00 2001 From: Sesostris Vieira Date: Fri, 14 Aug 2015 12:21:45 -0300 Subject: [PATCH] Move business rule to correct place --- sigi/apps/ocorrencias/admin.py | 8 +------- sigi/apps/ocorrencias/models.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/sigi/apps/ocorrencias/admin.py b/sigi/apps/ocorrencias/admin.py index 0aec760..0e0e1b6 100644 --- a/sigi/apps/ocorrencias/admin.py +++ b/sigi/apps/ocorrencias/admin.py @@ -26,7 +26,7 @@ class ComentarioInline(admin.StackedInline): fieldsets = ((None, {'fields': (('novo_status', 'encaminhar_setor',), 'descricao', )}),) def get_queryset(self, queryset): - return self.model.objects.get_query_set() + return self.model.objects.none() class AnexosInline(admin.TabularInline): @@ -105,12 +105,6 @@ class OcorrenciaAdmin(BaseModelAdmin): for instance in instances: if isinstance(instance, Comentario): instance.usuario = servidor - if instance.encaminhar_setor and (instance.encaminhar_setor != instance.ocorrencia.setor_responsavel): - instance.ocorrencia.setor_responsavel = instance.encaminhar_setor - instance.ocorrencia.save() - if instance.novo_status and (instance.novo_status != instance.ocorrencia.status): - instance.ocorrencia.status = instance.novo_status - instance.ocorrencia.save() instance.save() super(OcorrenciaAdmin, self).save_formset(request, form, formset, change) diff --git a/sigi/apps/ocorrencias/models.py b/sigi/apps/ocorrencias/models.py index 0abf4f1..fa83c42 100644 --- a/sigi/apps/ocorrencias/models.py +++ b/sigi/apps/ocorrencias/models.py @@ -68,12 +68,21 @@ class Ocorrencia(models.Model): class Comentario(models.Model): - ocorrencia = models.ForeignKey(Ocorrencia, verbose_name=_(u'Ocorrência')) + ocorrencia = models.ForeignKey(Ocorrencia, verbose_name=_(u'Ocorrência'), related_name='comentarios') data_criacao = models.DateTimeField(_(u'Data de criação'), null=True, blank=True, auto_now_add=True) descricao = models.TextField(_(u'Descrição'), blank=True, null=True) usuario = models.ForeignKey('servidores.Servidor', verbose_name=_(u'Usuário')) novo_status = models.IntegerField(_(u'Novo status'), choices=Ocorrencia.STATUS_CHOICES, blank=True, null=True) encaminhar_setor = models.ForeignKey('servidores.Servico', verbose_name=_(u'Encaminhar para setor'), blank=True, null=True) + + def save(self, *args, **kwargs): + if self.encaminhar_setor and (self.encaminhar_setor != self.ocorrencia.setor_responsavel): + self.ocorrencia.setor_responsavel = self.encaminhar_setor + self.ocorrencia.save() + if self.novo_status and (self.novo_status != self.ocorrencia.status): + self.ocorrencia.status = self.novo_status + self.ocorrencia.save() + super(Comentario, self).save(*args, **kwargs) class Anexo(models.Model):