Skip to content

Commit b48226d

Browse files
committed
fix: #558 Add Nick Name field into account settings
1 parent debdd9e commit b48226d

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ local/
3232
.vscode/tasks.json
3333
.vscode/extensions.json
3434

35+
.gnupg
36+
.ssh
37+
.vscode-server
3538

3639
# Generated by Docker development containers
3740
build/

src/pretix/base/forms/user.py

+5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ class Meta:
4646
model = User
4747
fields = [
4848
'fullname',
49+
'wikimedia_username',
4950
'locale',
5051
'timezone',
5152
'email'
5253
]
54+
labels = {
55+
'wikimedia_username': 'Nick name',
56+
}
5357
widgets = {
5458
'locale': SingleLanguageWidget
5559
}
@@ -58,6 +62,7 @@ def __init__(self, *args, **kwargs):
5862
self.user = kwargs.pop('user')
5963
super().__init__(*args, **kwargs)
6064
self.fields['email'].required = True
65+
self.fields['wikimedia_username'].widget.attrs['readonly'] = True
6166
if self.user.auth_backend != 'native':
6267
del self.fields['old_pw']
6368
del self.fields['new_pw']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.1.7 on 2025-03-15 09:17
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('pretixbase', '0008_cartposition_job_title_orderposition_job_title'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='user',
15+
name='wikimedia_username',
16+
field=models.CharField(max_length=255, null=True),
17+
),
18+
]

src/pretix/base/models/auth.py

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class User(AbstractBaseUser, PermissionsMixin, LoggingMixin):
8787
verbose_name=_('E-mail'), max_length=190)
8888
fullname = models.CharField(max_length=255, blank=True, null=True,
8989
verbose_name=_('Full name'))
90+
wikimedia_username = models.CharField(max_length=255, blank=True, null=True,
91+
verbose_name=('Wikimedia username'))
9092
is_active = models.BooleanField(default=True,
9193
verbose_name=_('Is active'))
9294
is_staff = models.BooleanField(default=False,
@@ -162,10 +164,13 @@ def get_full_name(self) -> str:
162164
Returns the first of the following user properties that is found to exist:
163165
164166
* Full name
167+
* Wikimedia username
165168
* Email address
166169
"""
167170
if self.fullname:
168171
return self.fullname
172+
elif self.wikimedia_username:
173+
return self.wikimedia_username
169174
else:
170175
return self.email
171176

src/pretix/control/templates/pretixcontrol/user/settings.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ <h1>{% trans "Account settings" %}</h1>
1010
<fieldset>
1111
<legend>{% trans "General settings" %}</legend>
1212
{% bootstrap_field form.fullname layout='horizontal' %}
13+
{% bootstrap_field form.wikimedia_username layout='horizontal' readonly='readonly' %}
1314
{% bootstrap_field form.locale layout='horizontal' %}
1415
{% bootstrap_field form.timezone layout='horizontal' %}
1516
<div class="form-group">

src/pretix/eventyay_common/templates/eventyay_common/account/settings.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ <h1>{% trans "Account settings" %}</h1>
1010
<fieldset>
1111
<legend>{% trans "General settings" %}</legend>
1212
{% bootstrap_field form.fullname layout='horizontal' %}
13+
{% bootstrap_field form.wikimedia_username layout='horizontal' readonly='readonly' %}
1314
{% bootstrap_field form.locale layout='horizontal' %}
1415
{% bootstrap_field form.timezone layout='horizontal' %}
1516
<div class="form-group">

src/pretix/plugins/socialauth/views.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,30 @@ def get_or_create_user(request: HttpRequest) -> User:
104104
"""
105105
Get or create a user from social auth information.
106106
"""
107-
return User.objects.get_or_create(
107+
social_account = request.user.socialaccount_set.filter(provider="mediawiki").last() # Fetch only the latest signed in Wikimedia account
108+
wikimedia_username = ""
109+
110+
if social_account:
111+
extra_data = social_account.extra_data
112+
wikimedia_username = extra_data.get("username", extra_data.get("realname", ""))
113+
114+
user, created = User.objects.get_or_create(
108115
email=request.user.email,
109116
defaults={
110117
"locale": getattr(request, "LANGUAGE_CODE", settings.LANGUAGE_CODE),
111118
"timezone": getattr(request, "timezone", settings.TIME_ZONE),
112119
"auth_backend": "native",
113120
"password": "",
121+
"wikimedia_username": wikimedia_username,
114122
},
115-
)[0]
123+
)
124+
125+
# 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
126+
if not created and (not user.wikimedia_username or user.wikimedia_username != wikimedia_username):
127+
user.wikimedia_username = wikimedia_username
128+
user.save()
129+
130+
return user
116131

117132

118133
class SocialLoginView(AdministratorPermissionRequiredMixin, TemplateView):

0 commit comments

Comments
 (0)