From 847f4bea92724c35bf05a460cc5dea2f5a22d273 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 25 Aug 2022 02:16:28 +0000 Subject: [PATCH 1/2] Collect preferred name on sign up and use it --- apiserver/apiserver/api/serializers.py | 13 +++++++------ apiserver/apiserver/api/utils.py | 11 ++++++----- apiserver/apiserver/api/utils_email.py | 6 +++--- apiserver/apiserver/api/utils_ldap.py | 6 +++--- apiserver/apiserver/api/views.py | 10 +++++----- webclient/src/Account.js | 2 +- webclient/src/Admin.js | 4 ++-- webclient/src/AdminMembers.js | 4 ---- webclient/src/LoginSignup.js | 24 ++++++++++++++++++------ 9 files changed, 45 insertions(+), 35 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index e77d648..f9951f3 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -296,7 +296,7 @@ class MemberSerializer(serializers.ModelSerializer): index__owner_id=instance.id, index__history_date__gte=ONE_WEEK, ).count() >= 6: - msg = 'Member allow_last_scanned rate limit exceeded by: ' + instance.first_name + ' ' + instance.last_name + msg = 'Member allow_last_scanned rate limit exceeded by: ' + instance.preferred_name + ' ' + instance.last_name utils.alert_tanner(msg) logger.info(msg) raise ValidationError(dict(allow_last_scanned='You\'re doing that too often.')) @@ -726,6 +726,7 @@ class UserSerializer(serializers.ModelSerializer): class MyRegisterSerializer(RegisterSerializer): first_name = serializers.CharField(max_length=32) last_name = serializers.CharField(max_length=32) + preferred_name = serializers.CharField(max_length=32) request_id = serializers.CharField(required=False) def validate_username(self, username): @@ -770,7 +771,7 @@ class MyPasswordChangeSerializer(PasswordChangeSerializer): username=self.user.username, password=self.data['new_password1'], email=self.user.email, - first_name=self.user.member.first_name, + first_name=self.user.member.preferred_name, ) data['username'] = self.user.member.mediawiki_username or self.user.username @@ -811,7 +812,7 @@ class MyPasswordResetSerializer(PasswordResetSerializer): def save(self): email = self.data['email'] member = User.objects.get(email__iexact=email).member - logging.info('Password reset requested for: {} - {} {} ({})'.format(email, member.first_name, member.last_name, member.id)) + logging.info('Password reset requested for: {} - {} {} ({})'.format(email, member.preferred_name, member.last_name, member.id)) super().save() class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer): @@ -835,7 +836,7 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer): username=self.user.username, password=self.data['new_password1'], email=self.user.email, - first_name=self.user.member.first_name, + first_name=self.user.member.preferred_name, ) data['username'] = self.user.member.mediawiki_username or self.user.username @@ -862,7 +863,7 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer): self.user.member.save() member = self.user.member - logging.info('Password reset completed for: {} {} ({})'.format(member.first_name, member.last_name, member.id)) + logging.info('Password reset completed for: {} {} ({})'.format(member.preferred_name, member.last_name, member.id)) if request_id: utils_stats.set_progress(request_id, 'Success! You can now log in as: ' + self.user.username) @@ -912,7 +913,7 @@ class SpaceportAuthSerializer(LoginSerializer): if user: data = self.context['request'].data.copy() data['email'] = user.email - data['first_name'] = user.member.first_name + data['first_name'] = user.member.preferred_name data['username'] = user.member.mediawiki_username or user.username utils_auth.set_wiki_password(data) diff --git a/apiserver/apiserver/api/utils.py b/apiserver/apiserver/api/utils.py index 6c9c72d..7f6fa91 100644 --- a/apiserver/apiserver/api/utils.py +++ b/apiserver/apiserver/api/utils.py @@ -258,7 +258,7 @@ def gen_card_photo(member): font = ImageFont.truetype('DejaVuSans.ttf', font_sizes[0]) x = CARD_PHOTO_MARGIN_SIDE y = my + CARD_PHOTO_MARGIN_TOP + CARD_PHOTO_MARGIN_SIDE - draw.text((x, y), str(member.first_name), (0,0,0), font=font) + draw.text((x, y), str(member.preferred_name), (0,0,0), font=font) font = ImageFont.truetype('DejaVuSans-Bold.ttf', font_sizes[1]) y = my + CARD_PHOTO_MARGIN_TOP + CARD_PHOTO_MARGIN_SIDE + font_sizes[1] @@ -339,13 +339,14 @@ def create_new_member(data, user): user=user, first_name=data['first_name'], last_name=data['last_name'], - preferred_name=data['first_name'], + preferred_name=data['preferred_name'], ) def register_user(data, user): data = data.copy() data['first_name'] = data['first_name'].title().strip() data['last_name'] = data['last_name'].title().strip() + data['preferred_name'] = data['preferred_name'].title().strip() # Sometimes during demos, a user makes a fake account then then has to be cleaned out # Notify me that this has happened so I can go clean out the database @@ -370,7 +371,7 @@ def register_user(data, user): username=data['username'], password=data['password1'], email=data['email'], - first_name=data['first_name'], + first_name=data['preferred_name'], ) if utils_auth.wiki_is_configured(): @@ -410,7 +411,7 @@ def register_user(data, user): gen_search_strings() - cache.set('sign', 'Welcome to Protospace, {}!'.format(data['first_name'])) + cache.set('sign', 'Welcome to Protospace, {}!'.format(data['preferred_name'])) BLANK_FORM = 'misc/blank_member_form.pdf' @@ -434,7 +435,7 @@ def gen_member_forms(member): packet = io.BytesIO() can = canvas.Canvas(packet, pagesize=letter) can.drawRightString(600, 770, '{} {} ({})'.format( - data['first_name'], + data['preferred_name'], data['last_name'], data['id'], )) diff --git a/apiserver/apiserver/api/utils_email.py b/apiserver/apiserver/api/utils_email.py index 549b85d..c9faf99 100644 --- a/apiserver/apiserver/api/utils_email.py +++ b/apiserver/apiserver/api/utils_email.py @@ -17,7 +17,7 @@ def send_welcome_email(member): def replace_fields(text): return text.replace( - '[name]', member.first_name, + '[name]', member.preferred_name, ).replace( '[username]', member.user.username, ).replace( @@ -45,7 +45,7 @@ def send_ical_email(member, session, ical_file): date = session.datetime.astimezone(utils.TIMEZONE_CALGARY).strftime('%A, %B %d') return text.replace( - '[name]', member.first_name, + '[name]', member.preferred_name, ).replace( '[class]', session.course.name, ).replace( @@ -71,7 +71,7 @@ def send_ical_email(member, session, ical_file): def send_interest_email(interest): def replace_fields(text): return text.replace( - '[name]', interest.user.member.first_name, + '[name]', interest.user.member.preferred_name, ).replace( '[course]', interest.course.name, ).replace( diff --git a/apiserver/apiserver/api/utils_ldap.py b/apiserver/apiserver/api/utils_ldap.py index f3ceadc..1364586 100644 --- a/apiserver/apiserver/api/utils_ldap.py +++ b/apiserver/apiserver/api/utils_ldap.py @@ -26,7 +26,7 @@ def find_user(username): def create_user(data): ldap_data = dict( - first=data['first_name'], + first=data['preferred_name'], last=data['last_name'], username=data['username'], email=data['email'], @@ -50,7 +50,7 @@ def add_to_group(member, group): if ldap_api('add-to-group', ldap_data) != 200: raise except BaseException as e: logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e))) - m = '{} {} ({})'.format(member.first_name, member.last_name, member.id) + m = '{} {} ({})'.format(member.preferred_name, member.last_name, member.id) msg = 'Problem adding {} to group {}!'.format(m, group) utils.alert_tanner(msg) logger.info(msg) @@ -64,7 +64,7 @@ def remove_from_group(member, group): if ldap_api('remove-from-group', ldap_data) != 200: raise except BaseException as e: logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e))) - m = '{} {} ({})'.format(member.first_name, member.last_name, member.id) + m = '{} {} ({})'.format(member.preferred_name, member.last_name, member.id) msg = 'Problem removing {} from group {}!'.format(m, group) utils.alert_tanner(msg) logger.info(msg) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index b73dd34..f245073 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -533,7 +533,7 @@ class DoorViewSet(viewsets.ViewSet, List): if not member.is_allowed_entry: continue active_member_cards[card.card_number] = '{} ({})'.format( - member.first_name + ' ' + member.last_name[0], + member.preferred_name + ' ' + member.last_name[0], member.id, ) @@ -547,7 +547,7 @@ class DoorViewSet(viewsets.ViewSet, List): member = card.user.member t = utils.now_alberta_tz().strftime('%Y-%m-%d %H:%M:%S, %a %I:%M %p') - logger.info('Scan - Time: {} | Name: {} {} ({})'.format(t, member.first_name, member.last_name, member.id)) + logger.info('Scan - Time: {} | Name: {} {} ({})'.format(t, member.preferred_name, member.last_name, member.id)) utils_stats.calc_card_scans() @@ -1096,10 +1096,10 @@ class ProtocoinViewSet(Base): destination_delta = amount memo = 'Protocoin - Transaction {} ({}) sent ₱ {} to {} ({})'.format( - source_member.first_name + ' ' + source_member.last_name, + source_member.preferred_name + ' ' + source_member.last_name, source_member.id, amount, - destination_member.first_name + ' ' + destination_member.last_name, + destination_member.preferred_name + ' ' + destination_member.last_name, destination_member.id, ) @@ -1145,7 +1145,7 @@ class ProtocoinViewSet(Base): res = dict( balance=user_balance, - first_name=source_user.member.first_name, + first_name=source_user.member.preferred_name, ) return Response(res) diff --git a/webclient/src/Account.js b/webclient/src/Account.js index 78b07b3..f807072 100644 --- a/webclient/src/Account.js +++ b/webclient/src/Account.js @@ -212,7 +212,7 @@ export function AccountForm(props) {
Member Details
- +

{user.username}

diff --git a/webclient/src/Admin.js b/webclient/src/Admin.js index 1509127..efe5750 100644 --- a/webclient/src/Admin.js +++ b/webclient/src/Admin.js @@ -42,7 +42,7 @@ export function AdminVet(props) { onClick={handleVet} loading={loading} > - {yousure ? 'You Sure?' : 'Vet ' + member.first_name} + {yousure ? 'You Sure?' : 'Vet ' + member.preferred_name} ); } @@ -87,7 +87,7 @@ export function AdminVetting(props) { {(displayAll ? vetting : vetting.slice(0,5)).map(x => - {x.first_name} {x.last_name} + {x.preferred_name} {x.last_name} Email diff --git a/webclient/src/AdminMembers.js b/webclient/src/AdminMembers.js index 4c4c4cb..45a8b69 100644 --- a/webclient/src/AdminMembers.js +++ b/webclient/src/AdminMembers.js @@ -527,10 +527,6 @@ export function AdminMemberInfo(props) { - - Name: - {member.first_name} {member.last_name} - Status: diff --git a/webclient/src/LoginSignup.js b/webclient/src/LoginSignup.js index 42e5cc3..6607672 100644 --- a/webclient/src/LoginSignup.js +++ b/webclient/src/LoginSignup.js @@ -75,14 +75,14 @@ export function SignupForm(props) { const handleCheck = (e, v) => setInput({ ...input, [v.name]: v.checked }); const genUsername = () => { - if (input.first_name && input.last_name) { - let first_name = input.first_name.trim().toLowerCase(); + if (input.preferred_name && input.last_name) { + let preferred_name = input.preferred_name.trim().toLowerCase(); let last_name = input.last_name.trim().toLowerCase(); - first_name = first_name.replace(/[^a-z- ]+/g, ''); + preferred_name = preferred_name.replace(/[^a-z- ]+/g, ''); last_name = last_name.replace(/[^a-z- ]+/g, ''); - first_name = first_name.replace(/[ -]/g, '.'); + preferred_name = preferred_name.replace(/[ -]/g, '.'); last_name = last_name.replace(/[ -]/g, '.'); - return first_name + '.' + last_name; + return preferred_name + '.' + last_name; } else { return ''; } @@ -147,6 +147,18 @@ export function SignupForm(props) { /> + + + + + Date: Sat, 27 Aug 2022 06:55:55 +0000 Subject: [PATCH 2/2] More preferred name --- apiserver/apiserver/api/signals.py | 2 +- apiserver/apiserver/api/views.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apiserver/apiserver/api/signals.py b/apiserver/apiserver/api/signals.py index 815d5b8..6ee2163 100644 --- a/apiserver/apiserver/api/signals.py +++ b/apiserver/apiserver/api/signals.py @@ -11,7 +11,7 @@ from . import models from .permissions import is_admin_director def get_object_owner(obj): - full_name = lambda member: member.first_name + ' ' + member.last_name + full_name = lambda member: member.preferred_name + ' ' + member.last_name if obj.__class__.__name__ == 'Member': return full_name(obj), obj.id diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index f245073..04aa1b4 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -570,7 +570,7 @@ class LockoutViewSet(viewsets.ViewSet, List): authorization = {} authorization['id'] = member.id - authorization['name'] = member.first_name + ' ' + member.last_name + authorization['name'] = member.preferred_name + ' ' + member.last_name authorization['common'] = bool(member.orientation_date or member.vetted_date) authorization['lathe'] = bool(member.lathe_cert_date) and authorization['common'] authorization['mill'] = bool(member.mill_cert_date) and authorization['common']