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

fix: Recommended projects, my tasks in my contrib section and cleanups #6653

Merged
merged 1 commit into from
Dec 4, 2024
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
5 changes: 1 addition & 4 deletions backend/api/comments/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi.responses import JSONResponse
from loguru import logger

from backend.db import get_db, get_session
from backend.db import get_db
from backend.models.dtos.mapping_dto import TaskCommentDTO
from backend.models.dtos.message_dto import ChatMessageDTO
from backend.models.dtos.user_dto import AuthUserDTO
Expand All @@ -14,9 +14,6 @@
from backend.services.users.authentication_service import login_required
from backend.services.users.user_service import UserService

session = get_session()


router = APIRouter(
prefix="/projects",
tags=["projects"],
Expand Down
21 changes: 14 additions & 7 deletions backend/api/tasks/statistics.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from datetime import date, timedelta
from backend.services.stats_service import StatsService
from backend.api.utils import validate_date_input

from databases import Database
from fastapi import APIRouter, Depends, Request
from backend.db import get_session
from starlette.authentication import requires

from backend.api.utils import validate_date_input
from backend.db import get_db
from backend.models.dtos.user_dto import AuthUserDTO
from backend.services.stats_service import StatsService
from backend.services.users.authentication_service import login_required

router = APIRouter(
prefix="/tasks",
tags=["tasks"],
dependencies=[Depends(get_session)],
responses={404: {"description": "Not found"}},
)


@router.get("/statistics/")
@requires("authenticated")
async def get(request: Request):
async def get(
request: Request,
organisation_id: int,
db: Database = Depends(get_db),
user: AuthUserDTO = Depends(login_required),
):
"""
Get Task Stats
---
Expand Down
13 changes: 5 additions & 8 deletions backend/api/users/actions.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from fastapi import APIRouter, Depends, Request, Body
from databases import Database
from fastapi import APIRouter, Body, Depends, Request
from fastapi.responses import JSONResponse
from loguru import logger

from backend.models.dtos.user_dto import UserDTO, UserRegisterEmailDTO
from databases import Database
from backend.db import get_db
from backend.services.users.authentication_service import login_required
from backend.models.dtos.user_dto import AuthUserDTO
from backend.models.dtos.user_dto import AuthUserDTO, UserDTO, UserRegisterEmailDTO
from backend.services.interests_service import InterestService
from backend.services.messaging.message_service import MessageService
from backend.services.users.authentication_service import login_required
from backend.services.users.user_service import UserService, UserServiceError
from backend.services.interests_service import InterestService
from backend.db import get_session

router = APIRouter(
prefix="/users",
tags=["users"],
dependencies=[Depends(get_session)],
responses={404: {"description": "Not found"}},
)

Expand Down
23 changes: 13 additions & 10 deletions backend/api/users/openstreetmap.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from backend.services.users.user_service import UserService, OSMServiceError
from databases import Database
from fastapi import APIRouter, Depends, Request
from backend.db import get_session
from starlette.authentication import requires
from fastapi.responses import JSONResponse

from backend.db import get_db
from databases import Database
from backend.models.dtos.user_dto import AuthUserDTO
from backend.services.users.authentication_service import login_required
from backend.services.users.user_service import OSMServiceError, UserService

router = APIRouter(
prefix="/users",
tags=["users"],
dependencies=[Depends(get_session)],
responses={404: {"description": "Not found"}},
)


# class UsersOpenStreetMapAPI(Resource):
# @token_auth.login_required
@router.get("/{username}/openstreetmap/")
@requires("authenticated")
async def get(request: Request, db: Database = Depends(get_db), username: str = None):
async def get(
request: Request,
db: Database = Depends(get_db),
user: AuthUserDTO = Depends(login_required),
username: str = None,
):
"""
Get details from OpenStreetMap for a specified username
---
Expand Down Expand Up @@ -54,4 +57,4 @@ async def get(request: Request, db: Database = Depends(get_db), username: str =
osm_dto = await UserService.get_osm_details_for_user(username, db)
return osm_dto.model_dump(by_alias=True)
except OSMServiceError as e:
return {"Error": str(e)}, 502
return JSONResponse(content={"Error": str(e)}, status_code=502)
3 changes: 1 addition & 2 deletions backend/api/users/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from loguru import logger


from backend.db import get_db, get_session
from backend.db import get_db
from backend.models.dtos.user_dto import AuthUserDTO, UserSearchQuery
from backend.services.project_service import ProjectService
from backend.services.users.authentication_service import login_required
Expand All @@ -16,7 +16,6 @@
router = APIRouter(
prefix="/users",
tags=["users"],
dependencies=[Depends(get_session)],
responses={404: {"description": "Not found"}},
)

Expand Down
2 changes: 1 addition & 1 deletion backend/api/users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def get(
)
sort_by = request.query_params.get("sort_by", "-action_date")

tasks = UserService.get_tasks_dto(
tasks = await UserService.get_tasks_dto(
user.id,
project_id=project_id,
project_status=project_status,
Expand Down
6 changes: 0 additions & 6 deletions backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ def create_db_session(self):
db_connection = DatabaseConnection() # Create a single instance


# remove
def get_session():
"""Yield a new database session."""
return db_connection.create_db_session()


async def get_db():
"""Get the database connection from the pool."""
async with db_connection.database.connection() as connection:
Expand Down
11 changes: 0 additions & 11 deletions backend/models/dtos/interests_dto.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
from pydantic import BaseModel, Field
from typing import Optional, List

# class InterestDTO(Model):
# """DTO for a interest."""

# id = IntType()
# name = StringType(required=True, min_length=1)
# user_selected = BooleanType(
# serialized_name="userSelected", serialize_when_none=False
# )
# count_projects = IntType(serialize_when_none=False, serialized_name="countProjects")
# count_users = IntType(serialize_when_none=False, serialized_name="countUsers")


class InterestDTO(BaseModel):
id: Optional[int] = None
Expand Down
6 changes: 2 additions & 4 deletions backend/models/dtos/project_dto.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# from schematics import Model
# from schematics.exceptions import ValidationError
from datetime import date, datetime
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Union, Any

from fastapi import HTTPException
from pydantic import BaseModel, Field, root_validator
Expand Down Expand Up @@ -452,7 +450,7 @@ class Config:
class ProjectSearchResultsDTO(BaseModel):
"""Contains all results for the search criteria"""

map_results: Optional[List] = Field(default_factory=list, alias="mapResults")
map_results: Optional[Any] = Field(default_factory=list, alias="mapResults")
results: Optional[List["ListSearchResultDTO"]] = Field(default_factory=list)
pagination: Optional["Pagination"] = Field(default_factory=dict)

Expand Down
4 changes: 2 additions & 2 deletions backend/models/dtos/user_dto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import List, Optional
from typing import List, Optional, Dict

from pydantic import BaseModel, Field
from pydantic.functional_validators import field_validator
Expand Down Expand Up @@ -154,7 +154,7 @@ class MappedProject(BaseModel):
tasks_mapped: Optional[int] = Field(None, alias="tasksMapped")
tasks_validated: Optional[int] = Field(None, alias="tasksValidated")
status: Optional[str] = None
centroid: Optional[str] = None
centroid: Optional[Dict] = None

class Config:
populate_by_name = True
Expand Down
7 changes: 1 addition & 6 deletions backend/models/postgis/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
select,
)

from backend.db import Base, get_session
from backend.db import Base
from backend.models.dtos.application_dto import ApplicationDTO, ApplicationsDTO
from backend.models.postgis.utils import timestamp
from backend.services.users.authentication_service import AuthenticationService

session = get_session()


class Application(Base):
"""Describes an application that is authorized to access the TM"""
Expand Down Expand Up @@ -45,9 +43,6 @@ async def create(self, user_id, db: Database):
await db.execute(query)
return application

def save(self):
session.commit()

async def delete(self, db: Database):
query = delete(Application).where(Application.id == self.id)
await db.execute(query)
Expand Down
8 changes: 1 addition & 7 deletions backend/models/postgis/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from markdown import markdown
from sqlalchemy import Boolean, Column, Integer, String, insert, update

from backend.db import Base, get_session
from backend.db import Base
from backend.models.dtos.banner_dto import BannerDTO

session = get_session()


class Banner(Base):
"""Model for Banners"""
Expand All @@ -26,10 +24,6 @@ async def create(self, db: Database):
)
await db.execute(query)

def update(self):
"""Updates the current model in the DB"""
session.commit()

async def update_from_dto(self, db: Database, dto: BannerDTO):
"""Updates the current model in the DB"""
self.message = dto.message
Expand Down
24 changes: 1 addition & 23 deletions backend/models/postgis/campaign.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from sqlalchemy import Column, ForeignKey, Integer, String, Table, UniqueConstraint

from backend.db import Base, get_session
from backend.db import Base
from backend.models.dtos.campaign_dto import CampaignDTO, CampaignListDTO

session = get_session()

campaign_projects = Table(
"campaign_projects",
Expand Down Expand Up @@ -34,27 +33,6 @@ class Campaign(Base):
url = Column(String)
description = Column(String)

def create(self):
"""Creates and saves the current model to the DB"""
session.add(self)
session.commit()

def delete(self):
"""Deletes the current model from the DB"""
session.delete(self)
session.commit()

def save(self):
session.commit()

def update(self, dto: CampaignDTO):
"""Update the user details"""
self.name = dto.name if dto.name else self.name
self.logo = dto.logo if dto.logo else self.logo
self.url = dto.url if dto.url else self.url
self.description = dto.description if dto.description else self.description
session.commit()

@classmethod
def from_dto(cls, dto: CampaignDTO):
"""Creates new message from DTO"""
Expand Down
18 changes: 1 addition & 17 deletions backend/models/postgis/custom_editors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from databases import Database
from sqlalchemy import Column, ForeignKey, Integer, String, delete, update

from backend.db import Base, get_session
from backend.db import Base
from backend.models.dtos.project_dto import CustomEditorDTO

session = get_session()


class CustomEditor(Base):
"""Model for user defined editors for a project"""
Expand All @@ -16,20 +14,6 @@ class CustomEditor(Base):
description = Column(String)
url = Column(String, nullable=False)

def create(self):
"""Creates and saves the current model to the DB"""
session.add(self)
session.commit()

def save(self):
"""Save changes to db"""
session.commit()

@staticmethod
def get_by_project_id(project_id: int):
"""Get custom editor by it's project id"""
return session.get(CustomEditor, project_id)

@classmethod
async def create_from_dto(cls, project_id: int, dto: CustomEditorDTO, db: Database):
"""Creates a new CustomEditor from dto, used in project edit"""
Expand Down
43 changes: 2 additions & 41 deletions backend/models/postgis/interests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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
from backend.models.dtos.interests_dto import InterestDTO

session = get_session()

# Secondary table defining many-to-many join for interests of a user.
user_interests = Table(
Expand Down Expand Up @@ -43,47 +41,10 @@ async def get_by_id(interest_id: int, db: Database):
return Interest(**result)
return None

@staticmethod
def get_by_name(name: str):
"""Get interest by name"""
interest = session.query(Interest).filter(Interest.name == name).first()
if interest is None:
raise NotFound(sub_code="INTEREST_NOT_FOUND", interest_name=name)

return interest

def update(self, dto):
"""Update existing interest"""
self.name = dto.name
session.commit()

def create(self):
"""Creates and saves the current model to the DB"""
session.add(self)
session.commit()

def save(self):
"""Save changes to db"""
session.commit()

def delete(self):
"""Deletes the current model from the DB"""
session.delete(self)
session.commit()

def as_dto(self) -> InterestDTO:
"""Get the interest from the DB"""
dto = InterestDTO()
dto.id = self.id
dto.name = self.name

return dto

@staticmethod
def get_all_interests():
"""Get all interests"""
query = session.query(Interest).all()
interest_list_dto = InterestsListDTO()
interest_list_dto.interests = [interest.as_dto() for interest in query]

return interest_list_dto
Loading
Loading