diff --git a/backend/models/postgis/application.py b/backend/models/postgis/application.py index 7d79740225..9deec88224 100644 --- a/backend/models/postgis/application.py +++ b/backend/models/postgis/application.py @@ -1,18 +1,19 @@ from databases import Database from sqlalchemy import ( - Column, BigInteger, - String, + Column, DateTime, ForeignKey, - select, - insert, + String, delete, + insert, + select, ) + +from backend.db import Base, get_session from backend.models.dtos.application_dto import ApplicationDTO, ApplicationsDTO from backend.models.postgis.utils import timestamp from backend.services.users.authentication_service import AuthenticationService -from backend.db import Base, get_session session = get_session() diff --git a/backend/models/postgis/banner.py b/backend/models/postgis/banner.py index 2f5784dfa8..d9c341e5a9 100644 --- a/backend/models/postgis/banner.py +++ b/backend/models/postgis/banner.py @@ -1,10 +1,10 @@ import bleach +from databases import Database from markdown import markdown +from sqlalchemy import Boolean, Column, Integer, String, insert, update -from sqlalchemy import Column, Integer, String, Boolean, insert, update -from backend.models.dtos.banner_dto import BannerDTO from backend.db import Base, get_session -from databases import Database +from backend.models.dtos.banner_dto import BannerDTO session = get_session() diff --git a/backend/models/postgis/campaign.py b/backend/models/postgis/campaign.py index ebb12d66e6..70dcce24a6 100644 --- a/backend/models/postgis/campaign.py +++ b/backend/models/postgis/campaign.py @@ -1,6 +1,7 @@ -from sqlalchemy import Column, String, Integer, ForeignKey, Table, UniqueConstraint -from backend.models.dtos.campaign_dto import CampaignDTO, CampaignListDTO +from sqlalchemy import Column, ForeignKey, Integer, String, Table, UniqueConstraint + from backend.db import Base, get_session +from backend.models.dtos.campaign_dto import CampaignDTO, CampaignListDTO session = get_session() diff --git a/backend/models/postgis/interests.py b/backend/models/postgis/interests.py index df480fa9bb..57bb98c3ec 100644 --- a/backend/models/postgis/interests.py +++ b/backend/models/postgis/interests.py @@ -1,16 +1,9 @@ -from sqlalchemy import ( - Column, - Integer, - String, - BigInteger, - Table, - ForeignKey, - select, -) +from databases import Database +from sqlalchemy import BigInteger, Column, ForeignKey, Integer, String, Table, select + +from backend.db import Base, get_session from backend.exceptions import NotFound from backend.models.dtos.interests_dto import InterestDTO, InterestsListDTO -from backend.db import Base, get_session -from databases import Database session = get_session() diff --git a/backend/models/postgis/licenses.py b/backend/models/postgis/licenses.py index d8b2cd5bb5..983bdaa23f 100644 --- a/backend/models/postgis/licenses.py +++ b/backend/models/postgis/licenses.py @@ -1,9 +1,10 @@ -from sqlalchemy import Column, String, Integer, BigInteger, Table, ForeignKey +from databases import Database +from sqlalchemy import BigInteger, Column, ForeignKey, Integer, String, Table from sqlalchemy.orm import relationship + +from backend.db import Base, get_session from backend.exceptions import NotFound from backend.models.dtos.licenses_dto import LicenseDTO, LicenseListDTO -from backend.db import Base, get_session -from databases import Database session = get_session() diff --git a/backend/models/postgis/mapping_issues.py b/backend/models/postgis/mapping_issues.py index 8093f87687..424fcbb5e6 100644 --- a/backend/models/postgis/mapping_issues.py +++ b/backend/models/postgis/mapping_issues.py @@ -1,10 +1,10 @@ from databases import Database -from sqlalchemy import Column, Integer, String, Boolean, select, insert, delete, update +from sqlalchemy import Boolean, Column, Integer, String, delete, insert, select, update from backend.db import Base from backend.models.dtos.mapping_issues_dto import ( - MappingIssueCategoryDTO, MappingIssueCategoriesDTO, + MappingIssueCategoryDTO, ) diff --git a/backend/models/postgis/organisation.py b/backend/models/postgis/organisation.py index d044cabd52..a2ea5512c6 100644 --- a/backend/models/postgis/organisation.py +++ b/backend/models/postgis/organisation.py @@ -1,28 +1,28 @@ -from fastapi import HTTPException from databases import Database +from fastapi import HTTPException from slugify import slugify - from sqlalchemy import ( + BigInteger, Column, + ForeignKey, Integer, String, - BigInteger, - ForeignKey, Table, UniqueConstraint, ) -from sqlalchemy.orm import relationship, backref +from sqlalchemy.orm import backref, relationship + +from backend.db import Base, get_session from backend.exceptions import NotFound from backend.models.dtos.organisation_dto import ( - OrganisationDTO, NewOrganisationDTO, + OrganisationDTO, OrganisationManagerDTO, UpdateOrganisationDTO, ) -from backend.models.postgis.user import User from backend.models.postgis.campaign import Campaign, campaign_organisations from backend.models.postgis.statuses import OrganisationType -from backend.db import Base, get_session +from backend.models.postgis.user import User session = get_session() diff --git a/backend/models/postgis/partner.py b/backend/models/postgis/partner.py index 507f0fb4df..8b595e0e36 100644 --- a/backend/models/postgis/partner.py +++ b/backend/models/postgis/partner.py @@ -1,5 +1,6 @@ -from backend import db import json + +from backend import db from backend.exceptions import NotFound from backend.models.dtos.partner_dto import PartnerDTO diff --git a/backend/models/postgis/priority_area.py b/backend/models/postgis/priority_area.py index 249a87bbd5..0e0243bff5 100644 --- a/backend/models/postgis/priority_area.py +++ b/backend/models/postgis/priority_area.py @@ -27,32 +27,6 @@ class PriorityArea(Base): id = Column(Integer, primary_key=True) geometry = Column(Geometry("POLYGON", srid=4326)) - # @classmethod - # async def from_dict(cls, area_poly: dict, db: Database): - # """Create a new Priority Area from dictionary""" - # pa_geojson = geojson.loads(json.dumps(area_poly)) - - # if type(pa_geojson) is not geojson.Polygon: - # raise InvalidGeoJson("Priority Areas must be supplied as Polygons") - - # if not pa_geojson.is_valid: - # raise InvalidGeoJson( - # "Priority Area: Invalid Polygon - " + ", ".join(pa_geojson.errors()) - # ) - - # pa = cls() - # valid_geojson = geojson.dumps(pa_geojson) - # query = """ - # SELECT ST_AsText( - # ST_SetSRID( - # ST_GeomFromGeoJSON(:geojson), 4326 - # ) - # ) AS geometry_wkt; - # """ - # result = await db.fetch_one(query=query, values={"geojson": valid_geojson}) - # pa.geometry = result["geometry_wkt"] if result else None - # return pa - @classmethod async def from_dict(cls, area_poly: dict, db: Database): """Create a new Priority Area from dictionary and insert into the database.""" diff --git a/backend/models/postgis/task.py b/backend/models/postgis/task.py index 3138d796d4..34601c060a 100644 --- a/backend/models/postgis/task.py +++ b/backend/models/postgis/task.py @@ -1,5 +1,6 @@ import datetime import json +from datetime import timezone from enum import Enum from typing import Any, Dict, List @@ -26,7 +27,6 @@ ) from sqlalchemy.orm import relationship from sqlalchemy.orm.exc import MultipleResultsFound -from datetime import timezone from backend.db import Base, get_session from backend.exceptions import NotFound @@ -826,15 +826,6 @@ async def exists(task_id: int, project_id: int, db: Database) -> bool: ) return task is not None - # @staticmethod - # def get_tasks(project_id: int, task_ids: List[int]): - # """Get all tasks that match supplied list""" - # return ( - # session.query(Task) - # .filter(Task.project_id == project_id, Task.id.in_(task_ids)) - # .all() - # ) - @staticmethod async def get_tasks(project_id: int, task_ids: List[int], db: Database): """ @@ -850,11 +841,6 @@ async def get_tasks(project_id: int, task_ids: List[int], db: Database): rows = await db.fetch_all(query=query, values=values) return rows - # @staticmethod - # def get_all_tasks(project_id: int): - # """Get all tasks for a given project""" - # return session.query(Task).filter(Task.project_id == project_id).all() - @staticmethod async def get_all_tasks(project_id: int, db: Database): """ diff --git a/backend/models/postgis/task_annotation.py b/backend/models/postgis/task_annotation.py index 53ab5d396b..f813de9a69 100644 --- a/backend/models/postgis/task_annotation.py +++ b/backend/models/postgis/task_annotation.py @@ -1,17 +1,18 @@ -from backend.models.postgis.utils import timestamp from sqlalchemy import ( + JSON, Column, - Integer, - String, DateTime, ForeignKey, - JSON, - Index, ForeignKeyConstraint, + Index, + Integer, + String, ) -from backend.models.dtos.task_annotation_dto import TaskAnnotationDTO -from backend.models.dtos.project_dto import ProjectTaskAnnotationsDTO + from backend.db import Base, get_session +from backend.models.dtos.project_dto import ProjectTaskAnnotationsDTO +from backend.models.dtos.task_annotation_dto import TaskAnnotationDTO +from backend.models.postgis.utils import timestamp session = get_session() diff --git a/backend/models/postgis/team.py b/backend/models/postgis/team.py index ce660c34d8..fb26f1de2c 100644 --- a/backend/models/postgis/team.py +++ b/backend/models/postgis/team.py @@ -1,32 +1,33 @@ from databases import Database from sqlalchemy import ( - Column, - Integer, BigInteger, Boolean, + Column, ForeignKey, + Integer, String, insert, + select, ) -from sqlalchemy.orm import relationship, backref +from sqlalchemy.orm import backref, relationship + +from backend.db import Base, get_session from backend.exceptions import NotFound +from backend.models.dtos.organisation_dto import OrganisationTeamsDTO from backend.models.dtos.team_dto import ( - TeamDTO, NewTeamDTO, + TeamDTO, TeamMembersDTO, TeamProjectDTO, ) -from backend.models.dtos.organisation_dto import OrganisationTeamsDTO from backend.models.postgis.organisation import Organisation from backend.models.postgis.statuses import ( TeamJoinMethod, - TeamVisibility, TeamMemberFunctions, TeamRoles, + TeamVisibility, ) from backend.models.postgis.user import User -from backend.db import Base, get_session -from sqlalchemy import select session = get_session() @@ -504,10 +505,10 @@ async def update_team_members(team, team_dto: TeamDTO, db: Database): ) if existing_members_list != new_member_list: for member in existing_members_list: - if member.user_id not in new_member_list: + if member not in new_member_list: await db.execute( "DELETE FROM team_members WHERE team_id = :team_id AND user_id = :user_id", - {"team_id": team.id, "user_id": member.user_id}, + {"team_id": team.id, "user_id": member}, ) # Add or update members from the new member list diff --git a/backend/models/postgis/user.py b/backend/models/postgis/user.py index 99c8e9385e..e51560a870 100644 --- a/backend/models/postgis/user.py +++ b/backend/models/postgis/user.py @@ -1,42 +1,41 @@ import geojson from sqlalchemy import ( + ARRAY, + BigInteger, + Boolean, Column, + DateTime, Integer, - BigInteger, String, - DateTime, - Boolean, - ARRAY, delete, - update, insert, + update, ) from sqlalchemy.orm import relationship +from backend.db import Base, get_session from backend.exceptions import NotFound from backend.models.dtos.user_dto import ( - UserDTO, - UserMappedProjectsDTO, + ListedUser, MappedProject, - UserFilterDTO, Pagination, - UserSearchQuery, - UserSearchDTO, ProjectParticipantUser, - ListedUser, + UserDTO, + UserFilterDTO, + UserMappedProjectsDTO, + UserSearchDTO, + UserSearchQuery, ) +from backend.models.postgis.interests import Interest, user_interests from backend.models.postgis.licenses import License, user_licenses_table from backend.models.postgis.project_info import ProjectInfo from backend.models.postgis.statuses import ( MappingLevel, ProjectStatus, - UserRole, UserGender, + UserRole, ) - from backend.models.postgis.utils import timestamp -from backend.models.postgis.interests import Interest, user_interests -from backend.db import Base, get_session session = get_session() from databases import Database @@ -95,12 +94,6 @@ def create(self): def save(self): session.commit() - # @staticmethod - # async def get_by_id(user_id: int, session): - # """Return the user for the specified id, or None if not found""" - # result = await session.execute(sa.select(User).filter_by(id=user_id)) - # return result.scalars().first() - @staticmethod async def get_by_id(user_id: int, db: Database): """ diff --git a/backend/services/messaging/chat_service.py b/backend/services/messaging/chat_service.py index 2f46590e74..9f2c50eec8 100644 --- a/backend/services/messaging/chat_service.py +++ b/backend/services/messaging/chat_service.py @@ -1,4 +1,5 @@ import threading + from databases import Database from backend.exceptions import NotFound