internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: python-discord/site

Issue:
Django: Put application logs into logging handler



Discussion:

Resolving patch:
diff --git a/pydis_site/apps/api/dblogger.py b/pydis_site/apps/api/dblogger.py
new file mode 100644
--- /dev/null
+++ b/pydis_site/apps/api/dblogger.py
@@ -0,0 +1,22 @@
+from logging import LogRecord, StreamHandler
+
+
+class DatabaseLogHandler(StreamHandler):
+    """Logs entries into the database."""
+
+    def emit(self, record: LogRecord):
+        """Write the given `record` into the database."""
+        # This import needs to be deferred due to Django's application
+        # registry instantiation logic loading this handler before the
+        # application is ready.
+        from pydis_site.apps.api.models.log_entry import LogEntry
+
+        entry = LogEntry(
+            application='site',
+            logger_name=record.name,
+            level=record.levelname.lower(),
+            module=record.module,
+            line=record.lineno,
+            message=self.format(record)
+        )
+        entry.save()
diff --git a/pydis_site/settings.py b/pydis_site/settings.py
--- a/pydis_site/settings.py
+++ b/pydis_site/settings.py
@@ -227,11 +227,14 @@
     'handlers': {
         'console': {
             'class': 'logging.StreamHandler'
+        },
+        'database': {
+            'class': 'pydis_site.apps.api.dblogger.DatabaseLogHandler'
         }
     },
     'loggers': {
         'django': {
-            'handlers': ['console'],
+            'handlers': ['console', 'database'],
             'propagate': True,
             'level': env(
                 'LOG_LEVEL',


Test patch:
diff --git a/pydis_site/apps/api/tests/test_dblogger.py b/pydis_site/apps/api/tests/test_dblogger.py
new file mode 100644
--- /dev/null
+++ b/pydis_site/apps/api/tests/test_dblogger.py
@@ -0,0 +1,27 @@
+import logging
+from datetime import datetime
+
+from django.test import TestCase
+
+from ..dblogger import DatabaseLogHandler
+from ..models import LogEntry
+
+
+class DatabaseLogHandlerTests(TestCase):
+    def test_logs_to_database(self):
+        module_basename = __name__.split('.')[-1]
+        logger = logging.getLogger(__name__)
+        logger.handlers = [DatabaseLogHandler()]
+        logger.warning("I am a test case!")
+
+        # Ensure we only have a single record in the database
+        # after the logging call above.
+        [entry] = LogEntry.objects.all()
+
+        self.assertEqual(entry.application, 'site')
+        self.assertEqual(entry.logger_name, __name__)
+        self.assertIsInstance(entry.timestamp, datetime)
+        self.assertEqual(entry.level, 'warning')
+        self.assertEqual(entry.module, module_basename)
+        self.assertIsInstance(entry.line, int)
+        self.assertEqual(entry.message, "I am a test case!")
Repository: python-discord/site

Issue:
Ugly prefix on all ID links.
Currently, all the headers that are created by the wiki will have id's that are prefixed with `wiki-toc`. As such, when you want to link a header, the link will look something like https://pythondiscord.com/pages/contributing/site/#wiki-toc-development-environment.

It would be better if this simply said `#development-environment`, so let's change that.


Discussion:

Resolving patch:
diff --git a/pydis_site/__init__.py b/pydis_site/__init__.py
--- a/pydis_site/__init__.py
+++ b/pydis_site/__init__.py
@@ -0,0 +1,4 @@
+from wiki.plugins.macros.mdx import toc
+
+# Remove the toc header prefix. There's no option for this, so we gotta monkey patch it.
+toc.HEADER_ID_PREFIX = ''


Test patch:
Repository: python-discord/site

Issue:
Set up Sentry releases on site
This will allow us to see which commits introduce a specific Sentry event.


Discussion:

Resolving patch:
diff --git a/pydis_site/constants.py b/pydis_site/constants.py
new file mode 100644
--- /dev/null
+++ b/pydis_site/constants.py
@@ -0,0 +1,5 @@
+import git
+
+# Git SHA
+repo = git.Repo(search_parent_directories=True)
+GIT_SHA = repo.head.object.hexsha
diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py
new file mode 100644
--- /dev/null
+++ b/pydis_site/context_processors.py
@@ -0,0 +1,8 @@
+from django.template import RequestContext
+
+from pydis_site.constants import GIT_SHA
+
+
+def git_sha_processor(_: RequestContext) -> dict:
+    """Expose the git SHA for this repo to all views."""
+    return {'git_sha': GIT_SHA}
diff --git a/pydis_site/settings.py b/pydis_site/settings.py
--- a/pydis_site/settings.py
+++ b/pydis_site/settings.py
@@ -20,6 +20,7 @@
 from django.contrib.messages import constants as messages
 from sentry_sdk.integrations.django import DjangoIntegration
 
+from pydis_site.constants import GIT_SHA
 
 if typing.TYPE_CHECKING:
     from django.contrib.auth.models import User
@@ -33,7 +34,8 @@
 sentry_sdk.init(
     dsn=env('SITE_SENTRY_DSN'),
     integrations=[DjangoIntegration()],
-    send_default_pii=True
+    send_default_pii=True,
+    release=f"pydis-site@{GIT_SHA}"
 )
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@@ -157,8 +159,8 @@
                 'django.template.context_processors.static',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
-
                 "sekizai.context_processors.sekizai",
+                "pydis_site.context_processors.git_sha_processor"
             ],
         },
     },


Test patch:
Repository: python-discord/site

Issue:
Replace Allauth anti-email monkey-patch with proper settings.
The allauth extension we use for the discord login and connection to a github account for any of our site accounts.

We have a [monkeypatch](https://github.com/python-discord/site/blob/master/pydis_site/apps/home/apps.py#L17-L38) to avoid saving in our database any email details that we may get from oauth authorisations. This does not avoid the request for emails showing in the auth request page though, as the scope is still requested.

Instead, we can define in settings.py the appropriate provider settings, particularly any required scopes to be requested. If we only provide `identify` for the discord one, it won't add the `email` scope in the auth request page in future, making it a cleaner and more appropriate solution.

The setting would look like:
```py
SOCIALACCOUNT_PROVIDERS = {
    'discord': {
        'SCOPE': [
            'identify',
        ],
    }
}
```

The relevant scope setting for github can be given also, it just needs to be looked up as to what scopes we should restrict it to in order to avoid unnecessary sensitive data being stored.


Discussion:
The note on the [privacy page](https://pythondiscord.com/pages/privacy/) needs removing once we have got a PR merged for this.
This is what is probably causing the sentry issue [SITE-6](https://sentry.io/organizations/python-discord/issues/1539211531/?referrer=github_integration)
SITE-6 is not caused by this as far as I know. It is caused when you 
manually visit the OAuth2 callback URL without the information from Discord.

Well, yes, but from what I could gather on the Internet, the page is still supposed to be visited without any issue. 

Since the traceback message is `Reverse for 'account_email' not found.` and this view is supposed to be managed by AllAuth, I think this is causing it. 

Resolving patch:
diff --git a/pydis_site/settings.py b/pydis_site/settings.py
--- a/pydis_site/settings.py
+++ b/pydis_site/settings.py
@@ -401,3 +401,11 @@ def WIKI_CAN_WRITE(article: "Article", user: "User") -> bool:  # noqa: N802
 
 LOGIN_REDIRECT_URL = "home"
 SOCIALACCOUNT_ADAPTER = "pydis_site.utils.account.SocialAccountAdapter"
+SOCIALACCOUNT_PROVIDERS = {
+    "discord": {
+        "SCOPE": [
+            "identify",
+        ],
+        "AUTH_PARAMS": {"prompt": "none"}
+    }
+}


Test patch:
Repository: python-discord/site

Issue:
Unable to parse the GitHub repository metadata from response!
Sentry Issue: [SITE-P](https://sentry.io/organizations/python-discord/issues/2093966668/?referrer=github_integration)

```
Unable to parse the GitHub repository metadata from response!
```

This is caused by us hitting github ratelimits, as we're not authenticating with the API.


Discussion:

Resolving patch:
diff --git a/pydis_site/apps/home/views/home.py b/pydis_site/apps/home/views/home.py
--- a/pydis_site/apps/home/views/home.py
+++ b/pydis_site/apps/home/views/home.py
@@ -9,6 +9,7 @@
 from django.views import View
 
 from pydis_site.apps.home.models import RepositoryMetadata
+from pydis_site.constants import GITHUB_TOKEN
 
 log = logging.getLogger(__name__)
 
@@ -18,6 +19,7 @@ class HomeView(View):
 
     github_api = "https://api.github.com/users/python-discord/repos?per_page=100"
     repository_cache_ttl = 3600
+    headers = {"Authorization": f"token {GITHUB_TOKEN}"}
 
     # Which of our GitHub repos should be displayed on the front page, and in which order?
     repos = [
@@ -42,7 +44,7 @@ def _get_api_data(self) -> Dict[str, Dict[str, str]]:
         repo_dict = {}
 
         # Fetch the data from the GitHub API
-        api_data: List[dict] = requests.get(self.github_api).json()
+        api_data: List[dict] = requests.get(self.github_api, headers=self.headers).json()
 
         # Process the API data into our dict
         for repo in api_data:
diff --git a/pydis_site/constants.py b/pydis_site/constants.py
--- a/pydis_site/constants.py
+++ b/pydis_site/constants.py
@@ -1,3 +1,4 @@
 import os
 
 GIT_SHA = os.environ.get("GIT_SHA", "development")
+GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")


Test patch:
Repository: python-discord/site

Issue:
SystemExit: 1
Sentry Issue: [SITE-1G](https://sentry.io/organizations/python-discord/issues/2623186847/?referrer=github_integration)

```
SystemExit: 1
(36 additional frame(s) were not displayed)
...
  File "urllib3/connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
  File "gunicorn/workers/base.py", line 201, in handle_abort
    sys.exit(1)
```


Discussion:

Resolving patch:
diff --git a/pydis_site/apps/home/views/home.py b/pydis_site/apps/home/views/home.py
--- a/pydis_site/apps/home/views/home.py
+++ b/pydis_site/apps/home/views/home.py
@@ -9,7 +9,7 @@
 from django.views import View
 
 from pydis_site.apps.home.models import RepositoryMetadata
-from pydis_site.constants import GITHUB_TOKEN
+from pydis_site.constants import GITHUB_TOKEN, TIMEOUT_PERIOD
 
 log = logging.getLogger(__name__)
 
@@ -51,9 +51,16 @@ def _get_api_data(self) -> Dict[str, Dict[str, str]]:
         If we're unable to get that info for any reason, return an empty dict.
         """
         repo_dict = {}
-
-        # Fetch the data from the GitHub API
-        api_data: List[dict] = requests.get(self.github_api, headers=self.headers).json()
+        try:
+            # Fetch the data from the GitHub API
+            api_data: List[dict] = requests.get(
+                self.github_api,
+                headers=self.headers,
+                timeout=TIMEOUT_PERIOD
+            ).json()
+        except requests.exceptions.Timeout:
+            log.error("Request to fetch GitHub repository metadata for timed out!")
+            return repo_dict
 
         # Process the API data into our dict
         for repo in api_data:
diff --git a/pydis_site/constants.py b/pydis_site/constants.py
--- a/pydis_site/constants.py
+++ b/pydis_site/constants.py
@@ -2,3 +2,5 @@
 
 GIT_SHA = os.environ.get("GIT_SHA", "development")
 GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
+# How long to wait for synchronous requests before timing out
+TIMEOUT_PERIOD = int(os.environ.get("TIMEOUT_PERIOD", 5))


Test patch:
Repository: python-discord/site

Issue:
ValueError: Unknown format code 'X' for object of type 'str'
Sentry Issue: [SITE-25](https://sentry.io/organizations/python-discord/issues/3211854811/?referrer=github_integration)

```
ValueError: Unknown format code 'X' for object of type 'str'
(16 additional frame(s) were not displayed)
...
  File "django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "django/template/base.py", line 988, in render
    output = self.filter_expression.resolve(context)
  File "django/template/base.py", line 698, in resolve
    new_obj = func(obj, *arg_vals)
  File "pydis_site/apps/staff/templatetags/deletedmessage_filters.py", line 15, in hex_colour
    colour = f"#{color:0>6X}"
```


Discussion:

Resolving patch:
diff --git a/pydis_site/apps/staff/templatetags/deletedmessage_filters.py b/pydis_site/apps/staff/templatetags/deletedmessage_filters.py
--- a/pydis_site/apps/staff/templatetags/deletedmessage_filters.py
+++ b/pydis_site/apps/staff/templatetags/deletedmessage_filters.py
@@ -1,4 +1,5 @@
 from datetime import datetime
+from typing import Union
 
 from django import template
 
@@ -6,13 +7,16 @@
 
 
 @register.filter
-def hex_colour(color: int) -> str:
+def hex_colour(colour: Union[str, int]) -> str:
     """
-    Converts an integer representation of a colour to the RGB hex value.
+    Converts the given representation of a colour to its RGB hex string.
 
     As we are using a Discord dark theme analogue, black colours are returned as white instead.
     """
-    colour = f"#{color:0>6X}"
+    if isinstance(colour, str):
+        colour = colour if colour.startswith("#") else f"#{colour}"
+    else:
+        colour = f"#{colour:0>6X}"
     return colour if colour != "#000000" else "#FFFFFF"
 
 


Test patch:
diff --git a/pydis_site/apps/staff/tests/test_logs_view.py b/pydis_site/apps/staff/tests/test_logs_view.py
--- a/pydis_site/apps/staff/tests/test_logs_view.py
+++ b/pydis_site/apps/staff/tests/test_logs_view.py
@@ -95,12 +95,22 @@ def setUpTestData(cls):
             "description": "This embed is way too cool to be seen in public channels.",
         }
 
+        cls.embed_three = {
+            "description": "This embed is way too cool to be seen in public channels.",
+            "color": "#e74c3c",
+        }
+
+        cls.embed_four = {
+            "description": "This embed is way too cool to be seen in public channels.",
+            "color": "e74c3c",
+        }
+
         cls.deleted_message_two = DeletedMessage.objects.create(
             author=cls.author,
             id=614444836291870750,
             channel_id=1984,
             content='Does that mean this thing will halt?',
-            embeds=[cls.embed_one, cls.embed_two],
+            embeds=[cls.embed_one, cls.embed_two, cls.embed_three, cls.embed_four],
             attachments=['https://http.cat/100', 'https://http.cat/402'],
             deletion_context=cls.deletion_context,
         )
Repository: python-discord/site

Issue:
Missing API response for `last_applied` field
The added `last_applied` attribute in #751 currently is not in the API response.


Discussion:

Resolving patch:
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py
--- a/pydis_site/apps/api/serializers.py
+++ b/pydis_site/apps/api/serializers.py
@@ -176,6 +176,7 @@ class Meta:
         fields = (
             'id',
             'inserted_at',
+            'last_applied',
             'expires_at',
             'active',
             'user',


Test patch:
Repository: python-discord/site

Issue:
Add some more common abbreviations to the rule keyword command list
I believe it would be beneficial to add two more shorthands for invoking the 'Rules' embed: "hw" and "eng". "hw" is a common shorthand for "homework", so it should be associated with the embed for rule 8. Likewise, "eng" is a common abbreviation for "English", so it can be linked with rule 4.


Discussion:
The rule abbreviations are stored here https://github.com/python-discord/site/blob/3ee7d072b74b850f798dc19b0205f7073899ac21/pydis_site/apps/api/views.py#L133, so i'll move this to the site repo.

As for adding them, I don't really have an opinion either way, but it sounds fine.

Resolving patch:
diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py
--- a/pydis_site/apps/api/views.py
+++ b/pydis_site/apps/api/views.py
@@ -146,7 +146,7 @@ def get(self, request, format=None):  # noqa: ANN001, ANN201
             (
                 "Use English to the best of your ability. "
                 "Be polite if someone speaks English imperfectly.",
-                ["english", "language"]
+                ["english", "eng", "language"]
             ),
             (
                 "Do not provide or request help on projects that may violate terms of service, "
@@ -165,15 +165,15 @@ def get(self, request, format=None):  # noqa: ANN001, ANN201
             (
                 "Do not help with ongoing exams. When helping with homework, "
                 "help people learn how to do the assignment without doing it for them.",
-                ["exam", "exams", "assignment", "assignments", "homework"]
+                ["exam", "exams", "assignment", "assignments", "homework", "hw"]
             ),
             (
                 "Do not offer or ask for paid work of any kind.",
                 ["paid", "work", "money"]
             ),
             (
-              "Do not copy and paste answers from ChatGPT or similar AI tools.",
-              ["gpt", "chatgpt", "gpt3", "ai"]
+                "Do not copy and paste answers from ChatGPT or similar AI tools.",
+                ["gpt", "chatgpt", "gpt3", "ai"]
             ),
         ])
 


Test patch:
Repository: python-discord/site

Issue:
A filter list named redirect was loaded from the database, but no matching class.
Sentry Issue: [BOT-3FA](https://python-discord.sentry.io/issues/4067380416/?referrer=github_integration)

```
A filter list named redirect was loaded from the database, but no matching class.
```


Discussion:

Resolving patch:
diff --git a/pydis_site/apps/api/migrations/0092_remove_redirect_filter_list.py b/pydis_site/apps/api/migrations/0092_remove_redirect_filter_list.py
new file mode 100644
--- /dev/null
+++ b/pydis_site/apps/api/migrations/0092_remove_redirect_filter_list.py
@@ -0,0 +1,16 @@
+from django.db import migrations
+from django.apps.registry import Apps
+from django.db.backends.base.schema import BaseDatabaseSchemaEditor
+
+
+def forward(apps: Apps, _: BaseDatabaseSchemaEditor) -> None:
+    apps.get_model("api", "FilterList").objects.filter(name="redirect").delete()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("api", "0091_antispam_filter_list"),
+    ]
+
+    operations = [migrations.RunPython(forward)]


Test patch:
Repository: python-discord/site

Issue:
Re-work our main events page
# Proposal
Our current [events page](https://pythondiscord.com/events/) could be improved to draw in more engagement, clearly showcase the upcoming events, and have a stronger call to action for people to join and find the information they're looking for. 

The below image illustrates what I'd like to do.
![EventPage MockUp](https://user-images.githubusercontent.com/20641196/118335723-40edd580-b4de-11eb-9f4d-fda2b82ed6cb.jpg)
- A clear intro blurb briefly describing our events and providing a link to go to the server
- A center showcase of our upcoming events with a strong call-to-action to either join or find out more information
- A nice grid layout of our events with buttons that will either "Show More" info that will pop out a modal that provides more information or a "View Event Page" button that will bring you to a dedicated event page (mostly to be used for the code jams). 
- A nice ToC on the side to give an overview of what there is on the page

These are only ideas though and I'm more than happy with the final design looking different as long as it achieves the following objectives:
- Clear information that can be found via skimming
- Upcoming events are easily found with links for how to join / find out more info
- It's easy to see what events we host and generally when they occur



Discussion:
I can do this.
Is this still being worked on?
Yes, I'm working on it.
Hey @dawnofmidnight are you planning to continue work on this?
> Hey @dawnofmidnight are you planning to continue work on this?

I am not, no. Would you like to?
> > Hey @dawnofmidnight are you planning to continue work on this?
> 
> 
> 
> I am not, no. Would you like to?

Yes, that'd be great 😄 
Hi @doublevcodes, are you still interested in working on this? If not I would put this up for grabs :slightly_smiling_face:

Resolving patch:
diff --git a/pydis_site/settings.py b/pydis_site/settings.py
--- a/pydis_site/settings.py
+++ b/pydis_site/settings.py
@@ -323,6 +323,7 @@
         "primary": "#5365A4",         # A darker PyDis blurple
         "warning": "#4B4636",
         "warning-invert": "#FFFFFF",
+        "warning-light": "#3B3B36",
         "primary-dark": "#EFF1FB",    # Bulma's primary-light
         "primary-light": "#2B3660",
         "success-dark": "#EFFAF5",    # Bulma's success-light
@@ -347,6 +348,7 @@
         "black-ter": "#E6E6E6",
         "dark": "$black",
 
+        "grey": "#AFAFAF",
         "grey-darker": "#303032",
 
         "text": "#F4F4F4",


Test patch:
Repository: redis/redis-py

Issue:
SentinelConnectionPool. Error when working under uwsgi
It raises following exception:

```
redis.sentinel in get_master_address
AttributeError: 'int' object has no attribute 'discover_master'
```

The reason is `ConnectionPool._checkpid()` calls `__init__()` ([connection.py#L397](https://github.com/andymccurdy/redis-py/blob/7210906be09b969e5833de5a967178c4d6989f14/redis/connection.py#L397)) with 
`self.connection_class, self.max_connections`
instead
`self.service_name, self.sentinel_manager`.



Discussion:

Resolving patch:
diff --git a/redis/sentinel.py b/redis/sentinel.py
--- a/redis/sentinel.py
+++ b/redis/sentinel.py
@@ -1,3 +1,4 @@
+import os
 import random
 
 from redis.client import StrictRedis
@@ -90,6 +91,14 @@ def rotate_slaves(self):
             pass
         raise SlaveNotFoundError('No slave found for %r' % (self.service_name))
 
+    def _checkpid(self):
+        if self.pid != os.getpid():
+            self.disconnect()
+            self.__init__(self.service_name, self.sentinel_manager,
+                          connection_class=self.connection_class,
+                          max_connections=self.max_connections,
+                          **self.connection_kwargs)
+
 
 class Sentinel(object):
     """


Test patch:
Repository: redis/redis-py

Issue:
bad data from out-of-sync connection after "Wrong number of response items from pipeline execution"
I had a redis server that was stopped and restarted, and as redis-py 2.7.2 clients tried to get data from it in pipelines (possibly while it was still coming up) they started to get the "Wrong number of response items from pipeline execution" error.

During this time we also received bad data on other pipeline requests, which could only be explained by receiving data associated with earlier pipeline requests that failed.

This seems associated with https://github.com/andymccurdy/redis-py/issues/185 although not necessarily tied to a timeout as described in that issue.

In general it would seem that anytime this condition arises that the number of responses to a pipeline request doesn't equal the number of requests that the connection becomes suspect and should be disconnected to prevent other users of that same connection to get questionable data.



Discussion:

Resolving patch:
diff --git a/redis/client.py b/redis/client.py
--- a/redis/client.py
+++ b/redis/client.py
@@ -2061,6 +2061,7 @@ def _execute_transaction(self, connection, commands, raise_on_error):
             response.insert(i, e)
 
         if len(response) != len(commands):
+            self.connection.disconnect()
             raise ResponseError("Wrong number of response items from "
                                 "pipeline execution")
 


Test patch: