def add_member(self, member, roles=None, expiration_date=None): """Add a member to the group. :Parameters: - `member`: member (required) - `roles`: list of roles. Each role could be a role id, a role label or codename, a role instance (optional, default: ``[]``) - `expiration_date`: A timestamp specifying when the membership expires. Note that this doesn't automatically remove the member from the group but is only an indicator to an external application to check if the membership still is valid (optional, default: ``None``) """ if roles is None: roles = [] if not self.id: raise exceptions_gm.GroupNotSavedError( "You must save the group before to create a relation with members" ) if not member.id: raise exceptions_gm.MemberNotSavedError( "You must save the member before to create a relation with groups" ) group_member_model = self.group_member_model group_member, _ = group_member_model.objects.get_or_create( member=member, group=self, expiration_date=expiration_date, ) if roles: for role in roles: if isinstance(role, GroupMemberRole): group_member.roles.add(role) elif isinstance(role, int): role_obj = GroupMemberRole.objects.get(id=role) group_member.roles.add(role_obj) else: try: role_obj = GroupMemberRole.objects.get( models.Q(label=role) | models.Q(codename=role)) group_member.roles.add(role_obj) except Exception as e: raise exceptions_gm.GetRoleError(e) return group_member
def add_member(self, member, roles=None): """Add a member to the group. :Parameters: - `member`: member (required) - `roles`: list of roles. Each role could be a role id, a role label or codename, a role instance (optional, default: ``[]``) """ if roles is None: roles = [] if not self.id: raise exceptions_gm.GroupNotSavedError( "You must save the group before to create a relation with members" ) if not member.id: raise exceptions_gm.MemberNotSavedError( "You must save the member before to create a relation with groups" ) group_member_model = self.group_member_model group_member = group_member_model(member=member, group=self) group_member.save() if roles: for role in roles: if isinstance(role, GroupMemberRole): group_member.roles.add(role) elif isinstance(role, int): role_obj = GroupMemberRole.objects.get(id=role) group_member.roles.add(role_obj) else: try: role_obj = GroupMemberRole.objects.get( models.Q(label=role) | models.Q(codename=role)) group_member.roles.add(role_obj) except Exception as e: raise exceptions_gm.GetRoleError(e) return group_member