Answer the question
In order to leave comments, you need to log in
How to execute pending tasks on the backend?
In almost every web service where there is user registration and some kind of their state, such a task exists. For example, "gold" status, premium status. The user pays money, his status changes, the end date is set: in a month, a year. The question is how to see that the time has elapsed and return the user to a normal state. I gave only an example, you can come up with others.
So, right off the bat, I can come up with two solutions:
Answer the question
In order to leave comments, you need to log in
An alternative approach is to not store the current user status at all. Instead, keep a log of changes to this status and calculate its current value on demand. I am accustomed to talk in terms of Django, and we will focus on its ORM. Let's say you have your own model for the user - User in my_auth application . Let it have two status values: empty ( None ) and premium - for those who paid for a yearly subscription. Status change log:
from django.db import models
from my_auth.models import User
class StatusEvent(models.Model):
EVENT_TYPES = [
('subscription', 'User subscribed to premium')
]
user = models.ForeignKey(User, related_name='events')
time = models.DateTimeField(auto_now_add=True)
type = models.CharField(max_length=16, choices=EVENT_TYPES)
class Meta:
ordering = '-time'
from datetime import datetime
from django.contrib.auth.models import User as DefaultUser
class User(DefaultUser):
@property
def status(self):
event = self.events.filter(type='subscription').first()
if event and datetime.now() - event.time < self.subscription_duration:
return 'premium'
What's wrong with cron? In my opinion the most appropriate solution in this situation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question