Skip to content

Commit

Permalink
Merge branch 'main' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
mouse-reeve committed Jan 26, 2023
2 parents 5e2a416 + 9c3c348 commit cad83a3
Show file tree
Hide file tree
Showing 53 changed files with 724 additions and 52 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ REDIS_ACTIVITY_PORT=6379
REDIS_ACTIVITY_PASSWORD=redispassword345
# Optional, use a different redis database (defaults to 0)
# REDIS_ACTIVITY_DB_INDEX=0
# Alternatively specify the full redis url, i.e. if you need to use a unix:// socket
# REDIS_ACTIVITY_URL=

# Redis as celery broker
REDIS_BROKER_HOST=redis_broker
REDIS_BROKER_PORT=6379
REDIS_BROKER_PASSWORD=redispassword123
# Optional, use a different redis database (defaults to 0)
# REDIS_BROKER_DB_INDEX=0
# Alternatively specify the full redis url, i.e. if you need to use a unix:// socket
# REDIS_BROKER_URL=

# Monitoring for celery
FLOWER_PORT=8888
Expand Down
1 change: 1 addition & 0 deletions bookwyrm/activitypub/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ class Author(BookData):
bio: str = ""
wikipediaLink: str = ""
type: str = "Author"
website: str = ""
1 change: 1 addition & 0 deletions bookwyrm/forms/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Meta:
"invite_request_question",
"invite_question_text",
"require_confirm_email",
"default_user_auth_group",
]

widgets = {
Expand Down
2 changes: 2 additions & 0 deletions bookwyrm/forms/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Meta:
"aliases",
"bio",
"wikipedia_link",
"website",
"born",
"died",
"openlibrary_key",
Expand All @@ -31,6 +32,7 @@ class Meta:
"wikipedia_link": forms.TextInput(
attrs={"aria-describedby": "desc_wikipedia_link"}
),
"website": forms.TextInput(attrs={"aria-describedby": "desc_website"}),
"born": forms.SelectDateWidget(attrs={"aria-describedby": "desc_born"}),
"died": forms.SelectDateWidget(attrs={"aria-describedby": "desc_died"}),
"openlibrary_key": forms.TextInput(
Expand Down
36 changes: 34 additions & 2 deletions bookwyrm/importers/importer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
""" handle reading a csv from an external service, defaults are from Goodreads """
import csv
from datetime import timedelta
from django.utils import timezone
from bookwyrm.models import ImportJob, ImportItem
from bookwyrm.models import ImportJob, ImportItem, SiteSettings


class Importer:
Expand Down Expand Up @@ -33,6 +34,7 @@ class Importer:
"reading": ["currently-reading", "reading", "currently reading"],
}

# pylint: disable=too-many-locals
def create_job(self, user, csv_file, include_reviews, privacy):
"""check over a csv and creates a database entry for the job"""
csv_reader = csv.DictReader(csv_file, delimiter=self.delimiter)
Expand All @@ -49,7 +51,13 @@ def create_job(self, user, csv_file, include_reviews, privacy):
source=self.service,
)

enforce_limit, allowed_imports = self.get_import_limit(user)
if enforce_limit and allowed_imports <= 0:
job.complete_job()
return job
for index, entry in rows:
if enforce_limit and index >= allowed_imports:
break
self.create_item(job, index, entry)
return job

Expand Down Expand Up @@ -99,6 +107,24 @@ def normalize_row(self, entry, mappings): # pylint: disable=no-self-use
"""use the dataclass to create the formatted row of data"""
return {k: entry.get(v) for k, v in mappings.items()}

def get_import_limit(self, user): # pylint: disable=no-self-use
"""check if import limit is set and return how many imports are left"""
site_settings = SiteSettings.objects.get()
import_size_limit = site_settings.import_size_limit
import_limit_reset = site_settings.import_limit_reset
enforce_limit = import_size_limit and import_limit_reset
allowed_imports = 0

if enforce_limit:
time_range = timezone.now() - timedelta(days=import_limit_reset)
import_jobs = ImportJob.objects.filter(
user=user, created_date__gte=time_range
)
# pylint: disable=consider-using-generator
imported_books = sum([job.successful_item_count for job in import_jobs])
allowed_imports = import_size_limit - imported_books
return enforce_limit, allowed_imports

def create_retry_job(self, user, original_job, items):
"""retry items that didn't import"""
job = ImportJob.objects.create(
Expand All @@ -110,7 +136,13 @@ def create_retry_job(self, user, original_job, items):
mappings=original_job.mappings,
retry=True,
)
for item in items:
enforce_limit, allowed_imports = self.get_import_limit(user)
if enforce_limit and allowed_imports <= 0:
job.complete_job()
return job
for index, item in enumerate(items):
if enforce_limit and index >= allowed_imports:
break
# this will re-normalize the raw data
self.create_item(job, item.index, item.data)
return job
7 changes: 1 addition & 6 deletions bookwyrm/management/commands/erase_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

from bookwyrm import settings

r = redis.Redis(
host=settings.REDIS_ACTIVITY_HOST,
port=settings.REDIS_ACTIVITY_PORT,
password=settings.REDIS_ACTIVITY_PASSWORD,
db=settings.REDIS_ACTIVITY_DB_INDEX,
)
r = redis.from_url(settings.REDIS_ACTIVITY_URL)


def erase_streams():
Expand Down
2 changes: 2 additions & 0 deletions bookwyrm/management/commands/initdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ def init_connectors():

def init_settings():
"""info about the instance"""
group_editor = Group.objects.filter(name="editor").first()
models.SiteSettings.objects.create(
support_link="https://www.patreon.com/bookwyrm",
support_title="Patreon",
install_mode=True,
default_user_auth_group=group_editor,
)


Expand Down
23 changes: 23 additions & 0 deletions bookwyrm/migrations/0167_sitesettings_import_size_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.16 on 2022-12-05 13:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0166_sitesettings_imports_enabled"),
]

operations = [
migrations.AddField(
model_name="sitesettings",
name="import_size_limit",
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name="sitesettings",
name="import_limit_reset",
field=models.IntegerField(default=0),
),
]
13 changes: 13 additions & 0 deletions bookwyrm/migrations/0171_merge_20221219_2020.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 3.2.16 on 2022-12-19 20:20

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0167_sitesettings_import_size_limit"),
("bookwyrm", "0170_merge_0168_auto_20221205_2331_0169_auto_20221206_0902"),
]

operations = []
21 changes: 21 additions & 0 deletions bookwyrm/migrations/0173_author_website.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.16 on 2023-01-15 08:38

import bookwyrm.models.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0172_alter_user_preferred_language"),
]

operations = [
migrations.AddField(
model_name="author",
name="website",
field=bookwyrm.models.fields.CharField(
blank=True, max_length=255, null=True
),
),
]
34 changes: 34 additions & 0 deletions bookwyrm/migrations/0173_default_user_auth_group_setting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.2.16 on 2022-12-27 21:34

from django.db import migrations, models
import django.db.models.deletion


def backfill_sitesettings(apps, schema_editor):
db_alias = schema_editor.connection.alias
group_model = apps.get_model("auth", "Group")
editor_group = group_model.objects.using(db_alias).filter(name="editor").first()

sitesettings_model = apps.get_model("bookwyrm", "SiteSettings")
sitesettings_model.objects.update(default_user_auth_group=editor_group)


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0175_merge_0173_author_website_0174_merge_20230111_1523"),
]

operations = [
migrations.AddField(
model_name="sitesettings",
name="default_user_auth_group",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to="auth.group",
),
),
migrations.RunPython(backfill_sitesettings, migrations.RunPython.noop),
]
13 changes: 13 additions & 0 deletions bookwyrm/migrations/0173_merge_20230102_1444.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 3.2.16 on 2023-01-02 14:44

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0171_merge_20221219_2020"),
("bookwyrm", "0172_alter_user_preferred_language"),
]

operations = []
12 changes: 12 additions & 0 deletions bookwyrm/migrations/0174_merge_20230111_1523.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 3.2.16 on 2023-01-11 15:23

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0173_merge_20230102_1444"),
]

operations = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 3.2.16 on 2023-01-19 20:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0173_author_website"),
("bookwyrm", "0174_merge_20230111_1523"),
]

operations = []
4 changes: 4 additions & 0 deletions bookwyrm/models/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Author(BookDataModel):
isfdb = fields.CharField(
max_length=255, blank=True, null=True, deduplication_field=True
)

website = fields.CharField(
max_length=255, blank=True, null=True, deduplication_field=True
)
# idk probably other keys would be useful here?
born = fields.DateTimeField(blank=True, null=True)
died = fields.DateTimeField(blank=True, null=True)
Expand Down
6 changes: 6 additions & 0 deletions bookwyrm/models/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from urllib.parse import urljoin
import uuid

import django.contrib.auth.models as auth_models
from django.core.exceptions import PermissionDenied
from django.db import models, IntegrityError
from django.dispatch import receiver
Expand Down Expand Up @@ -70,6 +71,9 @@ class SiteSettings(SiteModel):
allow_invite_requests = models.BooleanField(default=True)
invite_request_question = models.BooleanField(default=False)
require_confirm_email = models.BooleanField(default=True)
default_user_auth_group = models.ForeignKey(
auth_models.Group, null=True, blank=True, on_delete=models.PROTECT
)

invite_question_text = models.CharField(
max_length=255, blank=True, default="What is your favourite book?"
Expand All @@ -90,6 +94,8 @@ class SiteSettings(SiteModel):

# controls
imports_enabled = models.BooleanField(default=True)
import_size_limit = models.IntegerField(default=0)
import_limit_reset = models.IntegerField(default=0)

field_tracker = FieldTracker(fields=["name", "instance_tagline", "logo"])

Expand Down
14 changes: 10 additions & 4 deletions bookwyrm/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from urllib.parse import urlparse

from django.apps import apps
from django.contrib.auth.models import AbstractUser, Group
from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import ArrayField, CICharField
from django.core.exceptions import PermissionDenied
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
from django.dispatch import receiver
from django.db import models, transaction
from django.utils import timezone
Expand Down Expand Up @@ -356,8 +356,14 @@ def save(self, *args, **kwargs):

# make users editors by default
try:
self.groups.add(Group.objects.get(name="editor"))
except Group.DoesNotExist:
group = (
apps.get_model("bookwyrm.SiteSettings")
.objects.get()
.default_user_auth_group
)
if group:
self.groups.add(group)
except ObjectDoesNotExist:
# this should only happen in tests
pass

Expand Down
7 changes: 1 addition & 6 deletions bookwyrm/redis_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

from bookwyrm import settings

r = redis.Redis(
host=settings.REDIS_ACTIVITY_HOST,
port=settings.REDIS_ACTIVITY_PORT,
password=settings.REDIS_ACTIVITY_PASSWORD,
db=settings.REDIS_ACTIVITY_DB_INDEX,
)
r = redis.from_url(settings.REDIS_ACTIVITY_URL)


class RedisStore(ABC):
Expand Down
Loading

0 comments on commit cad83a3

Please sign in to comment.