Org Web Adapter

pages/eng_8351.org

ID
3cddc1a3-0122-40b8-9e82-719cbb249e87
ROAM_ALIASES
ENG-8351

ENG-8351 - List people attribute values by groups via API

- tags :: People Attributes Admin Squad ENG-8320 Jira 15Five

- source :: https://15five-dev.atlassian.net/browse/ENG-8351

Description

User Story

As a user, I want to use the API to list all the data for any single people

attribute — default or custom — associated with users in a group, so that I can

easily see via my API interface group data for a specific people attribute.

Acceptance Criteria

An API user can request to see data for a single default or custom people

attribute for all users in a specific group.

Development notes

- Note taken on

While working on this I discovered the `company_struct` idea while

implementing this feature. What I needed was to get the membership of a given

group ID. Initially I did this in a literal way of looking up the CompanyGroup

instance, filtering by company and active state.

Andrii Pleskach noted that I could use CompanyStruct and CompanyGroupStruct to

get this value. I had shied away from using those two models in the past

because I didn’t totally understand them. What I started with was:

#+BEGIN_SRC python

def build_user_query_from_group_id(request, company):

"""Looks for a group ID in the given request GET parameters and, if found,

builds a custom QueryDict that replaces the group ID with a list of users

in the Group.

If the Group is not found in the given company, or an ID is not in the request,

we return the unmodified QueryDict from the request.

"""

group_id = request.GET.get('group', None)

if not group_id:

return request.GET

print(group_id)

group = company.companygroup_set.filter(delete_ts__isnull=True, id=group_id).first()

if not group:

return request.GET

query_dict = request.GET.copy()

if 'user' in query_dict:

query_dict.pop('user')

group_member_ids = [str(uid) for uid in group.members.values_list('member_id', flat=True)]

query_dict.setlist('user', group_member_ids)

print(query_dict)

return query_dict

#+END_SRC

The result was:

#+BEGIN_SRC python

def build_user_query_from_group_id(request, company):

"""Looks for a group ID in the given request GET parameters and, if found,

builds a custom QueryDict that replaces the group ID with a list of users

in the Group.

If the Group is not found in the given company, or an ID is not in the request,

we return the unmodified QueryDict from the request.

"""

group_id = request.GET.get('group', None)

if not group_id:

return request.GET

group = CompanyGroup.existing_objects.filter(company_id=company.id, id=group_id).first()

if not group:

return request.GET

query_dict = request.GET.copy()

if 'user' in query_dict:

query_dict.pop('user')

company_struct = CompanyStructure.get_for_company_id(company.id)

struct_group = CompanyStructureGroup(group, company_struct)

query_dict.setlist('user', struct_group.member_ids)

return query_dict

#+END_SRC

It looks a little more complicated. I haven’t made it tighter, but the

CompanyStruct object is cached pretty heavily, so hopefully the performance

improves. Also I don’t have to import the `CompanyGroup` model. All very nice.