moviewyrm/bookwyrm/views/setup.py

100 lines
3.4 KiB
Python
Raw Normal View History

2022-02-17 18:02:42 +00:00
""" Installation wizard 🧙 """
2022-02-17 21:22:33 +00:00
import re
2022-02-17 19:25:11 +00:00
from django.contrib.auth import login
from django.contrib.auth.models import Group
from django.core.exceptions import PermissionDenied
2022-02-17 19:25:11 +00:00
from django.db import transaction
from django.shortcuts import redirect
2022-02-17 18:02:42 +00:00
from django.template.response import TemplateResponse
from django.views import View
from bookwyrm import forms, models
2022-02-17 21:01:36 +00:00
from bookwyrm import settings
2022-02-17 21:22:33 +00:00
from bookwyrm.utils import regex
2022-02-17 18:02:42 +00:00
# pylint: disable= no-self-use
2022-02-17 21:01:36 +00:00
class InstanceConfig(View):
"""make sure the instance looks correct before adding any data"""
def get(self, request):
"""Check out this cool instance"""
# only allow this view when an instance is being configured
site = models.SiteSettings.objects.get()
if not site.install_mode:
raise PermissionDenied()
# check for possible problems with the instance configuration
warnings = {}
warnings["debug"] = settings.DEBUG
2022-02-17 21:22:33 +00:00
warnings["invalid_domain"] = not re.match(rf"^{regex.DOMAIN}$", settings.DOMAIN)
warnings["protocol"] = not settings.DEBUG and not settings.USE_HTTPS
2022-02-17 21:01:36 +00:00
2022-02-17 21:22:33 +00:00
# pylint: disable=line-too-long
2022-02-17 21:01:36 +00:00
data = {
"warnings": warnings,
"info": {
"domain": settings.DOMAIN,
"version": settings.VERSION,
"use_https": settings.USE_HTTPS,
"language": settings.LANGUAGE_CODE,
"use_s3": settings.USE_S3,
"email_sender": f"{settings.EMAIL_SENDER_NAME}@{settings.EMAIL_SENDER_DOMAIN}",
"preview_images": settings.ENABLE_PREVIEW_IMAGES,
"thumbnails": settings.ENABLE_THUMBNAIL_GENERATION,
},
}
return TemplateResponse(request, "setup/config.html", data)
2022-02-17 18:02:42 +00:00
class CreateAdmin(View):
"""manage things like the instance name"""
def get(self, request):
2022-02-17 19:25:11 +00:00
"""Create admin user form"""
# only allow this view when an instance is being configured
site = models.SiteSettings.objects.get()
if not site.install_mode:
raise PermissionDenied()
2022-02-17 19:31:52 +00:00
data = {"register_form": forms.RegisterForm()}
2022-02-17 18:02:42 +00:00
return TemplateResponse(request, "setup/admin.html", data)
2022-02-17 19:25:11 +00:00
@transaction.atomic
def post(self, request):
"""Create that user"""
2022-02-17 22:14:31 +00:00
site = models.SiteSettings.objects.get()
2022-02-17 19:25:11 +00:00
# you can't create an admin user if you're in config mode
if not site.install_mode:
raise PermissionDenied()
form = forms.RegisterForm(request.POST)
if not form.is_valid():
data = {"register_form": form}
return TemplateResponse(request, "setup/admin.html", data)
localname = form.data["localname"].strip()
2022-02-17 21:01:36 +00:00
username = f"{localname}@{settings.DOMAIN}"
2022-02-17 19:25:11 +00:00
user = models.User.objects.create_superuser(
username,
form.data["email"],
form.data["password"],
localname=localname,
local=True,
deactivation_reason=None,
is_active=True,
)
# Set "admin" role
try:
user.groups.set(Group.objects.filter(name__in=["admin", "moderator"]))
except Group.DoesNotExist:
# this should only happen in tests
pass
login(request, user)
site.install_mode = False
site.save()
return redirect("settings-site")