Skip to content
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

Add Wikimedia Nick Name field #582

Merged

Conversation

Gagan-Ram
Copy link
Contributor

@Gagan-Ram Gagan-Ram commented Mar 15, 2025

This PR updates the user model to include the wikimedia_username field.
Fixes: #558

Changes Made

  • Added a wikimedia_username field to the user model.
  • Updated the account settings UI to include the nickname field and set it to read-only.

Potential Issues / Considerations

  • Since Wikimedia uses the username as the primary key, a single user can create multiple Wikimedia accounts with the same email ID.
  • If a user signs in to our platform using multiple Wikimedia accounts linked to the same email, their nickname will be updated to the latest Wikimedia account they used.
  • However, they won’t be able to sign in as multiple separate users on our platform using those duplicate Wikimedia accounts.

Screenshot after the update

Account settings eventyay-common

Summary by Sourcery

Adds a wikimedia_username field to the user model and displays it as a read-only "Nick name" field in the account settings UI. The field is automatically populated when a user logs in via Wikimedia.

Enhancements:

  • Populate the wikimedia_username field upon user creation or update if the user logs in via Wikimedia.
  • Display the Wikimedia username as a read-only "Nick name" field in the account settings UI.

Copy link
Contributor

sourcery-ai bot commented Mar 15, 2025

Reviewer's Guide by Sourcery

This PR introduces the wikimedia_username field to the user model. It updates the user model, account settings UI, and the social auth view to populate this field upon login via Wikimedia. The field is displayed as a read-only 'Nick name' in the account settings. Additionally, it updates migrations in banktransfer, ticketoutputpdf, and helpers.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Added a wikimedia_username field to the user model.
  • Added wikimedia_username field to the User model in pretix/base/models/auth.py.
  • Created a database migration to add the wikimedia_username field.
  • Updated the get_full_name method to return wikimedia_username if fullname is not available.
src/pretix/base/models/auth.py
src/pretix/base/migrations/0009_user_wikimedia_username_and_more.py
Updated the account settings UI to include the nickname field and set it to read-only.
  • Added wikimedia_username field to the UserForm in pretix/base/forms/user.py.
  • Set the wikimedia_username field to read-only in the UserForm.
  • Added the wikimedia_username field to the account settings templates.
  • Updated the label for the wikimedia_username field to 'Nick name'.
src/pretix/base/forms/user.py
src/pretix/control/templates/pretixcontrol/user/settings.html
src/pretix/eventyay_common/templates/eventyay_common/account/settings.html
Populate and update the wikimedia_username field upon user login via Wikimedia.
  • Fetch the latest signed-in Wikimedia account.
  • Extract the Wikimedia username from the social account's extra data.
  • Set the wikimedia_username when creating a new user.
  • Update the wikimedia_username if it's empty or has been updated in the Wikimedia account.
src/pretix/plugins/socialauth/views.py
Updated migrations in banktransfer, ticketoutputpdf, and helpers.
  • Updated the migrations in banktransfer to use BigAutoField.
  • Updated the migrations in ticketoutputpdf to use BigAutoField.
  • Updated the migrations in helpers to use BigAutoField.
src/pretix/plugins/banktransfer/migrations/0003_alter_bankimportjob_id_alter_banktransaction_id_and_more.py
src/pretix/plugins/ticketoutputpdf/migrations/0002_alter_ticketlayout_id_alter_ticketlayoutitem_id.py
src/pretix/helpers/migrations/0002_alter_thumbnail_id.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Gagan-Ram - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a unique constraint to the wikimedia_username field to prevent multiple users from having the same username.
  • The migration file includes changes to all models, but it should only include changes related to the User model.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

name='wikimedia_username',
field=models.CharField(max_length=255, null=True),
),
migrations.AlterField(
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider using a loop to generate the AlterField operations for each model name to reduce repetition and improve maintainability of the migration file.

You can reduce this repetition by listing all affected model names and generating the corresponding operations in a loop. For example, instead of manually writing a block for each model, you could write:

# Generated by Django 5.1.7 on 2025-03-15 09:17
from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('pretixbase', '0008_cartposition_job_title_orderposition_job_title'),
    ]

    def generate_alter_field_operations():
        model_names = [
            'cachedcombinedticket', 'cachedticket', 'cancellationrequest', 'cartposition', 'checkin',
            'checkinlist', 'device', 'event', 'event_settingsstore', 'eventmetaproperty', 'eventmetavalue',
            'gate', 'giftcard', 'giftcardacceptance', 'giftcardtransaction', 'globalsettingsobject_settingsstore',
            'invoice', 'invoiceaddress', 'invoiceline', 'item', 'itemaddon', 'itembundle', 'itemcategory',
            'itemmetaproperty', 'itemmetavalue', 'itemvariation', 'logentry', 'notificationsetting', 'order',
            'orderfee', 'orderpayment', 'orderposition', 'orderrefund', 'organizer', 'organizer_settingsstore',
            'question', 'questionanswer', 'questionoption', 'quota', 'requiredaction', 'revokedticketsecret',
            'seat', 'seatcategorymapping', 'seatingplan', 'staffsession', 'staffsessionauditlog', 'subevent',
            'subeventitem', 'subeventitemvariation', 'subeventmetavalue', 'taxrule', 'team', 'teamapitoken',
            'teaminvite', 'u2fdevice', 'user', 'voucher', 'waitinglistentry', 'webauthndevice'
        ]
        return [
            migrations.AlterField(
                model_name=model_name,
                name='id',
                field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False),
            )
            for model_name in model_names
        ]

    operations = [
        migrations.AddField(
            model_name='user',
            name='wikimedia_username',
            field=models.CharField(max_length=255, null=True),
        ),
    ] + generate_alter_field_operations()

This refactoring retains all functionality while cutting down on redundancy and improving maintainability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. We don't let user to modify the wikimedia_username so not adding unique constraint should be okay.
  2. The migration files are autogenerated when I ran python manage.py makemigrations command

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, I understand.

Regarding your points:

  1. wikimedia_username and unique constraint: You're right, since the wikimedia_username is not intended to be modified by the user and is populated automatically upon login via Wikimedia, adding a unique constraint might not be necessary and could even cause issues if, for some reason, the same username is associated with multiple accounts in your system (although your code prevents multiple logins).

  2. Autogenerated migration files: I understand that the migration files are autogenerated. My suggestion was aimed at improving the structure and maintainability of the generated code. While the code works as is, refactoring the migration to reduce repetition can make it easier to understand and modify in the future. However, if the autogeneration process is likely to overwrite manual changes frequently, then my suggestion might not be practical.

Given that the migration files are autogenerated, and the wikimedia_username field doesn't need a unique constraint, it's perfectly reasonable to keep the migration as is. My suggestion was more of a "nice-to-have" rather than a critical change.

@Gagan-Ram
Copy link
Contributor Author

@hongquan could you please look into this

@hongquan
Copy link
Member

hongquan commented Mar 18, 2025

@Gagan-Ram Please delete all DB migration AlterField lines which are not related to your changes of model.

You said that they are generated when you ran python manage.py makemigrations. There are two possible causes:

  • Your localhost environment was damaged.
  • The existing DB migration files are broken.

We need time to check and determine which is the case. In the meantime, to prevent the codebase from being broken if the cause is the 1st case, you need to delete those lines from this PR.

@Gagan-Ram Gagan-Ram force-pushed the feature/558-add-nickname-field branch from 3caeb84 to cad6020 Compare March 19, 2025 10:12
@Gagan-Ram
Copy link
Contributor Author

Gagan-Ram commented Mar 19, 2025

@hongquan I noticed that Django is automatically applying BigAutoField because of DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' in settings.py.
I also identified the models related to the User model:
EmailAddress, LogEntry, NotificationSetting, OAuthAccessToken, OAuthApplication, OAuthGrant, OAuthIDToken, OAuthRefreshToken, RequiredAction, SocialAccount, StaffSession, StaffSessionAuditLog, StaticDevice, TOTPDevice, Team, U2FDevice, WebAuthnDevice.
However, none of these models are affected when the User model is modified.
So I am deleting all migrations which is altering the field 'id' into BigAutoField.

@hongquan
Copy link
Member

Of course we know that Django applies BigAutoField because of the settings. The problem is that it shouldn't generate those AlterField lines, because those fields should "already" be BigAutoField before your PR.

@Gagan-Ram
Copy link
Contributor Author

It seems like we still need to figure out the problem.
By the way, does the PR look good now? I've deleted the AlterField lines.

@hongquan
Copy link
Member

Hi, I added a comment yesterday but forgot to submit. Please check.

@Gagan-Ram Gagan-Ram force-pushed the feature/558-add-nickname-field branch from cad6020 to b48226d Compare March 20, 2025 08:35
@hongquan hongquan merged commit 2e373bf into fossasia:development Mar 21, 2025
@Gagan-Ram Gagan-Ram deleted the feature/558-add-nickname-field branch March 21, 2025 08:42
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.

Add Nick Name field into account settings
2 participants