Factor out first_or_none

This commit is contained in:
richard-dds 2018-09-04 12:50:44 -04:00
parent 6af50b78b0
commit 6bfbaea867
3 changed files with 8 additions and 11 deletions

View File

@ -6,9 +6,7 @@ from sqlalchemy.orm import relationship
from atst.models import Base from atst.models import Base
from atst.models.types import Id from atst.models.types import Id
from atst.models.request_status_event import RequestStatus from atst.models.request_status_event import RequestStatus
from atst.utils import first_or_none
def first_or_none(predicate, lst):
return next((x for x in lst if predicate(x)), None,)
class Request(Base): class Request(Base):

View File

@ -4,6 +4,7 @@ from sqlalchemy.orm import relationship
from atst.models import Base from atst.models import Base
from atst.models.types import Id from atst.models.types import Id
from atst.models.mixins import TimestampsMixin from atst.models.mixins import TimestampsMixin
from atst.utils import first_or_none
MOCK_MEMBERS = [ MOCK_MEMBERS = [
@ -48,14 +49,10 @@ class Workspace(Base, TimestampsMixin):
@property @property
def owner(self): def owner(self):
return next( def _is_workspace_owner(workspace_role):
( return workspace_role.role.name == "owner"
workspace_role.user
for workspace_role in self.roles return first_or_none(_is_workspace_owner, self.roles)
if workspace_role.role.name == "owner"
),
None,
)
@property @property
def users(self): def users(self):

2
atst/utils.py Normal file
View File

@ -0,0 +1,2 @@
def first_or_none(predicate, lst):
return next((x for x in lst if predicate(x)), None,)