mardi 23 avril 2019

using a if statement inside django model that is updating the model when threshold is reached

Please bear with me as I'm new with Django

I have in the model a table where I need to put a timer. If the timer limit is reached, we update the row of the column accordingly.

This is my model

class Question(models.Model):

    date = models.DateTimeField(auto_now_add=True, verbose_name=_('date'
                                ))
    first_name = models.CharField(max_length=255,
                                  verbose_name=_('first name'),
                                  blank=True)
    last_name = models.CharField(max_length=255,
                                 verbose_name=_('last name'),
                                 blank=True)
    email = models.EmailField(max_length=255,
                              verbose_name=_('email address'),
                              blank=True)
    phone = models.CharField(max_length=255, verbose_name=_('phone'),
                             blank=True)
    extra = models.CharField(max_length=255, verbose_name=_('extra'),
                             blank=True)
    subject = models.CharField(max_length=255, verbose_name=_('subject'
                               ), blank=True)
    message = models.TextField(max_length=1000,
                               verbose_name=_('question'))
    relevance = models.BooleanField(default=True,
                                    verbose_name=_('relevance'))
    marketing_consent = models.BooleanField(default=False,
            verbose_name=_('marketing consent'))

    expert = models.ForeignKey(Expert, on_delete=models.SET_NULL,
                               null=True, blank=True,
                               verbose_name=_('expert (target)'))
    analysis_tags = models.ManyToManyField(AnalysisTag, blank=True,
            verbose_name=_('analysis tags'))

    predicted_experts = models.ManyToManyField(Expert,
            through='AssociatedExpert', related_name='predicted_for',
            blank=True)
    public_token = models.CharField(max_length=50,
                                    default=generate_token,
                                    db_index=True, unique=True,
                                    verbose_name=_('public token'))

    STATE_CHOICES = (
        ('W', 'Waiting'),
        ('D', 'Declined'),
        ('T', 'Taken'),
        ('A', 'Answered'),
        ('F', 'Feedback'),
        ('P', 'Pending'),
        ('C', 'Cancelled'),
        )
    state = models.CharField(max_length=10, choices=STATE_CHOICES,
                             default='P', verbose_name=_('state'))

What I want to do is to add the following but I'm unsure if the model is the right place to add the method below and also if I'm writing correctly that.

def value(self):
48_mn_ago = now() - timedelta(minutes=48)
if self.date > 48_mn_ago and self.state == 'P':
    return self.state = 'C', self.first_name = '***', self.last_name = '***'

Many thanks for your help.

Aucun commentaire:

Enregistrer un commentaire