-
Notifications
You must be signed in to change notification settings - Fork 214
feat: Add user report feature for code of conduct violations #1211
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
Open
PcNerd9
wants to merge
5
commits into
hngprojects:dev
Choose a base branch
from
PcNerd9:report_feature
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64b3216
feat: Add user report feature for code of conduct violations
PcNerd9 b34f090
Merge branch 'dev' into report_feature
PcNerd9 719953c
Merge branch 'dev' into report_feature
PcNerd9 0ea1f46
remove ./api/v1/service/.invite.py.swp file
PcNerd9 a4a1dbf
Merge branch 'hngprojects:dev' into report_feature
PcNerd9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sqlalchemy import Column, String, Text, ForeignKey, Enum as SqlAlchemyEnum | ||
from api.v1.models.base_model import BaseTableModel | ||
from enum import Enum | ||
|
||
|
||
class ReportStatusEnum(Enum): | ||
resolved = "resolved" | ||
pending = "pending" | ||
|
||
|
||
class Report(BaseTableModel): | ||
|
||
__tablename__ = "reports" | ||
|
||
reported_by = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False) | ||
reported_user = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False) | ||
reason = Column(String, nullable=False) | ||
status = Column(SqlAlchemyEnum(ReportStatusEnum), default=ReportStatusEnum.pending) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from api.v1.models.report import Report | ||
from api.v1.schemas.report import ReportCreateSchema, ReportResponseSchema | ||
from fastapi import Depends, APIRouter, HTTPException, status, Request | ||
from sqlalchemy.orm import Session | ||
from api.db.database import get_db | ||
from api.utils.logger import logger | ||
from api.utils.success_response import success_response | ||
from api.v1.models.user import User | ||
from api.v1.services.user import user_service | ||
from api.v1.services.report import report_service | ||
from fastapi.encoders import jsonable_encoder | ||
|
||
|
||
|
||
report = APIRouter(prefix="/reports", tags=["Reports"]) | ||
|
||
@report.post("", response_model=success_response, status_code=status.HTTP_201_CREATED) | ||
def create_report(report_request: ReportCreateSchema, reported_by: User = Depends(user_service.get_current_user), db: Session = Depends(get_db)): | ||
|
||
reported_user = user_service.get_user_by_id(db, report_request.reported_user) | ||
|
||
|
||
if (reported_by.id == reported_user.id): | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User cannot report itself") | ||
|
||
reason = report_request.reason | ||
|
||
if (len(reason) == 0): | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="reason user been reported cannot be empty") | ||
|
||
report = report_service.create(db, report_request, reported_by.id) | ||
|
||
return success_response( | ||
status_code=status.HTTP_201_CREATED, | ||
message="report successfully created", | ||
data=jsonable_encoder(report) | ||
) | ||
|
||
@report.get("", response_model=success_response, status_code=status.HTTP_200_OK) | ||
def get_all_report(admin_user: User = Depends(user_service.get_current_super_admin), db: Session = Depends(get_db)): | ||
|
||
reports = report_service.fetch_all(db) | ||
|
||
if not reports: | ||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No report Found") | ||
|
||
return success_response( | ||
status_code=status.HTTP_200_OK, | ||
message="Report retrieced successfully", | ||
data=jsonable_encoder(reports) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pydantic import BaseModel | ||
from datetime import datetime | ||
|
||
|
||
|
||
class ReportCreateSchema(BaseModel): | ||
reported_user: str | ||
reason: str | ||
|
||
|
||
class ReportResponseSchema(BaseModel): | ||
reported_by: str | ||
reported_user: str | ||
reason: str | ||
status: str | ||
created_at: datetime | ||
updated_at: datetime |
joboy-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Any, Optional, List | ||
from sqlalchemy.orm import Session | ||
from api.core.base.services import Service | ||
from api.v1.models.report import Report | ||
from api.v1.schemas.report import ReportCreateSchema, ReportResponseSchema | ||
from api.utils.db_validators import check_model_existence | ||
from sqlalchemy import distinct | ||
from fastapi import HTTPException | ||
|
||
|
||
class ReportService(Service): | ||
"""Report Services""" | ||
|
||
def create(self, db: Session, schema: ReportCreateSchema, user_id: str): | ||
'''Create a new Region''' | ||
new_report = Report(**schema.model_dump(), reported_by=user_id) | ||
db.add(new_report) | ||
db.commit() | ||
db.refresh(new_report) | ||
|
||
return new_report | ||
|
||
|
||
def fetch_all(self, db: Session, **query_params: Optional[Any]): | ||
'''Fetch all Region with option to search using query parameters''' | ||
|
||
query = db.query(Report) | ||
|
||
# Enable filter by query parameter | ||
if query_params: | ||
for column, value in query_params.items(): | ||
if hasattr(Report, column) and value: | ||
query = query.filter(getattr(Report, column).ilike(f'%{value}%')) | ||
|
||
return query.all() | ||
|
||
|
||
def fetch(self, db: Session, report_id: str): | ||
'''Fetches a Region by id''' | ||
|
||
report = check_model_existence(db, Report, report_id) | ||
return report | ||
|
||
|
||
def update(self, db: Session, report_id: str): | ||
'''Updates a Region''' | ||
|
||
region = self.fetch(db=db, report_id=report_id) | ||
|
||
# Update the fields with the provided schema data | ||
|
||
db.commit() | ||
db.refresh(region) | ||
return region | ||
|
||
|
||
def delete(self, db: Session, report_id: str): | ||
'''Deletes a region service''' | ||
|
||
report = self.fetch(db=db, report_id=report_id) | ||
db.delete(report) | ||
db.commit() | ||
|
||
|
||
|
||
report_service = ReportService() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.