Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Campaign Service and Campaign static methods #3893

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backend/api/organisations/campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def post(self, organisation_id, campaign_id):
if OrganisationService.can_user_manage_organisation(
organisation_id, token_auth.current_user()
):
if Campaign.campaign_organisation_exists(campaign_id, organisation_id):
if CampaignService.campaign_organisation_exists(
campaign_id, organisation_id
):
message = (
"Campaign {} is already assigned to organisation {}.".format(
campaign_id, organisation_id
Expand Down
73 changes: 13 additions & 60 deletions backend/models/postgis/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,66 +52,6 @@ def update(self, dto: CampaignDTO):
self.description = dto.description if dto.description else self.description
db.session.commit()

@staticmethod
def get_all_campaigns() -> CampaignListDTO:
query = Campaign.query.order_by(Campaign.name).distinct()
campaign_list_dto = CampaignListDTO()
for campaign in query:
campaign_dto = CampaignDTO()
campaign_dto.id = campaign.id
campaign_dto.name = campaign.name

campaign_list_dto.campaigns.append(campaign_dto)

return campaign_list_dto

@staticmethod
def get_project_campaigns_as_dto(project_id: int) -> CampaignListDTO:

query = (
Campaign.query.join(campaign_projects)
.filter(campaign_projects.c.project_id == project_id)
.all()
)
campaign_list_dto = CampaignListDTO()
for campaign in query:
campaign_dto = CampaignDTO()
campaign_dto.id = campaign.id
campaign_dto.name = campaign.name

campaign_list_dto.campaigns.append(campaign_dto)

return campaign_list_dto

@staticmethod
def campaign_organisation_exists(campaign_id: int, org_id: int):
return (
Campaign.query.join(campaign_organisations)
.filter(
campaign_organisations.c.organisation_id == org_id,
campaign_organisations.c.campaign_id == campaign_id,
)
.one_or_none()
)

@staticmethod
def get_organisation_campaigns_as_dto(org_id: int) -> CampaignListDTO:

query = (
Campaign.query.join(campaign_organisations)
.filter(campaign_organisations.c.organisation_id == org_id)
.all()
)
campaign_list_dto = CampaignListDTO()
for campaign in query:
campaign_dto = CampaignDTO()
campaign_dto.id = campaign.id
campaign_dto.name = campaign.name

campaign_list_dto.campaigns.append(campaign_dto)

return campaign_list_dto

@classmethod
def from_dto(cls, dto: CampaignDTO):
""" Creates new message from DTO """
Expand All @@ -133,3 +73,16 @@ def as_dto(self) -> CampaignDTO:
campaign_dto.description = self.description

return campaign_dto

@staticmethod
def campaign_list_as_dto(campaigns: list) -> CampaignListDTO:
""" Converts a collection of campaigns into DTO"""
campaign_list_dto = CampaignListDTO()
for campaign in campaigns:
campaign_dto = CampaignDTO()
campaign_dto.id = campaign.id
campaign_dto.name = campaign.name

campaign_list_dto.campaigns.append(campaign_dto)

return campaign_list_dto
57 changes: 30 additions & 27 deletions backend/services/campaign_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,26 @@ def delete_campaign(campaign_id: int):
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

@staticmethod
def get_project_campaigns_as_dto(project_id: int) -> CampaignListDTO:
"""Gets all the campaigns for a specified project"""
return Campaign.get_project_campaigns_as_dto(project_id)
query = (
Campaign.query.join(campaign_projects)
.filter(campaign_projects.c.project_id == project_id)
.all()
)

return Campaign.campaign_list_as_dto(query)

@staticmethod
def delete_project_campaign(project_id: int, campaign_id: int):
Expand All @@ -90,11 +74,14 @@ def delete_project_campaign(project_id: int, campaign_id: int):

@staticmethod
def get_all_campaigns() -> CampaignListDTO:
"""List all campaigns"""
return Campaign.get_all_campaigns()
""" Returns a list of all campaigns """
query = Campaign.query.order_by(Campaign.name).distinct()

return Campaign.campaign_list_as_dto(query)

@staticmethod
def create_campaign(campaign_dto: NewCampaignDTO):
""" Creates a new campaign """
campaign = Campaign.from_dto(campaign_dto)
try:
campaign.create()
Expand Down Expand Up @@ -136,7 +123,23 @@ def create_campaign_organisation(organisation_id: int, campaign_id: int):
@staticmethod
def get_organisation_campaigns_as_dto(organisation_id: int) -> CampaignListDTO:
""" Gets all the campaigns for a specified project """
return Campaign.get_organisation_campaigns_as_dto(organisation_id)
query = (
Campaign.query.join(campaign_organisations)
.filter(campaign_organisations.c.organisation_id == organisation_id)
.all()
)
return Campaign.campaign_list_as_dto(query)

@staticmethod
def campaign_organisation_exists(campaign_id: int, org_id: int):
return (
Campaign.query.join(campaign_organisations)
.filter(
campaign_organisations.c.organisation_id == org_id,
campaign_organisations.c.campaign_id == campaign_id,
)
.one_or_none()
)

@staticmethod
def delete_organisation_campaign(organisation_id: int, campaign_id: int):
Expand Down
32 changes: 32 additions & 0 deletions backend/services/organisation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ListOrganisationsDTO,
UpdateOrganisationDTO,
)
from backend.models.postgis.campaign import campaign_organisations
from backend.models.postgis.organisation import Organisation
from backend.models.postgis.project import Project, ProjectInfo
from backend.models.postgis.utils import NotFound
Expand Down Expand Up @@ -207,3 +208,34 @@ def is_user_an_org_manager(organisation_id: int, user_id: int):
user = UserService.get_user_by_id(user_id)

return user in org.managers

@staticmethod
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