-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
582 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from dataclasses import dataclass | ||
|
||
from pyramid.httpexceptions import HTTPNotFound | ||
|
||
from h.exceptions import InvalidUserId | ||
from h.models import Group, GroupMembership, User | ||
|
||
|
||
@dataclass | ||
class GroupMembershipContext: | ||
group: Group | ||
user: User | ||
membership: GroupMembership | None | ||
|
||
|
||
def group_membership_api_factory(request) -> GroupMembershipContext: | ||
user_service = request.find_service(name="user") | ||
group_service = request.find_service(name="group") | ||
group_members_service = request.find_service(name="group_members") | ||
|
||
userid = request.matchdict["userid"] | ||
pubid = request.matchdict["pubid"] | ||
|
||
def get_user() -> User | None: | ||
if userid == "me": | ||
if request.authenticated_userid: | ||
return user_service.fetch(request.authenticated_userid) | ||
|
||
return None | ||
|
||
try: | ||
return user_service.fetch(userid) | ||
except InvalidUserId: | ||
return None | ||
|
||
user = get_user() | ||
|
||
if not user: | ||
raise HTTPNotFound(f"User not found: {userid}") | ||
|
||
group = group_service.fetch(pubid) | ||
|
||
if not group: | ||
raise HTTPNotFound(f"Group not found: {pubid}") | ||
|
||
membership = group_members_service.get(group, user) | ||
|
||
if not membership and request.method != "POST": | ||
raise HTTPNotFound(f"Membership not found: ({pubid}, {userid})") | ||
|
||
return GroupMembershipContext(group=group, user=user, membership=membership) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.