Skip to content

Commit

Permalink
Add membership array to Org post/get endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
bsatoriu committed Aug 28, 2024
1 parent 389b737 commit 5726d26
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions api/endpoints/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def get(self):
JobQueue.id == OrganizationJobQueue.job_queue_id
).order_by(JobQueue.queue_name).all()

membership_query = db.session.query(
Member, OrganizationMembership_db,
).filter(
Member.id == OrganizationMembership_db.member_id
).order_by(Member.first_name).all()

Record = namedtuple('Record', otree.keys())
org_tree_records = [Record(*r) for r in otree.fetchall()]
for r in org_tree_records:
Expand All @@ -56,6 +62,7 @@ def get(self):
'default_job_limit_count': r.default_job_limit_count,
'default_job_limit_hours': r.default_job_limit_hours,
'job_queues': [],
'members': [],
'creation_date': r.creation_date.strftime('%m/%d/%Y'),
}

Expand All @@ -67,6 +74,16 @@ def get(self):
'queue_description': q.JobQueue.queue_description
})

for m in membership_query:
if m.OrganizationMembership.org_id == r.id:
org['members'].append({
'id': m.Member.id,
'first_name': m.Member.first_name,
'last_name': m.Member.last_name,
'username': m.Member.username,
'email': m.Member.email
})

result.append(org)

return result
Expand Down Expand Up @@ -104,6 +121,21 @@ def post(self):
db.session.add(new_org)
db.session.commit()

# Update membership
db.session.execute(
db.delete(OrganizationMembership_db).filter_by(org_id=new_org.id)
)
db.session.commit()

org_members = []
members = req_data.get("members", [])
for org_member in members:
org_members.append(OrganizationMembership_db(member_id=org_member['member_id'], org_id=new_org.id, org_maintainer=org_member['maintainer'], creation_date=datetime.utcnow()))

if len(org_members) > 0:
db.session.add_all(org_members)
db.session.commit()

org_schema = OrganizationSchema()
return json.loads(org_schema.dumps(new_org))

Expand Down

0 comments on commit 5726d26

Please sign in to comment.