Skip to content

Commit

Permalink
Merge pull request #6637 from hotosm/fastapi-refactor
Browse files Browse the repository at this point in the history
fix: Update teams and formatting
  • Loading branch information
prabinoid authored Nov 18, 2024
2 parents 4b354e2 + 5be6a30 commit 3d26059
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 114 deletions.
11 changes: 6 additions & 5 deletions backend/models/postgis/application.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
6 changes: 3 additions & 3 deletions backend/models/postgis/banner.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
5 changes: 3 additions & 2 deletions backend/models/postgis/campaign.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
15 changes: 4 additions & 11 deletions backend/models/postgis/interests.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
7 changes: 4 additions & 3 deletions backend/models/postgis/licenses.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
4 changes: 2 additions & 2 deletions backend/models/postgis/mapping_issues.py
Original file line number Diff line number Diff line change
@@ -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,
)


Expand Down
16 changes: 8 additions & 8 deletions backend/models/postgis/organisation.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
3 changes: 2 additions & 1 deletion backend/models/postgis/partner.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 0 additions & 26 deletions backend/models/postgis/priority_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
16 changes: 1 addition & 15 deletions backend/models/postgis/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
from datetime import timezone
from enum import Enum
from typing import Any, Dict, List

Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down
15 changes: 8 additions & 7 deletions backend/models/postgis/task_annotation.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
21 changes: 11 additions & 10 deletions backend/models/postgis/team.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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
Expand Down
35 changes: 14 additions & 21 deletions backend/models/postgis/user.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions backend/services/messaging/chat_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import threading

from databases import Database

from backend.exceptions import NotFound
Expand Down

0 comments on commit 3d26059

Please sign in to comment.