FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 12ce65ff authored by Wajdi Hajji's avatar Wajdi Hajji
Browse files

Set User first_name and email attrs

parent 3ddb940a
Branches run-pipeline
No related tags found
1 merge request!19Update Django User attributes
Pipeline #95428 passed with warnings
django-ucamlookup changelog
=============================
3.0.4 - 21/06/2021
------------------
- Populate User object email with lookup email if accessible, or create it as crsid@cam.ac.uk,
otherwise. Also, set User first name to '' to not get updated by social auth pipelines.
3.0.3 - 09/05/2019
------------------
......
......@@ -70,8 +70,10 @@ password for the group and use the group short name as a username for authentica
## Lookup User
django-ucamlookup modifies a User object each time is going to be saved, either new or update, and assigns to its
*last_name* property the visible name from lookup for that user. The username is used to search for this user in lookup.
django-ucamlookup modifies a User object each time is going to be saved, either new or update. It assigns to its
*last_name* and *email* the visible name and email from lookup. If lookup's email is not accessible, create the
User email as crsid@cam.ac.uk. It also sets the User first name to '' to not get updated by social auth pipelines.
The username is used to search for this user in lookup.
## Lookup Group
......
......@@ -10,6 +10,7 @@ DIRNAME = os.path.dirname(__file__)
settings.configure(
DEBUG=False,
SECRET_KEY='placeholder',
DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'test.db', }},
TIME_ZONE='Europe/London',
USE_TZ=True,
......
......@@ -7,7 +7,7 @@ setup(
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='https://gitlab.developers.cam.ac.uk/uis/devops/django/ucamlookup',
version='3.0.3',
version='3.0.4',
license='MIT',
author='DevOps Division, University Information Services, University of Cambridge',
author_email='devops@uis.cam.ac.uk',
......
from django.contrib.auth.models import User
from django.db.models.signals import pre_save
from django.dispatch import receiver
from ucamlookup.models import LookupGroup
from ucamlookup.utils import return_visibleName_by_crsid, return_title_by_groupid
from ucamlookup.utils import (return_title_by_groupid,
return_visibleName_by_crsid)
@receiver(pre_save, sender=User)
def add_name_to_user(instance, **kwargs):
def add_user_attrs(instance, **kwargs):
user = instance
if user is not None:
# It is tempting to populate the Django User first name, last name,
# and email from lookup, but given their visibility setting this seems
# not to be always feasible. That's why we rely on the lookup visible name
# for comms with the users so that's retrieved and stored in User last name
# attribute.
user.last_name = return_visibleName_by_crsid(user.username)[:30]
# Email is required to 'social_core.pipeline.social_auth.associate_by_email'
# social auth pipeline to associate the social user to the existing Django User.
# So, it should be populated. We use the canonical email format: crsid@cam.ac.uk.
user.email = user.username + '@cam.ac.uk'
# If first_name is not set, social_core.pipeline.user.user_details
# social auth pipeline will update it and eventually we may end up with
# confusing information where there are redundant details in Django
# User first name and last name. Set it to '' to prevent this.
user.first_name = ''
@receiver(pre_save, sender=LookupGroup)
def add_title_to_group(instance, **kwargs):
......
import json
import sys
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from mock import patch, Mock
from mock import Mock, patch
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.test import TestCase, override_settings
from ucamlookup.models import LookupGroup
from ucamlookup.utils import user_in_groups, get_users_from_query, return_visibleName_by_crsid, get_groups_from_query, \
return_title_by_groupid, get_group_ids_of_a_user_in_lookup, get_institutions, get_institution_name_by_id, \
validate_crsid_list, validate_groupid_list, get_connection, get_user_lookupgroups, get_users_of_a_group
from ucamlookup.utils import (get_connection,
get_group_ids_of_a_user_in_lookup,
get_groups_from_query,
get_institution_name_by_id, get_institutions,
get_user_lookupgroups, get_users_from_query,
get_users_of_a_group, return_title_by_groupid,
return_visibleName_by_crsid, user_in_groups,
validate_crsid_list, validate_groupid_list)
class UcamLookupOptionsTests(TestCase):
......@@ -21,13 +29,13 @@ class UcamLookupOptionsTests(TestCase):
UCAMLOOKUP_CHECK_CERTS=False, UCAMLOOKUP_USERNAME="mock_username",
UCAMLOOKUP_PASSWORD="mock_password")
def test_optional_settings(self):
conn = get_connection()
self.assertEqual(conn.host, "mock_host")
self.assertEqual(conn.port, 80)
self.assertEqual(conn.url_base, "/mock/")
self.assertIsNone(conn.ca_certs)
self.assertEqual(conn.username, "mock_username")
self.assertEqual(conn.password, "mock_password")
conn = get_connection()
self.assertEqual(conn.host, "mock_host")
self.assertEqual(conn.port, 80)
self.assertEqual(conn.url_base, "/mock/")
self.assertIsNone(conn.ca_certs)
self.assertEqual(conn.username, "mock_username")
self.assertEqual(conn.password, "mock_password")
class UcamLookupTests(TestCase):
......@@ -61,10 +69,15 @@ class UcamLookupTests(TestCase):
if path == 'api/v1/person/crsid/amc203':
mock_result.person.visibleName = 'Dr Abraham Martin'
mock_result.email.value = 'amc203@cam.ac.uk'
mock_result.person.attributes = [mock_result.email]
elif path == 'api/v1/person/crsid/jw35':
mock_result.person.visibleName = 'John Warbrick'
mock_result.email.value = 'jw35@cam.ac.uk'
mock_result.person.attributes = [mock_result.email]
elif path == 'api/v1/person/crsid/test0001':
mock_result.person.visibleName = 'Test User 1'
mock_result.person.attributes = []
elif path == 'api/v1/person/crsid/amc20311':
mock_result.person = None
elif path == 'api/v1/group/101888':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment