diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index d723ebdbf..0a69dbe09 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -202,6 +202,12 @@ class ExpiryWidget(widgets.Select): return timezone.now() + interval +class InviteRequestForm(CustomForm): + class Meta: + model = models.InviteRequest + fields = ["email"] + + class CreateInviteForm(CustomForm): class Meta: model = models.SiteInvite diff --git a/bookwyrm/migrations/0056_auto_20210321_0123.py b/bookwyrm/migrations/0056_auto_20210321_0123.py new file mode 100644 index 000000000..f36e4b12d --- /dev/null +++ b/bookwyrm/migrations/0056_auto_20210321_0123.py @@ -0,0 +1,58 @@ +# Generated by Django 3.1.6 on 2021-03-21 01:23 + +import bookwyrm.models.fields +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0055_auto_20210321_0101"), + ] + + operations = [ + migrations.AddField( + model_name="sitesettings", + name="allow_invite_requests", + field=models.BooleanField(default=True), + ), + migrations.CreateModel( + name="InviteRequest", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created_date", models.DateTimeField(auto_now_add=True)), + ("updated_date", models.DateTimeField(auto_now=True)), + ( + "remote_id", + bookwyrm.models.fields.RemoteIdField( + max_length=255, + null=True, + validators=[bookwyrm.models.fields.validate_remote_id], + ), + ), + ("email", models.EmailField(max_length=255, unique=True)), + ("invite_sent", models.BooleanField(default=False)), + ( + "invite", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="bookwyrm.siteinvite", + ), + ), + ], + options={ + "abstract": False, + }, + ), + ] diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index 326a673e1..35e32c2cf 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -26,7 +26,7 @@ from .federated_server import FederatedServer from .import_job import ImportJob, ImportItem -from .site import SiteSettings, SiteInvite, PasswordReset +from .site import SiteSettings, SiteInvite, PasswordReset, InviteRequest cls_members = inspect.getmembers(sys.modules[__name__], inspect.isclass) activity_models = { diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index 7fde6781e..2722de174 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -3,10 +3,11 @@ import base64 import datetime from Crypto import Random -from django.db import models +from django.db import models, IntegrityError from django.utils import timezone from bookwyrm.settings import DOMAIN +from .base_model import BookWyrmModel from .user import User @@ -24,6 +25,7 @@ class SiteSettings(models.Model): code_of_conduct = models.TextField(default="Add a code of conduct here.") privacy_policy = models.TextField(default="Add a privacy policy here.") allow_registration = models.BooleanField(default=True) + allow_invite_requests = models.BooleanField(default=True) 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) @@ -69,6 +71,22 @@ class SiteInvite(models.Model): return "https://{}/invite/{}".format(DOMAIN, self.code) +class InviteRequest(BookWyrmModel): + """ prospective users can request an invite """ + + email = models.EmailField(max_length=255, unique=True) + invite = models.ForeignKey( + SiteInvite, on_delete=models.SET_NULL, null=True, blank=True + ) + invite_sent = models.BooleanField(default=False) + + def save(self, *args, **kwargs): + """ don't create a request for a registered email """ + if User.objects.filter(email=self.email).exists(): + raise IntegrityError() + super().save(*args, **kwargs) + + def get_passowrd_reset_expiry(): """ give people a limited time to use the link """ now = timezone.now()