moviewyrm/bookwyrm/models/site.py

204 lines
7.4 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" the particulars for this instance of BookWyrm """
2020-10-02 20:32:19 +00:00
import datetime
2021-11-18 04:22:00 +00:00
from urllib.parse import urljoin
2022-02-17 18:02:42 +00:00
import uuid
2021-03-21 01:23:59 +00:00
from django.db import models, IntegrityError
2021-05-26 08:19:39 +00:00
from django.dispatch import receiver
2020-06-03 16:38:30 +00:00
from django.utils import timezone
from model_utils import FieldTracker
2021-05-26 08:19:39 +00:00
from bookwyrm.preview_images import generate_site_preview_image_task
2021-11-18 04:22:00 +00:00
from bookwyrm.settings import DOMAIN, ENABLE_PREVIEW_IMAGES, STATIC_FULL_URL
2021-08-06 21:42:18 +00:00
from .base_model import BookWyrmModel, new_access_code
2020-06-03 16:38:30 +00:00
from .user import User
2021-11-18 04:22:00 +00:00
from .fields import get_absolute_url
2021-03-08 16:49:10 +00:00
class SiteSettings(models.Model):
2021-04-26 16:15:42 +00:00
"""customized settings for this instance"""
2021-03-08 16:49:10 +00:00
name = models.CharField(default="BookWyrm", max_length=100)
instance_tagline = models.CharField(
2021-03-08 16:49:10 +00:00
max_length=150, default="Social Reading and Reviewing"
2020-10-05 23:07:37 +00:00
)
2021-03-08 16:49:10 +00:00
instance_description = models.TextField(default="This instance has no description.")
instance_short_description = models.CharField(max_length=255, blank=True, null=True)
2022-02-26 20:43:27 +00:00
default_theme = models.ForeignKey(
"Theme", null=True, blank=True, on_delete=models.SET_NULL
)
2022-03-16 19:39:49 +00:00
version = models.CharField(null=True, blank=True, max_length=10)
2021-04-30 17:42:27 +00:00
2022-02-17 18:02:42 +00:00
# admin setup options
install_mode = models.BooleanField(default=False)
admin_code = models.CharField(max_length=50, default=uuid.uuid4)
2021-04-30 17:42:27 +00:00
# about page
2021-03-08 16:49:10 +00:00
registration_closed_text = models.TextField(
2021-09-18 23:00:06 +00:00
default="We aren't taking new users at this time. You can find an open "
'instance at <a href="https://joinbookwyrm.com/instances">'
"joinbookwyrm.com/instances</a>."
2021-09-18 22:42:48 +00:00
)
invite_request_text = models.TextField(
2021-09-18 23:00:06 +00:00
default="If your request is approved, you will receive an email with a "
"registration link."
2020-10-05 23:07:37 +00:00
)
2021-03-08 16:49:10 +00:00
code_of_conduct = models.TextField(default="Add a code of conduct here.")
privacy_policy = models.TextField(default="Add a privacy policy here.")
2021-04-30 17:42:27 +00:00
# registration
2022-02-17 19:27:05 +00:00
allow_registration = models.BooleanField(default=False)
2021-03-21 01:23:59 +00:00
allow_invite_requests = models.BooleanField(default=True)
2022-03-07 17:49:59 +00:00
invite_request_question = models.BooleanField(default=False)
2021-08-06 23:24:57 +00:00
require_confirm_email = models.BooleanField(default=True)
2021-04-30 17:42:27 +00:00
2022-03-07 17:49:59 +00:00
invite_question_text = models.CharField(
2022-03-13 12:37:09 +00:00
max_length=255, blank=True, default="What is your favourite book?"
2022-03-09 15:04:58 +00:00
)
2021-04-30 17:42:27 +00:00
# images
2021-03-08 16:49:10 +00:00
logo = models.ImageField(upload_to="logos/", null=True, blank=True)
logo_small = models.ImageField(upload_to="logos/", null=True, blank=True)
favicon = models.ImageField(upload_to="logos/", null=True, blank=True)
2021-05-26 12:46:34 +00:00
preview_image = models.ImageField(
upload_to="previews/logos/", null=True, blank=True
)
2021-04-30 17:42:27 +00:00
# footer
2020-12-11 20:31:02 +00:00
support_link = models.CharField(max_length=255, null=True, blank=True)
support_title = models.CharField(max_length=100, null=True, blank=True)
admin_email = models.EmailField(max_length=255, null=True, blank=True)
2021-04-30 17:42:27 +00:00
footer_item = models.TextField(null=True, blank=True)
2021-05-27 19:40:23 +00:00
field_tracker = FieldTracker(fields=["name", "instance_tagline", "logo"])
@classmethod
def get(cls):
2021-04-26 16:15:42 +00:00
"""gets the site settings db entry or defaults"""
try:
return cls.objects.get(id=1)
except cls.DoesNotExist:
default_settings = SiteSettings(id=1)
default_settings.save()
return default_settings
2021-11-18 22:39:22 +00:00
@property
def logo_url(self):
"""helper to build the logo url"""
return self.get_url("logo", "images/logo.png")
2021-11-18 04:22:00 +00:00
@property
def logo_small_url(self):
2021-11-18 22:39:22 +00:00
"""helper to build the logo url"""
return self.get_url("logo_small", "images/logo-small.png")
@property
def favicon_url(self):
"""helper to build the logo url"""
return self.get_url("favicon", "images/favicon.png")
def get_url(self, field, default_path):
"""get a media url or a default static path"""
uploaded = getattr(self, field, None)
if uploaded:
return get_absolute_url(uploaded)
return urljoin(STATIC_FULL_URL, default_path)
2021-11-18 04:22:00 +00:00
def save(self, *args, **kwargs):
2022-03-16 23:50:34 +00:00
"""if require_confirm_email is disabled, make sure no users are pending,
if enabled, make sure invite_question_text is not empty"""
if not self.require_confirm_email:
User.objects.filter(is_active=False, deactivation_reason="pending").update(
is_active=True, deactivation_reason=None
)
if not self.invite_question_text:
self.invite_question_text = "What is your favourite book?"
super().save(*args, **kwargs)
2021-03-08 16:49:10 +00:00
2022-02-26 20:43:27 +00:00
class Theme(models.Model):
"""Theme files"""
created_date = models.DateTimeField(auto_now_add=True)
2022-02-26 21:38:45 +00:00
name = models.CharField(max_length=50, unique=True)
2022-02-27 18:00:50 +00:00
path = models.CharField(max_length=50, unique=True)
2022-02-26 20:43:27 +00:00
2022-02-26 21:38:45 +00:00
def __str__(self):
2022-02-27 16:09:17 +00:00
# pylint: disable=invalid-str-returned
2022-02-26 21:38:45 +00:00
return self.name
2022-02-26 20:43:27 +00:00
class SiteInvite(models.Model):
2021-04-26 16:15:42 +00:00
"""gives someone access to create an account on the instance"""
2021-03-08 16:49:10 +00:00
2021-01-05 19:37:48 +00:00
created_date = models.DateTimeField(auto_now_add=True)
2020-10-02 20:32:19 +00:00
code = models.CharField(max_length=32, default=new_access_code)
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)
2021-04-02 00:19:29 +00:00
invitees = models.ManyToManyField(User, related_name="invitees")
def valid(self):
2021-04-26 16:15:42 +00:00
"""make sure it hasn't expired or been used"""
2021-03-08 16:49:10 +00:00
return (self.expiry is None or self.expiry > timezone.now()) and (
self.use_limit is None or self.times_used < self.use_limit
)
2020-06-03 16:38:30 +00:00
@property
def link(self):
2021-04-26 16:15:42 +00:00
"""formats the invite link"""
2021-09-18 18:32:00 +00:00
return f"https://{DOMAIN}/invite/{self.code}"
2020-10-02 20:32:19 +00:00
2021-03-21 01:23:59 +00:00
class InviteRequest(BookWyrmModel):
2021-04-26 16:15:42 +00:00
"""prospective users can request an invite"""
2021-03-21 01:23:59 +00:00
email = models.EmailField(max_length=255, unique=True)
invite = models.ForeignKey(
SiteInvite, on_delete=models.SET_NULL, null=True, blank=True
)
2022-03-13 22:34:49 +00:00
answer = models.TextField(max_length=50, unique=False, null=True, blank=True)
2021-03-21 01:23:59 +00:00
invite_sent = models.BooleanField(default=False)
ignored = models.BooleanField(default=False)
2021-03-21 01:23:59 +00:00
def save(self, *args, **kwargs):
2021-04-26 16:15:42 +00:00
"""don't create a request for a registered email"""
if not self.id and User.objects.filter(email=self.email).exists():
2021-03-21 01:23:59 +00:00
raise IntegrityError()
super().save(*args, **kwargs)
2020-10-02 20:32:19 +00:00
def get_passowrd_reset_expiry():
2021-04-26 16:15:42 +00:00
"""give people a limited time to use the link"""
now = timezone.now()
2020-10-02 20:32:19 +00:00
return now + datetime.timedelta(days=1)
class PasswordReset(models.Model):
2021-04-26 16:15:42 +00:00
"""gives someone access to create an account on the instance"""
2021-03-08 16:49:10 +00:00
2020-10-02 20:32:19 +00:00
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):
2021-04-26 16:15:42 +00:00
"""make sure it hasn't expired or been used"""
2020-10-02 20:32:19 +00:00
return self.expiry > timezone.now()
@property
def link(self):
2021-04-26 16:15:42 +00:00
"""formats the invite link"""
2021-09-21 00:19:26 +00:00
return f"https://{DOMAIN}/password-reset/{self.code}"
2021-05-26 08:19:39 +00:00
# pylint: disable=unused-argument
2021-06-18 22:24:10 +00:00
@receiver(models.signals.post_save, sender=SiteSettings)
2021-05-26 08:19:39 +00:00
def preview_image(instance, *args, **kwargs):
2021-06-18 22:24:10 +00:00
"""Update image preview for the default site image"""
if not ENABLE_PREVIEW_IMAGES:
return
changed_fields = instance.field_tracker.changed()
2021-05-26 08:19:39 +00:00
if len(changed_fields) > 0:
2021-05-26 08:19:39 +00:00
generate_site_preview_image_task.delay()