internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: bookwyrm-social/bookwyrm

Issue:
Books on multiple shelves duplicated in the "all books" view
<img width="1172" alt="Screen Shot 2021-12-02 at 11 47 40 AM" src="https://user-images.githubusercontent.com/1807695/144495864-f94425a7-b11f-4df4-8ab7-c850d5a17996.png">



Discussion:
Is this happening because `shelve_button` isn't removing old reading status `ShelfBook` objects when the user "moves" the book?
Hm, I don't think so? The book duplication seems to be happening because the query treats each shelf's version of the book as unique. I'm not sure about the duplicate "remove" buttons but I suspect adding a `distinct()` to a query would help with them.
Ok I have worked out what's causing this, though I don't know how to fix it and I think there may be two bugs here.

The first bug is what I was referring to earlier: notice that `Remove from Currently Reading` appears twice, which it shouldn't. I haven't looked into that more yet.

The second bug is that each edition appears once for every shelf it is on, in the "All Books" view. I'm pretty sure this is happening because when the book is annotated, we collect the `ShelfBook.shelf_date`:

in views/shelf.py:
```python
books = books.annotate(
    rating=Subquery(reviews.values("rating")[:1]),
    shelved_date=F("shelfbook__shelved_date"),
    start_date=Subquery(reading.values("start_date")[:1]),
    finish_date=Subquery(reading.values("finish_date")[:1]),
    author=Subquery(
        models.Book.objects.filter(id=OuterRef("id")).values("authors__name")[
            :1
        ]
    ),
).prefetch_related("authors")
```

If I comment out that line, we don't get duplicates, but we also can't sort by shelved date. What we ideally want is some way to remove `shelved_date` from the "all books" view OR perhaps better would be to somehow annotate it in a way that whatever is the _latest_ shelved date is used for the All Books view and we don't have duplicates.

Unfortunately, I'm a bit stumped as to how we can do either of those. Any ideas?

Resolving patch:
diff --git a/bookwyrm/views/shelf/shelf.py b/bookwyrm/views/shelf/shelf.py
--- a/bookwyrm/views/shelf/shelf.py
+++ b/bookwyrm/views/shelf/shelf.py
@@ -72,9 +72,13 @@ def get(self, request, username, shelf_identifier=None):
             "start_date"
         )
 
+        if shelf_identifier:
+            books = books.annotate(shelved_date=F("shelfbook__shelved_date"))
+        else:
+            # sorting by shelved date will cause duplicates in the "all books" view
+            books = books.annotate(shelved_date=F("updated_date"))
         books = books.annotate(
             rating=Subquery(reviews.values("rating")[:1]),
-            shelved_date=F("shelfbook__shelved_date"),
             start_date=Subquery(reading.values("start_date")[:1]),
             finish_date=Subquery(reading.values("finish_date")[:1]),
             author=Subquery(


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Allow changing what email notifications are sent from
**Is your feature request related to a problem? Please describe.**
It came up on the Matrix chat that an admin didn't have an admin@ email for their domain, and wanted to change it to no-reply@, which is only possible by changing `settings.py`

**Describe the solution you'd like**
It would be nice to be able to configure this in the `.env` alongside the rest of the email things - perhaps `EMAIL_FROM_ADDRESS` or `EMAIL_SENDER_ADDRESS` or something.

**Describe alternatives you've considered**
Changing in settings.py works but is in a checked-in file so that's not great. We could allow just changing the username of the email, but it seems useful to have the additional flexibility (for example, I might want to send my Bookywrm emails from `bookwyrm@cincodenada.com` instead of `admin@bookwyrm.cincodenada.com`, since I can't receive email at `bookwyrm.cincodenada.com`)

**Additional context**
[Matrix conversation](https://matrix.to/#/!zoxBMxLlvIyeEKkHuB:matrix.org/$tQg0cS2vzhBeziDszNLxnIc21TR-KzYk55PgWBpyRTo?via=matrix.org&via=tchncs.de&via=group.lt)



Discussion:

Resolving patch:
diff --git a/bookwyrm/emailing.py b/bookwyrm/emailing.py
--- a/bookwyrm/emailing.py
+++ b/bookwyrm/emailing.py
@@ -69,7 +69,7 @@ def format_email(email_name, data):
 def send_email(recipient, subject, html_content, text_content):
     """use a task to send the email"""
     email = EmailMultiAlternatives(
-        subject, text_content, settings.DEFAULT_FROM_EMAIL, [recipient]
+        subject, text_content, settings.EMAIL_SENDER, [recipient]
     )
     email.attach_alternative(html_content, "text/html")
     email.send()
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -24,7 +24,9 @@
 EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD")
 EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", True)
 EMAIL_USE_SSL = env.bool("EMAIL_USE_SSL", False)
-DEFAULT_FROM_EMAIL = f"admin@{DOMAIN}"
+EMAIL_SENDER_NAME = env("EMAIL_SENDER_NAME", "admin")
+EMAIL_SENDER_DOMAIN = env("EMAIL_SENDER_NAME", DOMAIN)
+EMAIL_SENDER = f"{EMAIL_SENDER_NAME}@{EMAIL_SENDER_DOMAIN}"
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Receive '403: forbidden' when registering new account
**Describe the bug**
When attempting to register a new account I receive a `403` status from the `/register` endpoint. 

*Full message:*
> 
> CSRF verification failed. Request aborted.
> 
> You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
> 
> If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for “same-origin” requests.
> 

A friend reported this to me and I was able to reproduce. Cookies are not disabled on my browser. 

**To Reproduce**
Steps to reproduce the behavior:
1. Go to https://bookwyrm.social/
2. Fill in new user form and click "Sign Up"
3. Observe failed request and 403 response. 

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
<img width="798" alt="Screen Shot 2022-01-06 at 2 35 53 PM" src="https://user-images.githubusercontent.com/32184074/148462597-dead2839-f237-4523-b821-bb0f3055214e.png">
<img width="819" alt="Screen Shot 2022-01-06 at 2 34 21 PM" src="https://user-images.githubusercontent.com/32184074/148462625-136f0f46-4fd9-48a9-a02c-df628225c87e.png">


**Instance**
bookwyrm.social

---

**Desktop (please complete the following information):**
    - OS: macOS 12.1
    - Chrome Version 96.0.4664.110



Discussion:
I can replicate this too. On a fresh dev install my first user goes through but the second one in a brand new incognito window gets this error. @mouse-reeve could this be something to do with the new caching setup?

Resolving patch:
diff --git a/bookwyrm/views/landing/landing.py b/bookwyrm/views/landing/landing.py
--- a/bookwyrm/views/landing/landing.py
+++ b/bookwyrm/views/landing/landing.py
@@ -1,8 +1,6 @@
 """ non-interactive pages """
 from django.template.response import TemplateResponse
 from django.views import View
-from django.utils.decorators import method_decorator
-from django.views.decorators.cache import cache_page
 
 from bookwyrm import forms
 from bookwyrm.views import helpers
@@ -33,7 +31,6 @@ def get(self, request):
 class Landing(View):
     """preview of recently reviewed books"""
 
-    @method_decorator(cache_page(60 * 60), name="dispatch")
     def get(self, request):
         """tiled book activity page"""
         data = {


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Unread notifications are no longer visually distinguished from read ones
I just forgot to consider this when I re-wrote the notifications page


Discussion:

Resolving patch:
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -14,7 +14,7 @@
 PAGE_LENGTH = env("PAGE_LENGTH", 15)
 DEFAULT_LANGUAGE = env("DEFAULT_LANGUAGE", "English")
 
-JS_CACHE = "2d3181e1"
+JS_CACHE = "9b4cc1f7"
 
 # email
 EMAIL_BACKEND = env("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend")


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Server error on adding read start date
**Describe the bug**
Over the past days I get a server error when setting a read start date.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to ['book page'](https://bookwyrm.social/book/111431)
2. Click on +Add read dates, set start date
3. See server error when loading https://bookwyrm.social/create-readthrough

**Expected behavior**
Start date saved and book visibile in current reading list.

**Instance**
bookwyrm.social

**Additional context**

---

**Desktop (please complete the following information):**
    - OS: MacOS 11.2.3
    - Browser Firefox 95.0.2
    - Version [e.g. 22]




Discussion:

Resolving patch:
diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py
--- a/bookwyrm/forms.py
+++ b/bookwyrm/forms.py
@@ -500,7 +500,7 @@ def clean(self):
         cleaned_data = super().clean()
         start_date = cleaned_data.get("start_date")
         finish_date = cleaned_data.get("finish_date")
-        if start_date > finish_date:
+        if start_date and finish_date and start_date > finish_date:
             self.add_error(
                 "finish_date", _("Reading finish date cannot be before start date.")
             )


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Let admins set default interface language
Right now, BookWyrm's interface language is determined by, in order of preference:
1. The language selected in the user's settings
2. The language requested by the browser, if it's available in the app
3. English

Admin should be able to set the default display language of the interface for logged out viewers and users that don't have a preference saved.


Discussion:
Does `DEFAULT_LANGUAGE` from `.env` not do this already? And regardless of that, would it be preferable to set the default language from the admin web interface rather than `.env`?
Nope, all that does is prefer that the default edition of a book be in the provided language -- which is super non-obvious from context. Also it assumes the language field on the book is in English. Not one of the most fleshed out options 😬 

And yeah agreed, having an option in the admin panel to set language would be the best solution. 
Based on https://docs.djangoproject.com/en/3.2/topics/i18n/translation/#how-django-discovers-language-preference it sounds like it will only be practical to set language code in the `.env` file, rather than the site settings interface. The alternative appears to be writing custom locale middleware, which I don't think has benefits that outweigh how much hassle that would be.

Resolving patch:
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -243,7 +243,7 @@
 # Internationalization
 # https://docs.djangoproject.com/en/3.2/topics/i18n/
 
-LANGUAGE_CODE = "en-us"
+LANGUAGE_CODE = env("LANGUAGE_CODE", "en-us")
 LANGUAGES = [
     ("en-us", _("English")),
     ("de-de", _("Deutsch (German)")),


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Invalid table limit error
**Describe the bug**
When running a fresh dev instance I get an `Invalid table limit` error, coming from `initdb.py`. Not sure if something is broken in the latest main branch, or I need to update my configuration.

**To Reproduce**
Steps to reproduce the behavior:
1. fetch latest `main` branch
2. `./bw-dev resetdb`
3. Get error (see below)

**Expected behavior**
BookWyrm resets database and new install works without errors.

**Screenshots**
```
  Applying sessions.0001_initial... OK
+ execweb python manage.py initdb
+ docker-compose exec web python manage.py initdb
Traceback (most recent call last):
  File "/app/manage.py", line 18, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/app/bookwyrm/management/commands/initdb.py", line 168, in handle
    raise Exception("Invalid table limit:", limit)
Exception: ('Invalid table limit:', None)
```

**Instance**
local development, current `main` branch.

**Additional context**
I initially started getting this error on a branch I was working on, but it's occuring on the latest `main` branch without any changes.

---

**Desktop (please complete the following information):**
    - OS: MacOS



Discussion:
Oh! I broke this when I updated the management command to take options. 

Resolving patch:
diff --git a/bookwyrm/management/commands/initdb.py b/bookwyrm/management/commands/initdb.py
--- a/bookwyrm/management/commands/initdb.py
+++ b/bookwyrm/management/commands/initdb.py
@@ -164,7 +164,7 @@ def handle(self, *args, **options):
             "settings",
             "linkdomain",
         ]
-        if limit not in tables:
+        if limit and limit not in tables:
             raise Exception("Invalid table limit:", limit)
 
         if not limit or limit == "group":


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Email to admins when a user is reported has the wrong link



Discussion:

Resolving patch:
diff --git a/bookwyrm/emailing.py b/bookwyrm/emailing.py
--- a/bookwyrm/emailing.py
+++ b/bookwyrm/emailing.py
@@ -48,7 +48,9 @@ def moderation_report_email(report):
     data["reportee"] = report.user.localname or report.user.username
     data["report_link"] = report.remote_id
 
-    for admin in models.User.objects.filter(groups__name__in=["admin", "moderator"]):
+    for admin in models.User.objects.filter(
+        groups__name__in=["admin", "moderator"]
+    ).distinct():
         data["user"] = admin.display_name
         send_email.delay(admin.email, *format_email("moderation_report", data))
 
diff --git a/bookwyrm/models/report.py b/bookwyrm/models/report.py
--- a/bookwyrm/models/report.py
+++ b/bookwyrm/models/report.py
@@ -1,5 +1,6 @@
 """ flagged for moderation """
 from django.db import models
+from bookwyrm.settings import DOMAIN
 from .base_model import BookWyrmModel
 
 
@@ -15,6 +16,9 @@ class Report(BookWyrmModel):
     links = models.ManyToManyField("Link", blank=True)
     resolved = models.BooleanField(default=False)
 
+    def get_remote_id(self):
+        return f"https://{DOMAIN}/settings/reports/{self.id}"
+
     class Meta:
         """set order by default"""
 
diff --git a/bookwyrm/views/admin/reports.py b/bookwyrm/views/admin/reports.py
--- a/bookwyrm/views/admin/reports.py
+++ b/bookwyrm/views/admin/reports.py
@@ -58,6 +58,7 @@ def get(self, request, report_id):
         """load a report"""
         data = {
             "report": get_object_or_404(models.Report, id=report_id),
+            "group_form": forms.UserGroupForm(),
         }
         return TemplateResponse(request, "settings/reports/report.html", data)
 


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
List of items failed to import does not allow access to next pages
**Describe the bug**
I've recently migrated my GoodReads library to bookwyrm and I've had 109 items failing to import.
When I go to that failed list location, https://bookwyrm.social/import/1151/failed, I see that there are 8 pages of failed results.
However, the list point to the imported list, not to the failures.

**To Reproduce**
Steps to reproduce the behavior:
1. Import a GoodReads CSV with many books missing from bookwyrm.
2. Click on 'View and troubleshoot failed items': https://bookwyrm.social/import/1151/failed
3. Hover over page two of the list
4. See link to page 2 of the whole import list: https://bookwyrm.social/import/1151?page=2

**Expected behavior**
The link of the next pages should include the failed path: https://bookwyrm.social/import/1151/failed?page=2
The navigation works correctly, it's just the link that is incorrect.


**Instance**
bookwyrm.social


Discussion:

Resolving patch:
diff --git a/bookwyrm/views/imports/troubleshoot.py b/bookwyrm/views/imports/troubleshoot.py
--- a/bookwyrm/views/imports/troubleshoot.py
+++ b/bookwyrm/views/imports/troubleshoot.py
@@ -5,6 +5,7 @@
 from django.shortcuts import get_object_or_404, redirect
 from django.template.response import TemplateResponse
 from django.utils.decorators import method_decorator
+from django.urls import reverse
 from django.views import View
 
 from bookwyrm import models
@@ -35,6 +36,7 @@ def get(self, request, job_id):
                 page.number, on_each_side=2, on_ends=1
             ),
             "complete": True,
+            "page_path": reverse("import-troubleshoot", args=[job.id]),
         }
 
         return TemplateResponse(request, "import/troubleshoot.html", data)


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Reviews of delete user still show up
**Describe the bug**
I have a username (treaz) which I've used to import my GoodReads book initially, but then I removed it completely.
With my current username (horiaconstantin), I redid the import again and I see that I have a doubled review.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to https://bookwyrm.social/book/210792#reviews
2. See that there are 2 reviews: June by my initial user (treaz) and one by my current user.

**Expected behavior**
Delete user reviews are discarded. Actually (might be relevant for the future), according to EU laws, once you remove your account from a platform, all the data should be deleted also. This doesn't seem to be the case right now.


Discussion:

Resolving patch:
diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py
--- a/bookwyrm/models/status.py
+++ b/bookwyrm/models/status.py
@@ -227,7 +227,7 @@ def raise_not_editable(self, viewer):
     @classmethod
     def privacy_filter(cls, viewer, privacy_levels=None):
         queryset = super().privacy_filter(viewer, privacy_levels=privacy_levels)
-        return queryset.filter(deleted=False)
+        return queryset.filter(deleted=False, user__is_active=True)
 
     @classmethod
     def direct_filter(cls, queryset, viewer):


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
localisation string for a shelf is missing
**Describe the bug**
A translation exists, but the string "Currently reading" is shown in English language

**To Reproduce**
Switch language to non-english and check book status

**Expected behavior**
Translated string used instead of English

**Screenshots**

![image](https://user-images.githubusercontent.com/81133/151704062-a99581a4-a941-4c6c-815a-62c7fd4885f6.png)

**Instance**
https://ziurkes.group.lt




Discussion:

Resolving patch:
diff --git a/bookwyrm/templatetags/shelf_tags.py b/bookwyrm/templatetags/shelf_tags.py
--- a/bookwyrm/templatetags/shelf_tags.py
+++ b/bookwyrm/templatetags/shelf_tags.py
@@ -1,5 +1,6 @@
 """ Filters and tags related to shelving books """
 from django import template
+from django.utils.translation import gettext_lazy as _
 
 from bookwyrm import models
 from bookwyrm.utils import cache
@@ -32,6 +33,24 @@ def get_next_shelf(current_shelf):
     return "to-read"
 
 
+@register.filter(name="translate_shelf_name")
+def get_translated_shelf_name(shelf):
+    """produced translated shelf nidentifierame"""
+    if not shelf:
+        return ""
+    # support obj or dict
+    identifier = shelf["identifier"] if isinstance(shelf, dict) else shelf.identifier
+    if identifier == "all":
+        return _("All books")
+    if identifier == "to-read":
+        return _("To Read")
+    if identifier == "reading":
+        return _("Currently Reading")
+    if identifier == "read":
+        return _("Read")
+    return shelf["name"] if isinstance(shelf, dict) else shelf.name
+
+
 @register.simple_tag(takes_context=True)
 def active_shelf(context, book):
     """check what shelf a user has a book on, if any"""


Test patch:
Repository: bookwyrm-social/bookwyrm

Issue:
Theme selection not working in production
On a closer scan, it's expected that the `compilescss` management command doesn't work with a variable for the scss file path, but the processor still ought to be able to produce the stylesheet on the fly. This is working locally (with or without s3 enabled) but not in production. 


Discussion:

Resolving patch:
diff --git a/bookwyrm/management/commands/compilethemes.py b/bookwyrm/management/commands/compilethemes.py
new file mode 100644
--- /dev/null
+++ b/bookwyrm/management/commands/compilethemes.py
@@ -0,0 +1,32 @@
+""" Compile themes """
+import os
+from django.contrib.staticfiles.utils import get_files
+from django.contrib.staticfiles.storage import StaticFilesStorage
+from django.core.files.base import ContentFile
+from django.core.management.base import BaseCommand
+
+import sass
+from sass_processor.processor import SassProcessor
+
+# pylint: disable=line-too-long
+class Command(BaseCommand):
+    """Compile themes"""
+
+    help = "Compile theme scss files"
+
+    # pylint: disable=no-self-use,unused-argument
+    def handle(self, *args, **options):
+        """compile themes"""
+        storage = StaticFilesStorage()
+        theme_files = list(get_files(storage, location="css/themes"))
+        theme_files = [t for t in theme_files if t[-5:] == ".scss"]
+        for filename in theme_files:
+            path = storage.path(filename)
+            content = sass.compile(
+                filename=path,
+                include_paths=SassProcessor.include_paths,
+            )
+            basename, _ = os.path.splitext(path)
+            destination_filename = basename + ".css"
+            print(f"saving f{destination_filename}")
+            storage.save(destination_filename, ContentFile(content))
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -189,10 +189,6 @@
 
 SASS_PROCESSOR_INCLUDE_FILE_PATTERN = r"^.+\.[s]{0,1}(?:a|c)ss$"
 
-SASS_PROCESSOR_INCLUDE_DIRS = [
-    os.path.join(BASE_DIR, ".css-config-sample"),
-]
-
 # minify css is production but not dev
 if not DEBUG:
     SASS_OUTPUT_STYLE = "compressed"


Test patch: