2020-09-21 17:25:26 +00:00
|
|
|
''' the particulars for this instance of BookWyrm '''
|
2020-06-01 21:34:45 +00:00
|
|
|
import base64
|
2020-10-02 20:32:19 +00:00
|
|
|
import datetime
|
2020-06-01 21:34:45 +00:00
|
|
|
|
|
|
|
from Crypto import Random
|
2020-06-01 18:54:08 +00:00
|
|
|
from django.db import models
|
2020-06-03 16:38:30 +00:00
|
|
|
from django.utils import timezone
|
2020-06-01 21:34:45 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2020-06-03 16:38:30 +00:00
|
|
|
from .user import User
|
2020-06-01 18:54:08 +00:00
|
|
|
|
|
|
|
class SiteSettings(models.Model):
|
2020-09-21 17:25:26 +00:00
|
|
|
''' customized settings for this instance '''
|
2020-10-05 21:42:15 +00:00
|
|
|
name = models.CharField(default='BookWyrm', max_length=100)
|
2020-06-01 18:54:08 +00:00
|
|
|
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):
|
2020-09-21 17:25:26 +00:00
|
|
|
''' gets the site settings db entry or defaults '''
|
2020-06-01 18:54:08 +00:00
|
|
|
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
|
|
|
|
2020-10-02 20:32:19 +00:00
|
|
|
def new_access_code():
|
2020-09-21 17:25:26 +00:00
|
|
|
''' the identifier for a user invite '''
|
2020-06-01 21:34:45 +00:00
|
|
|
return base64.b32encode(Random.get_random_bytes(5)).decode('ascii')
|
|
|
|
|
|
|
|
class SiteInvite(models.Model):
|
2020-09-21 17:25:26 +00:00
|
|
|
''' gives someone access to create an account on the instance '''
|
2020-10-02 20:32:19 +00:00
|
|
|
code = models.CharField(max_length=32, default=new_access_code)
|
2020-06-01 21:34:45 +00:00
|
|
|
expiry = models.DateTimeField(blank=True, null=True)
|
|
|
|
use_limit = models.IntegerField(blank=True, null=True)
|
|
|
|
times_used = models.IntegerField(default=0)
|
2020-06-03 16:38:30 +00:00
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
2020-06-01 21:34:45 +00:00
|
|
|
|
|
|
|
def valid(self):
|
2020-09-21 17:25:26 +00:00
|
|
|
''' make sure it hasn't expired or been used '''
|
2020-06-01 21:34:45 +00:00
|
|
|
return (
|
2020-06-03 16:38:30 +00:00
|
|
|
(self.expiry is None or self.expiry > timezone.now()) and
|
2020-06-01 21:34:45 +00:00
|
|
|
(self.use_limit is None or self.times_used < self.use_limit))
|
2020-06-03 16:38:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def link(self):
|
2020-09-21 17:25:26 +00:00
|
|
|
''' formats the invite link '''
|
2020-06-03 16:38:30 +00:00
|
|
|
return "https://{}/invite/{}".format(DOMAIN, self.code)
|
2020-10-02 20:32:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_passowrd_reset_expiry():
|
|
|
|
''' give people a limited time to use the link '''
|
2020-11-28 00:24:53 +00:00
|
|
|
now = timezone.now()
|
2020-10-02 20:32:19 +00:00
|
|
|
return now + datetime.timedelta(days=1)
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordReset(models.Model):
|
|
|
|
''' gives someone access to create an account on the instance '''
|
|
|
|
code = models.CharField(max_length=32, default=new_access_code)
|
|
|
|
expiry = models.DateTimeField(default=get_passowrd_reset_expiry)
|
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
''' make sure it hasn't expired or been used '''
|
|
|
|
return self.expiry > timezone.now()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def link(self):
|
|
|
|
''' formats the invite link '''
|
|
|
|
return "https://{}/password-reset/{}".format(DOMAIN, self.code)
|