Skip to content

Commit

Permalink
refactor: fix all linting errors for code
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 13, 2024
1 parent 1b0e4de commit 5d0546f
Show file tree
Hide file tree
Showing 20 changed files with 374 additions and 404 deletions.
1 change: 1 addition & 0 deletions src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ async def my_data(
"""Read access token and get user details from OSM.
Args:
request: The HTTP request (automatically included variable).
db: The db session.
user_data: User data provided by osm-login-python Auth.
Expand Down
6 changes: 3 additions & 3 deletions src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
from pyxform.xls2xform import xls2xform_convert
from sqlalchemy.orm import Session

from ..config import settings
from ..db import db_models
from ..projects import project_schemas
from app.config import settings
from app.db import db_models
from app.projects import project_schemas


def get_odk_project(odk_central: project_schemas.ODKCentral = None):
Expand Down
48 changes: 28 additions & 20 deletions src/backend/app/central/central_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#
"""Routes to relay requests to ODK Central server."""

import json

from fastapi import APIRouter, Depends, HTTPException
Expand All @@ -29,9 +31,9 @@
from sqlalchemy.orm import Session
from sqlalchemy.sql import text

from ..central import central_crud
from ..db import database
from ..projects import project_crud, project_schemas
from app.central import central_crud
from app.db import database
from app.projects import project_crud, project_schemas

router = APIRouter(
prefix="/central",
Expand Down Expand Up @@ -75,17 +77,21 @@ async def create_appuser(
async def get_form_lists(
db: Session = Depends(database.get_db), skip: int = 0, limit: int = 100
):
"""This function retrieves a list of XForms from a database,
with the option to skip a certain number of records and limit the number of records returned.
"""Get a list of all XForms on ODK Central.
Option to skip a certain number of records and limit the number of
records returned.
Parameters:
skip:int: the number of records to skip before starting to retrieve records. Defaults to 0 if not provided.
limit:int: the maximum number of records to retrieve. Defaults to 10 if not provided.
skip (int): the number of records to skip before starting to retrieve records.
Defaults to 0 if not provided.
limit (int): the maximum number of records to retrieve.
Defaults to 10 if not provided.
Returns:
A list of dictionary containing the id and title of each XForm record retrieved from the database.
list[dict]: list of id:title dicts of each XForm record.
"""
# NOTE runs in separate thread using run_in_threadpool
forms = await run_in_threadpool(lambda: central_crud.get_form_list(db, skip, limit))
Expand Down Expand Up @@ -138,7 +144,8 @@ async def list_submissions(
project_id: int,
xml_form_id: str = None,
db: Session = Depends(database.get_db),
):
) -> list[dict]:
"""Get all submissions JSONs for a project."""
try:
project = table(
"projects",
Expand Down Expand Up @@ -188,20 +195,21 @@ async def list_submissions(
@router.get("/submission")
async def get_submission(
project_id: int,
xmlFormId: str = None,
xml_form_id: str = None,
submission_id: str = None,
db: Session = Depends(database.get_db),
):
"""This api returns the submission json.
) -> dict:
"""Return the submission JSON for a single XForm.
Parameters:
project_id:int the id of the project in the database.
xml_form_id:str: the xmlFormId of the form in Central.
submission_id:str: the submission id of the submission in Central.
project_id (int): the id of the project in the database.
xml_form_id (str): the xml_form_id of the form in Central.
submission_id (str): the submission id of the submission in Central.
If the submission_id is provided, an individual submission is returned.
Returns: Submission json.
Returns:
dict: Submission JSON.
"""
try:
"""Download the submissions data from Central."""
Expand Down Expand Up @@ -231,9 +239,9 @@ async def get_submission(

submissions = []

if xmlFormId and submission_id:
if xml_form_id and submission_id:
data = central_crud.download_submissions(
first.odkid, xmlFormId, submission_id, True, odk_credentials
first.odkid, xml_form_id, submission_id, True, odk_credentials
)
if submissions != 0:
submissions.append(json.loads(data[0]))
Expand All @@ -242,7 +250,7 @@ async def get_submission(
submissions.append(json.loads(data[entry]))

else:
if not xmlFormId:
if not xml_form_id:
xforms = central_crud.list_odk_xforms(first.odkid, odk_credentials)
for xform in xforms:
try:
Expand All @@ -262,7 +270,7 @@ async def get_submission(
submissions.append(json.loads(data[entry]))
else:
data = central_crud.download_submissions(
first.odkid, xmlFormId, None, True, odk_credentials
first.odkid, xml_form_id, None, True, odk_credentials
)
submissions.append(json.loads(data[0]))
if len(data) >= 2:
Expand Down
12 changes: 12 additions & 0 deletions src/backend/app/central/central_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,40 @@
# You should have received a copy of the GNU General Public License
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#
"""Schemas for returned ODK Central objects."""

from enum import Enum

from pydantic import BaseModel


class CentralBase(BaseModel):
"""ODK Central return."""

central_url: str


class Central(CentralBase):
"""ODK Central return, with extras."""

geometry_geojson: str
# qr_code_binary: bytes


class CentralOut(CentralBase):
"""ODK Central output."""

pass


class CentralFileType(BaseModel):
"""ODK Central file return."""

filetype: Enum("FileType", ["xform", "extract", "zip", "xlsform", "all"])
pass


class CentralDetails(CentralBase):
"""ODK Central details."""

pass
7 changes: 3 additions & 4 deletions src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
relationship,
)

from app.db.database import Base, FmtmMetadata
from app.db.postgis_utils import timestamp
from app.models.enums import (
BackgroundTaskStatus,
MappingLevel,
Expand All @@ -60,9 +62,6 @@
ValidationPermission,
)

from .database import Base, FmtmMetadata
from .postgis_utils import timestamp


class DbUserRoles(Base):
"""Fine grained user access for projects, described by roles."""
Expand Down Expand Up @@ -448,7 +447,7 @@ class DbProject(Base):
DbProjectInfo,
cascade="all, delete, delete-orphan",
uselist=False,
backref="projects",
backref="project",
)
location_str = Column(String)

Expand Down
1 change: 1 addition & 0 deletions src/backend/app/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class IntEnum(int, Enum):

class HTTPStatus(IntEnum):
"""All HTTP status codes used in endpoints."""

# Success
OK = 200
CREATED = 201
Expand Down
10 changes: 8 additions & 2 deletions src/backend/app/models/languages_and_countries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# see https://gist.github.com/alexanderjulo/4073388
"""Language and country codes for reference.
see https://gist.github.com/alexanderjulo/4073388
"""

languages = [
("aa", "Afar"),
Expand Down Expand Up @@ -34,7 +37,10 @@
("zh", "Chinese"),
(
"cu",
"Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic",
(
"Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; "
"Old Church Slavonic",
),
),
("cv", "Chuvash"),
("kw", "Cornish"),
Expand Down
4 changes: 4 additions & 0 deletions src/backend/app/pagination/pagination.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Logic for API pagination."""

import math
from typing import List


def get_pages_nav(total_pages, current_page):
"""Get page position (prev / next pages)."""
next_page = None
prev_page = None
if current_page + 1 <= total_pages:
Expand All @@ -13,6 +16,7 @@ def get_pages_nav(total_pages, current_page):


def paginate_data(data: List[dict], page_no: int, page_size: int, total_content: int):
"""Generate pagination JSON."""
total_pages = math.ceil(total_content / page_size)
next_page, prev_page = get_pages_nav(total_pages, page_no)

Expand Down
30 changes: 7 additions & 23 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2064,22 +2064,6 @@ async def update_project_form(
return True


async def update_odk_credentials_in_db(
project_instance: project_schemas.ProjectUpload,
odk_central_cred: project_schemas.ODKCentral,
odkid: int,
db: Session,
):
"""Update odk credentials for a project."""
project_instance.odkid = odkid
project_instance.odk_central_url = odk_central_cred.odk_central_url
project_instance.odk_central_user = odk_central_cred.odk_central_user
project_instance.odk_central_password = odk_central_cred.odk_central_password

db.commit()
db.refresh(project_instance)


async def get_extracted_data_from_db(db: Session, project_id: int, outfile: str):
"""Get the geojson of those features for this project."""
query = text(
Expand Down Expand Up @@ -2358,17 +2342,17 @@ async def get_tasks_count(db: Session, project_id: int):
async def get_pagination(page: int, count: int, results_per_page: int, total: int):
"""Pagination result for splash page."""
total_pages = (count + results_per_page - 1) // results_per_page
hasNext = (page * results_per_page) < count # noqa: N806
hasPrev = page > 1 # noqa: N806
has_next = (page * results_per_page) < count # noqa: N806
has_prev = page > 1 # noqa: N806

pagination = project_schemas.PaginationInfo(
hasNext=hasNext,
hasPrev=hasPrev,
nextNum=page + 1 if hasNext else None,
has_next=has_next,
has_prev=has_prev,
next_num=page + 1 if has_next else None,
page=page,
pages=total_pages,
prevNum=page - 1 if hasPrev else None,
perPage=results_per_page,
prev_num=page - 1 if has_prev else None,
per_page=results_per_page,
total=total,
)

Expand Down
Loading

0 comments on commit 5d0546f

Please sign in to comment.