takahe/users/models/invite.py

52 lines
1.3 KiB
Python
Raw Normal View History

2022-11-18 07:09:04 +00:00
import random
2022-12-20 08:51:53 +00:00
import urlman
2022-11-18 07:09:04 +00:00
from django.db import models
from django.utils import timezone
2022-11-18 07:09:04 +00:00
class Invite(models.Model):
"""
An invite token, good for one signup.
"""
# Should always be lowercase
token = models.CharField(max_length=500, unique=True)
# Admin note about this code
note = models.TextField(null=True, blank=True)
# Uses remaining (null means "infinite")
uses = models.IntegerField(null=True, blank=True)
# Expiry date
expires = models.DateTimeField(null=True, blank=True)
2022-11-18 07:09:04 +00:00
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
2022-12-20 08:51:53 +00:00
class urls(urlman.Urls):
admin = "/admin/invites/"
admin_create = "{admin}create/"
admin_view = "{admin}{self.pk}/"
2022-11-18 07:09:04 +00:00
@classmethod
def create_random(cls, uses=None, expires=None, note=None):
2022-11-18 07:09:04 +00:00
return cls.objects.create(
token="".join(
random.choice("abcdefghkmnpqrstuvwxyz23456789") for i in range(20)
),
uses=uses,
expires=expires,
2022-12-20 14:38:42 +00:00
note=note,
2022-11-18 07:09:04 +00:00
)
@property
def valid(self):
if self.uses is not None:
if self.uses <= 0:
return False
if self.expires is not None:
return self.expires >= timezone.now()
return True