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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ local/
.vscode/tasks.json
.vscode/extensions.json

.gnupg
.ssh
.vscode-server

# Generated by Docker development containers
build/
Expand Down
5 changes: 5 additions & 0 deletions src/pretix/base/forms/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ class Meta:
model = User
fields = [
'fullname',
'wikimedia_username',
'locale',
'timezone',
'email'
]
labels = {
'wikimedia_username': 'Nick name',
}
widgets = {
'locale': SingleLanguageWidget
}
Expand All @@ -58,6 +62,7 @@ def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
self.fields['email'].required = True
self.fields['wikimedia_username'].widget.attrs['readonly'] = True
if self.user.auth_backend != 'native':
del self.fields['old_pw']
del self.fields['new_pw']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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'),
]

operations = [
migrations.AddField(
model_name='user',
name='wikimedia_username',
field=models.CharField(max_length=255, null=True),
),
]
5 changes: 5 additions & 0 deletions src/pretix/base/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class User(AbstractBaseUser, PermissionsMixin, LoggingMixin):
verbose_name=_('E-mail'), max_length=190)
fullname = models.CharField(max_length=255, blank=True, null=True,
verbose_name=_('Full name'))
wikimedia_username = models.CharField(max_length=255, blank=True, null=True,
verbose_name=('Wikimedia username'))
is_active = models.BooleanField(default=True,
verbose_name=_('Is active'))
is_staff = models.BooleanField(default=False,
Expand Down Expand Up @@ -162,10 +164,13 @@ def get_full_name(self) -> str:
Returns the first of the following user properties that is found to exist:

* Full name
* Wikimedia username
* Email address
"""
if self.fullname:
return self.fullname
elif self.wikimedia_username:
return self.wikimedia_username
else:
return self.email

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>{% trans "Account settings" %}</h1>
<fieldset>
<legend>{% trans "General settings" %}</legend>
{% bootstrap_field form.fullname layout='horizontal' %}
{% bootstrap_field form.wikimedia_username layout='horizontal' readonly='readonly' %}
{% bootstrap_field form.locale layout='horizontal' %}
{% bootstrap_field form.timezone layout='horizontal' %}
<div class="form-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>{% trans "Account settings" %}</h1>
<fieldset>
<legend>{% trans "General settings" %}</legend>
{% bootstrap_field form.fullname layout='horizontal' %}
{% bootstrap_field form.wikimedia_username layout='horizontal' readonly='readonly' %}
{% bootstrap_field form.locale layout='horizontal' %}
{% bootstrap_field form.timezone layout='horizontal' %}
<div class="form-group">
Expand Down
19 changes: 17 additions & 2 deletions src/pretix/plugins/socialauth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,30 @@ def get_or_create_user(request: HttpRequest) -> User:
"""
Get or create a user from social auth information.
"""
return User.objects.get_or_create(
social_account = request.user.socialaccount_set.filter(provider="mediawiki").last() # Fetch only the latest signed in Wikimedia account
wikimedia_username = ""

if social_account:
extra_data = social_account.extra_data
wikimedia_username = extra_data.get("username", extra_data.get("realname", ""))

user, created = User.objects.get_or_create(
email=request.user.email,
defaults={
"locale": getattr(request, "LANGUAGE_CODE", settings.LANGUAGE_CODE),
"timezone": getattr(request, "timezone", settings.TIME_ZONE),
"auth_backend": "native",
"password": "",
"wikimedia_username": wikimedia_username,
},
)[0]
)

# Update wikimedia_username if the user exists but has no wikimedia_username value set (Basically our existing users), or if the user has updated his username in his wikimedia account
if not created and (not user.wikimedia_username or user.wikimedia_username != wikimedia_username):
user.wikimedia_username = wikimedia_username
user.save()

return user


class SocialLoginView(AdministratorPermissionRequiredMixin, TemplateView):
Expand Down