Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
fix: All API classes refactored to use the new PGSupport class
Browse files Browse the repository at this point in the history
  • Loading branch information
rsavoye committed Mar 4, 2024
1 parent ff04392 commit d98401e
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 182 deletions.
83 changes: 61 additions & 22 deletions tm_admin/campaigns/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,21 @@
from cpuinfo import get_cpu_info
from shapely.geometry import shape
from shapely import centroid
from tm_admin.types_tm import Mappingtypes, Projectstatus, Taskcreationmode, Editors, Permissions, Projectpriority, Projectdifficulty, Teamroles
from tm_admin.projects.projects_class import ProjectsTable
from tm_admin.tasks.tasks_class import TasksTable
from tm_admin.messages.messages import MessagesDB
from tm_admin.projects.projects import ProjectsDB
from tm_admin.types_tm import Mappingtypes
from tm_admin.campaigns.campaigns_class import CampaignsTable
from tm_admin.users.users import UsersDB
from tm_admin.teams.teams import TeamsDB
from shapely import wkb, get_coordinates
from tm_admin.dbsupport import DBSupport
from tm_admin.generator import Generator
from osm_rawdata.pgasync import PostgresClient
# from osm_rawdata.pgasync import PostgresClient
import re
# from progress import Bar, PixelBar
from tqdm import tqdm
import tqdm.asyncio
from codetiming import Timer
import asyncio
from tm_admin.pgsupport import PGSupport

# The number of threads is based on the CPU cores
info = get_cpu_info()
Expand All @@ -56,7 +54,7 @@
# Instantiate logger
log = logging.getLogger(__name__)

class CampaignsAPI(PostgresClient):
class CampaignsAPI(PGSupport):
def __init__(self):
"""
Create a class to handle the backend API calls, so the code can be shared
Expand All @@ -65,28 +63,64 @@ def __init__(self):
Returns:
(CampaignsAPI): An instance of this class
"""
self.allowed_roles = [
Teamroles.TEAM_MAPPER,
Teamroles.TEAM_VALIDATOR,
Teamroles.TEAM_MANAGER,
]
self.messagesdb = MessagesDB()
self.usersdb = UsersDB()
self.teamsdb = TeamsDB()

async def connectDBs(self,
# self.allowed_roles = [
# Teamroles.TEAM_MAPPER,
# Teamroles.TEAM_VALIDATOR,
# Teamroles.TEAM_MANAGER,
# ]
# self.messagesdb = MessagesDB()
# self.usersdb = UsersDB()
# self.teamsdb = TeamsDB()
super().__init__("campaigns")

async def initialize(self,
uri: str,
):
"""
Connect to all tables for API endpoints that require accessing multiple tables.
Connect to all tables for API endpoints that require
accessing multiple tables.
Args:
inuri (str): The URI for the TM Admin output database
"""
await self.connect(uri)
await self.messagesdb.connect(uri)
await self.usersdb.connect(uri)
await self.teamsdb.connect(uri)
await self.getTypes("campaigns")
#await self.usersdb.connect(uri)
#await self.teamsdb.connect(uri)

async def getByID(self,
org_id: int,
):
"""
Get all the information for an campaign using it's ID
Args:
project_id (int): The campaign to get the data for
Returns:
(dict): the campaign information
"""
# log.debug(f"--- getByID() ---")
sql = f"SELECT * FROM campaigns WHERE id={org_id}"
results = await self.execute(sql)
return results

async def getByName(self,
name: str,
):
"""
Get all the information for a campaign using the name
Args:
name (str): The campaign to get the data for
Returns:
(dict): the campaign information
"""
# log.debug(f"--- getByName() ---")
sql = f"SELECT * FROM campaigns WHERE name='{name}'"
results = await self.execute(sql)
return results

async def create(self,
campaign: CampaignsTable,
Expand All @@ -100,7 +134,12 @@ async def create(self,
Returns:
(bool): Whether the campaign got created
"""
log.warning(f"create(): unimplemented!")
# log.warning(f"create(): unimplemented!")
result = await self.insertRecords([campaign])

# The ID of the record that just got inserted is returned
if result:
return True

return False

Expand Down
77 changes: 58 additions & 19 deletions tm_admin/messages/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@
from shapely import centroid
from tm_admin.types_tm import Mappingtypes
from tm_admin.messages.messages_class import MessagesTable
from tm_admin.tasks.tasks_class import TasksTable
# from tm_admin.tasks.tasks_class import TasksTable
from tm_admin.messages.messages import MessagesDB
from tm_admin.projects.projects import ProjectsDB
# from tm_admin.projects.projects import ProjectsDB
from tm_admin.users.users import UsersDB
from tm_admin.teams.teams import TeamsDB
# from tm_admin.teams.teams import TeamsDB
from shapely import wkb, get_coordinates
from tm_admin.dbsupport import DBSupport
from tm_admin.generator import Generator
from osm_rawdata.pgasync import PostgresClient
# from osm_rawdata.pgasync import PostgresClient
import re
# from progress import Bar, PixelBar
from tqdm import tqdm
import tqdm.asyncio
from codetiming import Timer
import asyncio
from tm_admin.pgsupport import PGSupport

# The number of threads is based on the CPU cores
info = get_cpu_info()
Expand All @@ -56,7 +57,7 @@
# Instantiate logger
log = logging.getLogger(__name__)

class MessagesAPI(PostgresClient):
class MessagesAPI(PGSupport):
def __init__(self):
"""
Create a class to handle the backend API calls, so the code can be shared
Expand All @@ -65,28 +66,60 @@ def __init__(self):
Returns:
(MessagesAPI): An instance of this class
"""
self.allowed_roles = [
Teamroles.TEAM_MAPPER,
Teamroles.TEAM_VALIDATOR,
Teamroles.TEAM_MANAGER,
]
self.messagesdb = MessagesDB()
self.usersdb = UsersDB()
self.teamsdb = TeamsDB()

async def connectDBs(self,
# self.messagesdb = MessagesDB()
# self.usersdb = UsersDB()
# self.teamsdb = TeamsDB()

super().__init__("campaigns")

async def initialize(self,
uri: str,
):
"""
Connect to all tables for API endpoints that require accessing multiple tables.
Connect to all tables for API endpoints that require
accessing multiple tables.
Args:
inuri (str): The URI for the TM Admin output database
"""
await self.connect(uri)
await self.messagesdb.connect(uri)
await self.usersdb.connect(uri)
await self.teamsdb.connect(uri)
await self.getTypes("campaigns")
#await self.usersdb.connect(uri)
#await self.teamsdb.connect(uri)

async def getByID(self,
org_id: int,
):
"""
Get all the information for an message using it's ID
Args:
project_id (int): The message to get the data for
Returns:
(dict): the message information
"""
# log.debug(f"--- getByID() ---")
sql = f"SELECT * FROM messages WHERE id={org_id}"
results = await self.execute(sql)
return results

async def getByName(self,
name: str,
):
"""
Get all the information for a message using the name
Args:
name (str): The message to get the data for
Returns:
(dict): the message information
"""
# log.debug(f"--- getByName() ---")
sql = f"SELECT * FROM messages WHERE name='{name}'"
results = await self.execute(sql)
return results

async def create(self,
message: MessagesTable,
Expand All @@ -102,6 +135,12 @@ async def create(self,
"""
log.warning(f"create(): unimplemented!")

result = await self.insertRecords([message])

# The ID of the record that just got inserted is returned
if result:
return True

return False

async def update(self,
Expand Down
Loading

0 comments on commit d98401e

Please sign in to comment.