Skip to content

Commit d12f2fd

Browse files
committed
Move generate_username to toplevel of security.services
1 parent 647b233 commit d12f2fd

1 file changed

Lines changed: 38 additions & 27 deletions

File tree

membersuite_api_client/security/models.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@
99
from ..utils import convert_ms_object
1010

1111

12+
def generate_username(membersuite_object):
13+
"""Return a username suitable for storing in auth.User.username.
14+
15+
Has to be <= 30 characters long. (Until we drop support for
16+
Django 1.4, after which we can define a custom User model with
17+
a larger username field.)
18+
19+
We want to incorporate the membersuite_id in the username.
20+
Those look like this:
21+
22+
6faf90e4-0032-c842-a28a-0b3c8b856f80
23+
24+
That's 36 characters, too long for username. Making the
25+
assumption that those leading digits will always be there in
26+
every ID. Since they're not needed to generate a unique
27+
value, they can go.
28+
29+
After chomping the intro, we're at 27 characters, so we
30+
insert "ms" in the front.
31+
32+
"""
33+
username = "ms" + membersuite_object.membersuite_id[len("6faf90e4"):]
34+
return username
35+
36+
1237
@python_2_unicode_compatible
1338
class PortalUser(MemberSuiteObject):
1439

@@ -38,30 +63,6 @@ def __str__(self):
3863
session_id=self.session_id))
3964

4065

41-
def generate_username(self):
42-
"""Return a username suitable for storing in auth.User.username.
43-
44-
Has to be <= 30 characters long. (Until we drop support for
45-
Django 1.4, after which we can define a custom User model with
46-
a larger username field.)
47-
48-
We want to incorporate the membersuite_id in the username.
49-
Those look like this:
50-
51-
6faf90e4-0032-c842-a28a-0b3c8b856f80
52-
53-
That's 36 characters, too long for username. Making the
54-
assumption that those leading digits will always be there in
55-
every ID. Since they're not needed to generate a unique
56-
value, they can go.
57-
58-
After chomping the intro, we're at 27 characters, so we
59-
insert "ms" in the front.
60-
61-
"""
62-
username = "ms" + self.membersuite_id[len("6faf90e4"):]
63-
return username
64-
6566
def get_individual(self, client):
6667
"""Return the Individual that owns this PortalUser.
6768
@@ -100,8 +101,9 @@ def __init__(self, membersuite_object_data, portal_user=None):
100101
self.email_address = self.fields["EmailAddress"]
101102
self.first_name = self.fields["FirstName"]
102103
self.last_name = self.fields["LastName"]
104+
self.title = self.fields["Title"]
103105

104-
self.primary_organization__rtg = (
106+
self.primary_organization_id = (
105107
self.fields["PrimaryOrganization__rtg"])
106108

107109
self.portal_user = portal_user
@@ -114,6 +116,15 @@ def __str__(self):
114116
first_name=self.first_name,
115117
last_name=self.last_name))
116118

119+
@property
120+
def phone_number(self):
121+
for key_value_pair in (
122+
self.fields["PhoneNumbers"]["MemberSuiteObject"][0]
123+
["Fields"]["KeyValueOfstringanyType"]):
124+
if key_value_pair["Key"] == "PhoneNumber":
125+
return key_value_pair["Value"]
126+
return None
127+
117128
def is_member(self, client):
118129
"""Is this Individual a member?
119130
@@ -148,14 +159,14 @@ def get_primary_organization(self, client):
148159
"""Return the primary Organization for this Individual.
149160
150161
"""
151-
if self.primary_organization__rtg is None:
162+
if self.primary_organization_id is None:
152163
return None
153164

154165
if not client.session_id:
155166
client.request_session()
156167

157168
query = "SELECT OBJECT() FROM ORGANIZATION WHERE ID = '{}'".format(
158-
self.primary_organization__rtg)
169+
self.primary_organization_id)
159170

160171
result = client.runSQL(query)
161172

0 commit comments

Comments
 (0)