2020-06-01 21:34:45 +00:00
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from Crypto import Random
|
2020-06-01 18:54:08 +00:00
|
|
|
from django.db import models
|
2020-06-01 21:34:45 +00:00
|
|
|
|
2020-06-01 18:54:08 +00:00
|
|
|
from fedireads.settings import DOMAIN
|
|
|
|
|
|
|
|
class SiteSettings(models.Model):
|
|
|
|
name = models.CharField(default=DOMAIN, max_length=100)
|
|
|
|
instance_description = models.TextField(
|
|
|
|
default="This instance has no description.")
|
|
|
|
code_of_conduct = models.TextField(
|
|
|
|
default="Add a code of conduct here.")
|
|
|
|
allow_registration = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls):
|
|
|
|
try:
|
|
|
|
return cls.objects.get(id=1)
|
|
|
|
except cls.DoesNotExist:
|
|
|
|
default_settings = SiteSettings(id=1)
|
|
|
|
default_settings.save()
|
|
|
|
return default_settings
|
2020-06-01 21:34:45 +00:00
|
|
|
|
|
|
|
def new_invite_code():
|
|
|
|
return base64.b32encode(Random.get_random_bytes(5)).decode('ascii')
|
|
|
|
|
|
|
|
class SiteInvite(models.Model):
|
|
|
|
code = models.CharField(max_length=32, default=new_invite_code)
|
|
|
|
expiry = models.DateTimeField(blank=True, null=True)
|
|
|
|
use_limit = models.IntegerField(blank=True, null=True)
|
|
|
|
times_used = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
return (
|
|
|
|
(self.expiry is None or self.expiry > datetime.datetime.now()) and
|
|
|
|
(self.use_limit is None or self.times_used < self.use_limit))
|