How can I generate a dynamic shorturl with expiring time?

442 Views Asked by At

I have a python code with Django that makes the URL shorter with hash decoder, but it cannot generate dynamic code for an especial full URL. In fact, I want to generate short URL codes with expiring time so that when its time expired, I would be able to generate a new short URL for my full URL. Thank you for your answers!

model.py

class URL(models.Model):
full_url = models.URLField(unique=True)
short_url = models.URLField(unique=True)
created_at = models.DateTimeField(auto_now_add=True)

def save(self, *args, **kwargs):
    if not self.id:
        self.short_url = md5(self.full_url.encode()).hexdigest()[:8]

    validate = URLValidator()
    try:
        validate(self.full_url)
    except ValidationError as e:
        raise GraphQLError('invalid url')

    return super().save(*args, **kwargs)
1

There are 1 best solutions below

0
fuzes On

I don't know exactly what you're trying to do.

do you want to block access url after time expired??

if so I'd issue a token(like jwt token) to access url.