0

Need to change user status to inactive if not active for 1 day using django fsm library

class UserValidity(models.Model):
    state = FSMField(default="active")
    name = models.CharField(max_length=10)
    date = models.DateField(auto_now_add=True)
def state_inactive(self):
    if self.date < datetime.date.today():
        self.state = 'inactive'
        return self.save()
    else:
        return True

@transition(field=state, source="active", target="inactive", conditions=[state_inactive])
def state_change(self):
    print('State Inactive')
TMD
  • 1
  • 1

1 Answers1

0

The FSMField field is an identifier here, you will be able to check user status from this field but you need to change it based on the condition.

One of the best solutions is you can run a scheduler that will run at 12:00 AM (Midnight) and this will run a function that will change the FSMField after comparing a datetime field, this datetime field will store the last date and time the user made a request to the server.

Bashar
  • 453
  • 5
  • 14