azarjaved
azarjaved

Reputation: 63

How to create url to image in databse with expiration time

I have create model like:

class Photo(models.Model):

"""
Photo model with automatically generated Thumbnail by TierPhotoSetting of UserTier
"""
user = models.ForeignKey(User, blank=False, null=False, on_delete=models.CASCADE)
photo = models.ImageField(upload_to='photos', blank=False, null=False)

and now I would like to create a new database object which stores a link to photo from Photo. This url link should have expiration time like 500 sec. How to serve user url to image which are active just some time? Thanks in advance.

Upvotes: 0

Views: 430

Answers (1)

umair
umair

Reputation: 534

there are two ways to implement it depending on your use case,

If you want to keep the image on the server and disable access after 500sec, you can keep a record of when it's created and failed the request if the difference between the request time and created time is greater than 500Sec.

if you want to remove the image completely, then you need to go for some async operations for this you need to run a celery instance which is taking care of the async job. you can schedule a task to delete the image and update the database record to expired. for this you will need celery, Please refer to the documentation for more details.

https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html

Upvotes: 1

Related Questions