Skip to content

Commit

Permalink
Merge pull request #20 from uw-it-aca/develop
Browse files Browse the repository at this point in the history
Enable update of user roles
  • Loading branch information
fanglinfang committed Jul 30, 2019
2 parents bc88187 + c72356a commit f3919f1
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 483 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
author="UW-IT AXDD",
author_email="aca-it@uw.edu",
include_package_data=True,
install_requires=['UW-RestClients-Core>=1.1,<2.0',
install_requires=['UW-RestClients-Core<2.0',
'python-dateutil',
],
license='Apache License, Version 2.0',
Expand Down
9 changes: 8 additions & 1 deletion uw_bridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ def put_resource(url, body):
Update the entire resource
Bridge PUT seems to have the same effect as PATCH currently.
"""
pass
response = Bridge_DAO().putURL(url, PHEADER, body)
req_data = "PUT {0}: {1}".format(url, body)
_log_resp(req_data, response)

if response.status != 200:
_raise_exception(req_data, url, response)

return response.data


def _log_resp(req_data, response):
Expand Down
31 changes: 23 additions & 8 deletions uw_bridge/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ class CustomFields:
def __init__(self):
self.fields = []
self.name_id_map = {}
self.id_name_map = {}
self.get_custom_fields()

def get_custom_fields(self):
resp = get_resource(URL)
resp_data = json.loads(resp)
if resp_data.get("custom_fields") is not None:
for field in resp_data["custom_fields"]:
if (field.get("id") is not None and
field.get("name") is not None):
cf = BridgeCustomField(field_id=field["id"],
name=field["name"].lower())
self.fields.append(cf)
self.name_id_map[cf.name] = cf.field_id
for field in resp_data["custom_fields"]:
if field.get("id") is not None and field.get("name") is not None:
cf = BridgeCustomField(field_id=field["id"],
name=field["name"].lower())
self.fields.append(cf)
self.name_id_map[cf.name] = cf.field_id
self.id_name_map[cf.field_id] = cf.name

def get_fields(self):
"""
Expand All @@ -41,6 +41,9 @@ def get_fields(self):
def get_field_id(self, field_name):
return self.name_id_map.get(field_name)

def get_field_name(self, field_id):
return self.id_name_map.get(field_id)

def new_custom_field(self,
field_name,
field_value):
Expand All @@ -52,3 +55,15 @@ def new_custom_field(self,
return BridgeCustomField(field_id=self.get_field_id(field_name),
name=field_name,
value=field_value)

def get_custom_field(self,
field_id,
value_id,
value):
"""
Return a new BridgeCustomField object
"""
return BridgeCustomField(field_id=field_id,
name=self.get_field_name(field_id),
value_id=value_id,
value=value)
31 changes: 18 additions & 13 deletions uw_bridge/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class BridgeUser(models.Model):
next_due_date = models.DateTimeField(null=True, default=None)
completed_courses_count = models.IntegerField(default=-1)

def add_role(self, user_role):
if user_role not in self.roles:
self.roles.append(user_role)

def delete_role(self, user_role):
if user_role in self.roles:
self.roles.remove(user_role)

def is_deleted(self):
return self.deleted_at is not None

Expand Down Expand Up @@ -143,11 +151,7 @@ def to_json(self, omit_custom_fields=False):
return ret_user

def custom_fields_json(self):
custom_fields_json = []
if len(self.custom_fields) > 0:
for field in self.custom_fields.values():
custom_fields_json.append(field.to_json())
return custom_fields_json
return [field.to_json() for field in self.custom_fields.values()]

def to_json_post(self):
# for POST (add new or restore a user)
Expand All @@ -161,6 +165,9 @@ def to_json_patch(self):
user_data["custom_field_values"] = self.custom_fields_json()
return {"user": user_data}

def roles_to_json(self):
return [r.role_id for r in self.roles]

def __str__(self):
json_data = self.to_json()
json_data["deleted_at"] = self.deleted_at
Expand All @@ -169,16 +176,11 @@ def __str__(self):
json_data["completed_courses_count"] = self.completed_courses_count

if len(self.custom_fields):
custom_fields = []
for field in self.custom_fields.values():
custom_fields.append(field.to_json_short())
json_data["custom_fields"] = custom_fields
json_data["custom_fields"] = [f.to_json_short()
for f in self.custom_fields.values()]

if len(self.roles) > 0:
roles_json = []
for role in self.roles:
roles_json.append(role.to_json())
json_data["roles"] = roles_json
json_data["roles"] = self.roles_to_json()

return json.dumps(json_data, default=str)

Expand Down Expand Up @@ -216,6 +218,9 @@ def is_campus_admin(self):
def is_it_admin(self):
return self.name == BridgeUserRole.IT_ADMIN_NAME

def __eq__(self, role):
return self.role_id == role.role_id

def __init__(self, *args, **kwargs):
super(BridgeUserRole, self).__init__(*args, **kwargs)
# self.permissions = [] # unknown 2016/11
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"email":"bill@u.washington.edu",
"is_manager":true,
"locale":"en",
"roles":[],
"roles":["author", "fb412e52"],
"name":"Bill Average Teacher",
"avatar_url":null,
"updated_at":"2016-09-08T13:58:20.635-07:00",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"full_name":"Bill Average Teacher",
"sortable_name":"Teacher, Bill Average",
"email":"bill@u.washington.edu",
"locale":"en",
"is_manager":true,
"roles":[],
"locale":"en",
"roles":["author", "fb412e52"],
"name":"Bill Average Teacher",
"avatar_url":null,
"updated_at":"2016-09-08T13:58:20.635-07:00",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"meta":{},
"linked":{
"custom_fields": [
{
"id": "5",
"name": "REGID"
}
],
"custom_field_values": [
{
"id": "1",
"value": "9136CCB8F66711D5BE060004AC494FFE",
"links": {
"custom_field": {
"id": "5",
"type": "custom_fields"
}
}
}
]
},
"users":[
{"id":"195",
"uid":"javerage@uw.edu",
"hris_id":null,
"first_name":"James",
"last_name":"Student",
"full_name":"James Student",
"sortable_name":"Student, James",
"email":"javerage@uw.edu",
"locale":"en",
"roles":["account_admin", "author", "fb412e52"],
"name":"James Student",
"avatar_url":null,
"updated_at":"2016-07-25T16:24:42.131-07:00",
"deleted_at":null,
"unsubscribed":null,
"welcomedAt":null,
"loggedInAt":null,
"links":{
"custom_field_values":["1"]
},
"meta":{"can_masquerade":true}
}
]}

This file was deleted.

0 comments on commit f3919f1

Please sign in to comment.