from django.db import models from django.utils.translation import gettext_lazy as _ from django_scopes import ScopedManager from scopedsites.models import Domain from xenua.django.models import RandomSlugPKMixin class ShortLink(RandomSlugPKMixin, models.Model): class Meta: unique_together = ('domain', 'location') domain = models.ForeignKey(Domain, related_name='links', on_delete=models.CASCADE) location = models.CharField(_("short link"), max_length=100) to = models.URLField(_("redirect to"), max_length=2500) click_count = models.IntegerField(default=0) objects = ScopedManager(domain='domain') def click(self): # todo: replace with cache + regular cleanup impl self.click_count += 1 self.save() def link(self): return f"{self.domain.fqdn}/{self.location}"