Unverified Commit 0368e371 authored by Verdan Mahmood's avatar Verdan Mahmood Committed by GitHub

Make User ID the CTA for user operations (#440)

* Initial commit to make user id label customizable

* Make use of amundsen common models

* Fixes the missing file

* Makes email the default label for user id

* Removes the code used for debugging

* Updates based on Tamika's review
parent e7ae9cd4
from typing import Dict, Optional
from marshmallow import Schema, fields, pre_load, post_load, validates_schema, ValidationError
from typing import Dict
from amundsen_common.models.user import UserSchema, User
from flask import current_app as app
"""
TODO: Explore all internationalization use cases and
redesign how User handles names
TODO - Delete this file
Once all of the upstream services provide a complete User object we will no
longer need to supplement the User objects as done in `preprocess_data`
"""
class User:
def __init__(self,
display_name: str = None,
email: str = None,
employee_type: str = None,
first_name: str = None,
full_name: str = None,
github_username: str = None,
is_active: bool = True,
last_name: str = None,
manager_email: str = None,
manager_fullname: str = None,
profile_url: str = None,
role_name: str = None,
slack_id: str = None,
team_name: str = None,
user_id: str = None) -> None:
self.display_name = display_name
self.email = email
self.employee_type = employee_type
self.first_name = first_name
self.full_name = full_name
self.github_username = github_username
self.is_active = is_active
self.last_name = last_name
self.manager_email = manager_email
self.manager_fullname = manager_fullname
self.profile_url = profile_url
self.role_name = role_name
self.slack_id = slack_id
self.team_name = team_name
self.user_id = user_id
# TODO: Add frequent_used, bookmarked, & owned resources
class UserSchema(Schema):
display_name = fields.Str(allow_none=False)
email = fields.Str(required=True)
employee_type = fields.Str(allow_none=True)
first_name = fields.Str(allow_none=True)
full_name = fields.Str(allow_none=True)
github_username = fields.Str(allow_none=True)
is_active = fields.Bool(allow_none=True)
last_name = fields.Str(allow_none=True)
manager_email = fields.Str(allow_none=True)
manager_fullname = fields.Str(allow_none=True)
profile_url = fields.Str(allow_none=True)
role_name = fields.Str(allow_none=True)
slack_id = fields.Str(allow_none=True)
team_name = fields.Str(allow_none=True)
user_id = fields.Str(allow_none=False)
def _str_no_value(self, s: Optional[str]) -> bool:
# Returns True if the given string is None or empty
if not s:
return True
if len(s.strip()) == 0:
return True
return False
@pre_load
def preprocess_data(self, data: Dict) -> Dict:
if self._str_no_value(data.get('user_id')):
data['user_id'] = data.get('email')
if self._str_no_value(data.get('profile_url')):
data['profile_url'] = ''
if app.config['GET_PROFILE_URL']:
data['profile_url'] = app.config['GET_PROFILE_URL'](data['user_id'])
first_name = data.get('first_name')
last_name = data.get('last_name')
if self._str_no_value(data.get('full_name')) and first_name and last_name:
data['full_name'] = f"{first_name} {last_name}"
if self. _str_no_value(data.get('display_name')):
if self._str_no_value(data.get('full_name')):
data['display_name'] = data.get('email')
else:
data['display_name'] = data.get('full_name')
return data
@post_load
def make_user(self, data: Dict) -> User:
return User(**data)
@validates_schema
def validate_user(self, data: Dict) -> None:
if self._str_no_value(data.get('display_name')):
raise ValidationError('"display_name", "full_name", or "email" must be provided')
if self._str_no_value(data.get('user_id')):
raise ValidationError('"user_id" or "email" must be provided')
from marshmallow import ValidationError
def load_user(user_data: Dict) -> User:
try:
schema = UserSchema()
# To make sure we pass frontend configuration to amundsen common models
user_data['GET_PROFILE_URL'] = app.config['GET_PROFILE_URL']
data, errors = schema.load(user_data)
return data
except ValidationError as err:
......
......@@ -152,7 +152,7 @@ export class OwnerEditor extends React.Component<OwnerEditorProps, OwnerEditorSt
<input
id='add-item-input'
autoFocus={true}
placeholder='Enter an email address'
placeholder={`Please enter ${AppConfig.userIdLabel}`}
ref={ this.inputRef }
/>
<button className="btn btn-default add-button" type="submit" aria-label="Add Item">
......
......@@ -22,6 +22,7 @@ const configCustom: AppConfigCustom = {
indexUsers: {
enabled: false,
},
userIdLabel: "email address",
issueTracking: {
enabled: false
}
......
......@@ -28,6 +28,7 @@ const configDefault: AppConfig = {
indexUsers: {
enabled: false,
},
userIdLabel: "email address",
issueTracking: {
enabled: false
},
......
......@@ -14,6 +14,7 @@ export interface AppConfig {
google: GoogleAnalyticsConfig;
indexDashboards: IndexDashboardsConfig;
indexUsers: IndexUsersConfig;
userIdLabel: string;
issueTracking: IssueTrackingConfig;
logoPath: string | null;
mailClientFeatures: MailClientFeaturesConfig;
......@@ -31,6 +32,7 @@ export interface AppConfigCustom {
google?: GoogleAnalyticsConfig;
indexDashboards?: IndexDashboardsConfig;
indexUsers?: IndexUsersConfig;
userIdLabel: string;
issueTracking?: IssueTrackingConfig;
logoPath?: string;
mailClientFeatures?: MailClientFeaturesConfig;
......
......@@ -217,6 +217,7 @@ class MetadataTest(unittest.TestCase):
'github_username': 'githubusername',
'is_active': True,
'last_name': 'Lastname',
'manager_id': 'managerid',
'manager_fullname': 'Manager Fullname',
'role_name': 'SWE',
'slack_id': 'slackuserid',
......@@ -234,6 +235,7 @@ class MetadataTest(unittest.TestCase):
'last_name': 'Lastname',
'manager_email': 'manager@email.com',
'manager_fullname': 'Manager Fullname',
'manager_id': 'managerid',
'profile_url': 'https://test-profile-url.com',
'role_name': 'SWE',
'slack_id': 'slackuserid',
......
......@@ -245,6 +245,7 @@ class SearchUser(unittest.TestCase):
'is_active': True,
'last_name': 'Last',
'manager_email': 'manager@email.com',
'manager_id': None,
'manager_fullname': None,
'profile_url': '',
'role_name': 'SWE',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment