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_campaign_organisations_as_dto(campaign_id: int, user_id: int): """ Returns organisations under a particular campaign """ organisation_list_dto = ListOrganisationsDTO() orgs = (Organisation.query.join(campaign_organisations).filter( campaign_organisations.c.campaign_id == campaign_id).all()) for org in orgs: if user_id != 0: logged_in = OrganisationService.can_user_manage_organisation( org.id, user_id) else: logged_in = False organisation_dto = OrganisationDTO() organisation_dto.organisation_id = org.id organisation_dto.name = org.name organisation_dto.logo = org.logo organisation_dto.url = org.url organisation_dto.is_manager = logged_in organisation_list_dto.organisations.append(organisation_dto) return organisation_list_dto
def get_campaign_as_dto(campaign_id: int, user_id: int): """Gets the specified campaign""" campaign = CampaignService.get_campaign(campaign_id) campaign_dto = CampaignDTO() campaign_dto.id = campaign.id campaign_dto.url = campaign.url campaign_dto.name = campaign.name campaign_dto.logo = campaign.logo campaign_dto.description = campaign.description campaign_dto.organisations = [] orgs = ( Organisation.query.join(campaign_organisations) .filter(campaign_organisations.c.campaign_id == campaign.id) .all() ) for org in orgs: if user_id != 0: logged_in = OrganisationService.can_user_manage_organisation( org.id, user_id ) else: logged_in = False organisation_dto = OrganisationDTO() organisation_dto.organisation_id = org.id organisation_dto.name = org.name organisation_dto.logo = org.logo organisation_dto.url = org.url organisation_dto.is_manager = logged_in return campaign_dto
def assert_validate_users(organisation_dto: OrganisationDTO): """ Validates that the users exist""" if organisation_dto.managers and len(organisation_dto.managers) == 0: raise OrganisationServiceError("Must have at least one admin") managers = [] for user in organisation_dto.managers: try: admin = UserService.get_user_by_username(user) except NotFound: raise NotFound(f"User {user} does not exist") managers.append(admin.username) organisation_dto.managers = managers
def as_dto(self, omit_managers=False): """ Returns a dto for an organisation """ organisation_dto = OrganisationDTO() organisation_dto.organisation_id = self.id organisation_dto.name = self.name organisation_dto.logo = self.logo organisation_dto.description = self.description organisation_dto.url = self.url organisation_dto.managers = [] organisation_dto.type = OrganisationType(self.type).name if omit_managers: return organisation_dto for manager in self.managers: org_manager_dto = OrganisationManagerDTO() org_manager_dto.username = manager.username org_manager_dto.picture_url = manager.picture_url organisation_dto.managers.append(org_manager_dto) return organisation_dto
def as_dto(self): """ Returns a dto for an organisation """ organisation_dto = OrganisationDTO() organisation_dto.organisation_id = self.id organisation_dto.name = self.name organisation_dto.logo = self.logo organisation_dto.url = self.url organisation_dto.managers = [] for manager in self.managers: org_manager_dto = OrganisationManagerDTO() org_manager_dto.username = manager.username org_manager_dto.picture_url = manager.picture_url organisation_dto.managers.append(org_manager_dto) return organisation_dto