Skip to content

Commit

Permalink
Merge pull request #307 from okta/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bretterer authored Jul 21, 2022
2 parents 0ae9eaa + bb76710 commit a10c1ea
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ python:
- "3.10-dev"

install:
- pip install -U importlib_metadata
- pip install -U -r requirements.txt

script:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Okta Python SDK Changelog

## v2.6.0
- Manage custom group profile attributes (Fixes #279)
## v2.5.0
- Regenerate code using the [open API spec v2.11.1](https://github.com/okta/okta-management-openapi-spec/releases/tag/openapi-2.11.1)
- Updates client template to persist aiohttp and related logic
Expand Down
2 changes: 1 addition & 1 deletion okta/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.5.0'
__version__ = '2.6.0'
40 changes: 40 additions & 0 deletions okta/models/group_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from okta.okta_object import OktaObject

from okta.helpers import to_snake_case, to_lower_camel_case


class GroupProfile(
OktaObject
Expand All @@ -28,13 +30,23 @@ class GroupProfile(
A class for GroupProfile objects.
"""

BASIC_ATTRIBUTES = (
"description",
"name",
)

def __init__(self, config=None):
super().__init__(config)
if config:
self.description = config["description"]\
if "description" in config else None
self.name = config["name"]\
if "name" in config else None
# set custom attributes not defined in model, do not change string case
for attr_name in config:
lower_camel_case = to_lower_camel_case(attr_name)
if lower_camel_case not in GroupProfile.BASIC_ATTRIBUTES:
setattr(self, attr_name, config[attr_name])
else:
self.description = None
self.name = None
Expand All @@ -45,5 +57,33 @@ def request_format(self):
"description": self.description,
"name": self.name
}
available_attrs = vars(self)
for attr in available_attrs:
# do not allow overriding of base attributes
if not attr in current_obj_format:
current_obj_format[attr] = getattr(self, attr)
parent_req_format.update(current_obj_format)
return parent_req_format

def __getattr__(self, attr_name):
"""
Try different cases for backward compatibility.
"""
available_attrs = vars(self)
snake_case = to_snake_case(attr_name)
if snake_case in available_attrs:
return available_attrs[snake_case]
lower_camel_case = to_lower_camel_case(attr_name)
if lower_camel_case in available_attrs:
return available_attrs[lower_camel_case]
raise AttributeError(f"'GroupProfile' object has no attribute '{attr_name}'")

def __setattr__(self, attr_name, attr_value):
"""
Keep custom attributes unchanged and keep backward compatibility for basic attributes.
"""
lower_camel_case = to_lower_camel_case(attr_name)
if lower_camel_case in GroupProfile.BASIC_ATTRIBUTES:
super(GroupProfile, self).__setattr__(lower_camel_case, attr_value)
else:
super(GroupProfile, self).__setattr__(attr_name, attr_value)
20 changes: 10 additions & 10 deletions openapi/templates/model.py.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ from okta.models.{{snakeCase model.extends}}\
{{else}}
from okta.okta_object import OktaObject
{{/if}}
{{#if (eq model.modelName "UserProfile")}}
{{#if (or (eq model.modelName "UserProfile") (eq model.modelName "GroupProfile"))}}

from okta.helpers import to_snake_case, to_lower_camel_case
{{/if}}
Expand Down Expand Up @@ -79,7 +79,7 @@ class {{pascalCase model.modelName}}(
"""
A class for {{pascalCase model.modelName}} objects.
"""
{{#if (eq model.modelName "UserProfile")}}
{{#if (or (eq model.modelName "UserProfile") (eq model.modelName "GroupProfile"))}}

BASIC_ATTRIBUTES = (
{{#each model.properties as |prop|}}
Expand Down Expand Up @@ -151,11 +151,11 @@ class {{pascalCase model.modelName}}(
{{/if}}
{{/if}}
{{/each}}
{{#if (eq model.modelName "UserProfile")}}
{{#if (or (eq model.modelName "UserProfile") (eq model.modelName "GroupProfile"))}}
# set custom attributes not defined in model, do not change string case
for attr_name in config:
lower_camel_case = to_lower_camel_case(attr_name)
if lower_camel_case not in UserProfile.BASIC_ATTRIBUTES:
if lower_camel_case not in {{model.modelName}}.BASIC_ATTRIBUTES:
setattr(self, attr_name, config[attr_name])
{{/if}}
else:
Expand Down Expand Up @@ -186,7 +186,7 @@ class {{pascalCase model.modelName}}(
{{/each}}
}
{{/if}}
{{#if (eq model.modelName "UserProfile")}}
{{#if (or (eq model.modelName "UserProfile") (eq model.modelName "GroupProfile"))}}
available_attrs = vars(self)
for attr in available_attrs:
# do not allow overriding of base attributes
Expand All @@ -195,7 +195,7 @@ class {{pascalCase model.modelName}}(
{{/if}}
parent_req_format.update(current_obj_format)
return parent_req_format
{{#if (eq model.modelName "UserProfile")}}
{{#if (or (eq model.modelName "UserProfile") (eq model.modelName "GroupProfile"))}}

def __getattr__(self, attr_name):
"""
Expand All @@ -208,16 +208,16 @@ class {{pascalCase model.modelName}}(
lower_camel_case = to_lower_camel_case(attr_name)
if lower_camel_case in available_attrs:
return available_attrs[lower_camel_case]
raise AttributeError(f"'UserProfile' object has no attribute '{attr_name}'")
raise AttributeError(f"'{{model.modelName}}' object has no attribute '{attr_name}'")

def __setattr__(self, attr_name, attr_value):
"""
Keep custom attributes unchanged and keep backward compatibility for basic attributes.
"""
lower_camel_case = to_lower_camel_case(attr_name)
if lower_camel_case in UserProfile.BASIC_ATTRIBUTES:
super(UserProfile, self).__setattr__(lower_camel_case, attr_value)
if lower_camel_case in {{model.modelName}}.BASIC_ATTRIBUTES:
super({{model.modelName}}, self).__setattr__(lower_camel_case, attr_value)
else:
super(UserProfile, self).__setattr__(attr_name, attr_value)
super({{model.modelName}}, self).__setattr__(attr_name, attr_value)
{{/if}}
{{/if}}
Loading

0 comments on commit a10c1ea

Please sign in to comment.