def update(self, organisation_dto: OrganisationDTO): """ Updates Organisation from DTO """ for attr, value in organisation_dto.items(): if attr == "type" and value is not None: value = OrganisationType[organisation_dto.type].value if attr == "managers": continue try: is_field_nullable = self.__table__.columns[attr].nullable if is_field_nullable and value is not None: setattr(self, attr, value) elif value is not None: setattr(self, attr, value) except KeyError: continue if organisation_dto.managers: self.managers = [] # Need to handle this in the loop so we can take care of NotFound users for manager in organisation_dto.managers: new_manager = User.get_by_username(manager) if new_manager is None: raise NotFound(f"User {manager} Not Found") self.managers.append(new_manager) db.session.commit()
def get_user_by_username(username: str) -> User: user = User.get_by_username(username) if user is None: raise NotFound() return user
def create_from_dto(cls, new_organisation_dto: NewOrganisationDTO): """ Creates a new organisation from a DTO """ new_org = cls() new_org.name = new_organisation_dto.name new_org.logo = new_organisation_dto.logo new_org.url = new_organisation_dto.url for manager in new_organisation_dto.managers: user = User.get_by_username(manager) if user is None: raise NotFound(f"User {manager} Not Found") new_org.managers.append(user) new_org.create() return new_org
def create_from_dto(cls, new_organisation_dto: NewOrganisationDTO): """ Creates a new organisation from a DTO """ new_org = cls() new_org.name = new_organisation_dto.name new_org.slug = new_organisation_dto.slug or slugify(new_organisation_dto.name) new_org.logo = new_organisation_dto.logo new_org.description = new_organisation_dto.description new_org.url = new_organisation_dto.url new_org.type = OrganisationType[new_organisation_dto.type].value new_org.subscription_tier = new_organisation_dto.subscription_tier for manager in new_organisation_dto.managers: user = User.get_by_username(manager) if user is None: raise NotFound(f"User {manager} Not Found") new_org.managers.append(user) new_org.create() return new_org
def update(self, team_dto: TeamDTO): """ Updates Team from DTO """ if team_dto.organisation: self.organisation = Organisation().get_organisation_by_name( team_dto.organisation) for attr, value in team_dto.items(): if attr == "visibility" and value is not None: value = TeamVisibility[team_dto.visibility].value if attr in ("members", "organisation"): continue try: is_field_nullable = self.__table__.columns[attr].nullable if is_field_nullable and value is not None: setattr(self, attr, value) elif value is not None: setattr(self, attr, value) except KeyError: continue if team_dto.members != self._get_team_members() and team_dto.members: for member in self.members: db.session.delete(member) for member in team_dto.members: user = User.get_by_username(member["userName"]) if user is None: raise NotFound("User not found") new_team_member = TeamMembers() new_team_member.team = self new_team_member.member = user new_team_member.function = TeamMemberFunctions[ member["function"]].value db.session.commit()