Skip to content

Commit

Permalink
Limit IMGZIP tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeshingles committed Sep 3, 2024
1 parent 0843568 commit 253847b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions atlasserver/forcephot/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class TaskPagination(CursorPagination):
ordering = ["id"]
template = "rest_framework/pagination/older_and_newer.html"
page_size_query_param = "pagesize"
querysetcount = None
pagefirsttaskposition = None # with ordered tasks, the position of the first page time
querysetcount: int | None = None
pagefirsttaskposition: int | None = None # with ordered tasks, the position of the first page time
previous_is_top = False

def paginate_queryset(self, queryset, request, view=None):
Expand Down
1 change: 1 addition & 0 deletions atlasserver/forcephot/throttles.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self):

def allow_request(self, request, view):
if request.method == "GET":
print(request)
return True

# We can only determine the scope once we're called by the view.
Expand Down
14 changes: 9 additions & 5 deletions atlasserver/forcephot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import PermissionDenied
from django.core.exceptions import ValidationError

# from django.contrib.auth.models import User
from django.db import transaction
from django.db.models import Count
from django.db.models import Max
Expand Down Expand Up @@ -52,12 +54,11 @@
from atlasserver.forcephot.models import Task
from atlasserver.forcephot.serializers import ForcePhotTaskSerializer

# from django.contrib.auth.models import User
# from django.core.exceptions import ValidationError
# from django.http import HttpResponse
# from django.http.response import HttpResponseRedirect
# from django.shortcuts import get_object_or_404
# from rest_framework.utils.urls import remove_query_param
maximgziptasks = 5


def calculate_queue_positions() -> None:
Expand Down Expand Up @@ -212,12 +213,10 @@ def create(self, request, *args, **kwargs) -> Response:
def perform_create(self, serializer) -> None:
"""Create new task(s)."""
if self.request.user and self.request.user.is_authenticated:
maximgziptasks = 10
usertaskcount = Task.objects.filter(user_id=self.request.user.pk, request_type="IMGZIP").count()
if usertaskcount > maximgziptasks:
msg = f"You have too many IMGZIP tasks ({usertaskcount} > {maximgziptasks}). Issue delete requests to remove some."
raise ValidationError(msg)
# serializer.save(user=self.request.user)

extra_fields: dict[str, Any] = {
"user": self.request.user,
Expand Down Expand Up @@ -344,7 +343,6 @@ class RequestImages(APIView):

def get(self, request, pk):
if not request.user.is_authenticated:
print(request.user)
raise PermissionDenied

try:
Expand All @@ -358,6 +356,12 @@ def get(self, request, pk):

redirurl = reverse("task-list")

if self.request.user and self.request.user.is_authenticated:
usertaskcount = Task.objects.filter(user_id=self.request.user.pk, request_type="IMGZIP").count()
if usertaskcount > maximgziptasks:
msg = f"You have too many IMGZIP tasks ({usertaskcount} > {maximgziptasks}). Delete some before making new requests."
return JsonResponse({"error": msg}, status=429)

if not parent_task.error_msg and parent_task.finishtimestamp:
data = model_to_dict(parent_task, exclude=["id"])
data["parent_task_id"] = parent_task.id
Expand Down
Loading

0 comments on commit 253847b

Please sign in to comment.