Skip to content

Commit

Permalink
#1073 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Arcuri committed Apr 9, 2024
1 parent 57ffcb9 commit be19539
Show file tree
Hide file tree
Showing 17 changed files with 2,973 additions and 149 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ repos:
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/hadialqattan/pycln
rev: v2.4.0
hooks:
- id: pycln
34 changes: 11 additions & 23 deletions orochi/api/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import datetime
from typing import Dict, List, Optional

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from ninja import ModelSchema, Schema
from ninja import Field, ModelSchema, Schema
from ninja.orm import create_schema

from orochi.website.defaults import OSEnum
from orochi.website.models import Dump, Folder, Plugin, Result

###################################################
Expand Down Expand Up @@ -124,6 +124,11 @@ class Meta:
]


class PluginInstallSchema(Schema):
plugin_url: str
operating_system: OSEnum


###################################################
# Folder
###################################################
Expand Down Expand Up @@ -192,25 +197,8 @@ class Meta:


###################################################
# Result
# Plugins [from Results]
###################################################
class PluginSmallSchema(ModelSchema):
class Meta:
model = Plugin
fields = ["name", "comment"]


class DumpSmallSchema(ModelSchema):
class Meta:
model = Dump
fields = ["index", "name"]


class ResultSmallOutSchema(ModelSchema):
plugin: PluginSmallSchema = None
dump: DumpSmallSchema = None
updated_at: datetime = None

class Meta:
model = Result
fields = ["result", "parameter", "description"]
class ResultSmallOutSchema(Schema):
name: str = Field(..., alias="plugin__name")
comment: str = Field(..., alias="plugin__comment")
52 changes: 51 additions & 1 deletion orochi/api/routers/dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

@router.get("/", auth=django_auth, response=List[DumpSchema])
def list_dumps(request, filters: Query[OperatingSytemFilters]):
"""
Summary:
Retrieve a list of dumps based on optional operating system filters.
Explanation:
Returns a list of dumps accessible to the user, filtered by the specified operating system if provided and the user's permissions.
Args:
- request: The request object.
- filters: Query object containing operating system filters.
Returns:
- List of DumpSchema objects representing the dumps that match the criteria.
"""
dumps = (
Dump.objects.all()
if request.user.is_superuser
Expand All @@ -28,14 +42,48 @@ def list_dumps(request, filters: Query[OperatingSytemFilters]):

@router.get("/{pk}", response=DumpInfoSchema, auth=django_auth)
def get_dump_info(request, pk: UUID):
"""
Summary:
Retrieve detailed information about a specific dump by its index.
Explanation:
Fetches the dump with the specified index and returns its information if the user has permission to view it; otherwise, returns a 403 Forbidden response.
Args:
- request: The request object.
- pk: The UUID index of the dump to retrieve information for.
Returns:
- DumpInfoSchema object representing the detailed information of the dump.
"""
dump = get_object_or_404(Dump, index=pk)
if dump not in get_objects_for_user(request.user, "website.can_see"):
return HttpResponse("Forbidden", status=403)
return dump


@router.get("/{idxs:pks}/plugins", response=ResultSmallOutSchema, auth=django_auth)
@router.get(
"/{idxs:pks}/plugins",
url_name="dumps_plugins",
response=List[ResultSmallOutSchema],
auth=django_auth,
)
def get_dump_plugins(request, pks: List[UUID], filters: Query[DumpFilters] = None):
"""
Summary:
Retrieve a list of plugins associated with specified dumps.
Explanation:
Fetches the plugins related to the dumps identified by the provided list of UUIDs, considering user permissions, and optionally filters the results based on DumpFilters.
Args:
- request: The request object.
- pks: List of UUIDs representing the indexes of the dumps to retrieve plugins for.
- filters: Optional Query object containing dump filters.
Returns:
- List of ResultSmallOutSchema objects representing the plugins associated with the specified dumps.
"""
dumps_ok = get_objects_for_user(request.user, "website.can_see")
dumps = [
dump.index for dump in Dump.objects.filter(index__in=pks) if dump in dumps_ok
Expand All @@ -44,6 +92,8 @@ def get_dump_plugins(request, pks: List[UUID], filters: Query[DumpFilters] = Non
Result.objects.select_related("dump", "plugin")
.filter(dump__index__in=dumps)
.order_by("plugin__name")
.distinct()
.values("plugin__name", "plugin__comment")
)
if filters and filters.result:
res = res.filter(result=filters.result)
Expand Down
52 changes: 49 additions & 3 deletions orochi/api/routers/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@

@router.get("/", auth=django_auth, response=List[FolderFullSchema])
def list_folders(request):
"""
Summary:
Retrieve a list of folders based on user permissions.
Explanation:
Returns all folders if the user is a superuser; otherwise, returns folders associated with the current user.
Args:
- request: The request object.
Returns:
- List of FolderFullSchema objects representing the folders accessible to the user.
"""
if request.user.is_superuser:
return Folder.objects.all()
return Folder.objects.filter(user=request.user)
Expand All @@ -28,6 +41,20 @@ def list_folders(request):
)
@ninja_test_required("is_not_readonly")
def create_folder(request, folder_in: FolderSchema):
"""
Summary:
Create a new folder with the provided name.
Explanation:
Attempts to create a new folder with the specified name for the current user. Returns the created folder if successful, or an error response if the folder already exists.
Args:
- request: The request object.
- folder_in: FolderSchema object containing the details of the folder to be created.
Returns:
- If successful, returns HTTP status code 201 and the created FolderFullSchema object. If the folder already exists, returns HTTP status code 400 and an ErrorsOut object with an error message.
"""
try:
folder = Folder.objects.create(name=folder_in.name, user=request.user)
folder.save()
Expand All @@ -36,8 +63,27 @@ def create_folder(request, folder_in: FolderSchema):
return 400, {"errors": "Folder already exists"}


@router.delete("/{name}", auth=django_auth, response={200: SuccessResponse})
@router.delete(
"/{str:name}", auth=django_auth, response={200: SuccessResponse, 400: ErrorsOut}
)
def delete_folder(request, name: str):
"""
Summary:
Delete a folder by name with error handling.
Explanation:
Attempts to delete the folder with the specified name belonging to the current user. Returns a success message if the deletion is successful, or an error response with details if an exception occurs during deletion.
Args:
- request: The request object.
- name: The name of the folder to delete.
Returns:
- If successful, returns HTTP status code 200 and a success message dictionary. If an exception occurs, returns HTTP status code 400 and an ErrorsOut object with the exception details.
"""
folder = get_object_or_404(Folder, name=name, user=request.user)
folder.delete()
return 200, {"message": f"Folder {name} deleted"}
try:
folder.delete()
return 200, {"message": f"Folder {name} deleted"}
except Exception as excp:
return 400, {"errors": str(excp)}

0 comments on commit be19539

Please sign in to comment.