Skip to content

Set up forms app with authentication #1497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: forms
Choose a base branch
from
Open

Conversation

jchristgit
Copy link
Member

This begins the work of integrating the forms backend into the site.
Changes, until complete, will be merged into the forms tracking
branch.

@jchristgit jchristgit force-pushed the forms-bootstrap branch 2 times, most recently from 0a51298 to 8b6dbeb Compare March 27, 2025 19:08
@jb3 jb3 requested a review from Copilot April 6, 2025 21:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 18 out of 19 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • .coveragerc: Language not supported
Comments suppressed due to low confidence (1)

pydis_site/apps/forms/tests/test_api.py:127

  • [nitpick] Consider revising or removing the informal comment to maintain a professional tone in test files.
# the ultimate power trip...

@jchristgit
Copy link
Member Author

The next person who sends "co""pilot" A"I" onto my review will have me send a Bengalese Tiger to their doorstep. And it will be very, very hungry.

@jchristgit jchristgit requested a review from MarkKoz as a code owner July 19, 2025 19:24
@python-discord-policy-bot python-discord-policy-bot bot requested a review from a team July 19, 2025 19:24
@jchristgit jchristgit requested review from ChrisLovering and jb3 July 19, 2025 19:25
This begins the work of integrating the forms backend into the site.
Changes, until complete, will be merged into the `forms` tracking
branch.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://discord.com/developers/docs/reference#user-agent

We should probably add our user agent in here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need:

  • Nearly as much role information as we are currently storing. We use this almost solely for calculating scopes and can probably get away with name and ID (if even that, most of this data is validated on the fly)
  • We don't need member information, just user information and whether they are in the guild or not.

As a sample of a filled response object:

{
        "_id" : "7d880da8-0f06-4f4e-bc1d-9987eefa0d51",
        "user" : {
                "username" : "...",
                "id" : "...",
                "discriminator" : "...",
                "avatar" : "...",
                "bot" : null,
                "system" : null,
                "locale" : "en-US",
                "verified" : null,
                "email" : null,
                "flags" : 64,
                "premium_type" : 2,
                "public_flags" : 64,
                "admin" : false
        },
        "antispam" : {
                "ip_hash" : "...",
                "user_agent_hash" : "...",
                "captcha_pass" : false
        },
        "response" : {
                "header" : false,
                "question" : "...",
                "attribution" : "Yes"
        },
        "form_id" : "qa",
        "timestamp" : "..."
}

We don't currently store users and I don't know if we should start storing users, sounds like a bit of a headache for data protection if we have another store of user data that is separate from form responses.

models.BigIntegerField(
help_text="Total permissions of the member in the channel.",
null=True,
verbose_name="Pending screening",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-paste typo.

)


# XXX: This duplicates the role object from the API app. The role object in the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can definitely get away with the role information in the API app.

@@ -0,0 +1,2 @@

# Register your models here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of this file if it's empty anyway?

class FormsConfig(AppConfig):
"""Django AppConfig for the forms app."""

name = "pydis_site.apps.forms"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to match the module name? Can we avoid hardcoding the name? Maybe something with importlib can help?


def fetch_member_details(member_id: int) -> models.DiscordMember | None:
"""Get a member by ID from the configured guild using the discord API."""
with httpx.Client() as client:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expensive to create a new client for each request?



def fetch_member_details(member_id: int) -> models.DiscordMember | None:
"""Get a member by ID from the configured guild using the discord API."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Get a member by ID from the configured guild using the discord API."""
"""Get a member by ID from the configured guild using the Discord API."""


def get_roles(*, force_refresh: bool = False, stale_after: int = 60 * 60 * 24) -> tuple[models.DiscordRole, ...]:
"""
Get a tuple of all roles from the cache, or discord API if not available.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Get a tuple of all roles from the cache, or discord API if not available.
Get a tuple of all roles from the cache, or Discord API if not available.


## Keyword arguments

- `force_refresh` (`bool`): Skip the cache and always update the roles from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think putting the type in the docs is redundant when we're using type annotations.

Comment on lines +127 to +128
- `stale_after` (`int`): Seconds after which to consider the stored roles
as stale and to refresh them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument does not exist for this function.

@property
def display_name(self) -> str:
"""Return username and discriminator as display name."""
return f"{self.payload['username']}#{self.payload['discriminator']}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't Discord do away with discriminators?


def is_admin(self) -> bool:
"""Return whether this user is an administrator."""
self.admin = models.Admin.objects.filter(id=self.payload["id"]).exists()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should self.admin be "private"? Currently, if someone inspects self.admin directly without calling is_admin(), its value may not be correct. The requirement to call is_admin() first is not obvious.

user = FormsUser(
token,
user_details,
discord.get_member(user_details["id"]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need error handling for this call or is it fine to let the exception bubble up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants