Skip to content

Commit

Permalink
Refactor TrainingWorkspaceView and TrainingWorkspaceDownloadView to e…
Browse files Browse the repository at this point in the history
…nable authentication and improve caching
  • Loading branch information
kshitijrajsharma committed Dec 11, 2024
1 parent 4dc99df commit 3e99467
Showing 1 changed file with 10 additions and 106 deletions.
116 changes: 10 additions & 106 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,52 +829,9 @@ def get(self, request, feedback_aoi_id: int):
return HttpResponse(gpx_xml, content_type="application/xml")


# class TrainingWorkspaceView(APIView):
# @method_decorator(cache_page(60 * 15))
# # @method_decorator(vary_on_headers("access-token"))
# def get(self, request, lookup_dir):
# """
# List the status of the training workspace.

# ### Returns:
# - **Size**: The total size of the workspace in bytes.
# - **dir/file**: The current dir/file on the lookup_dir.

# ### Workspace Structure:
# By default, the training workspace is organized as follows:
# - Training files are stored in the directory: `dataset{dataset_id}/output/training_{training}`
# """

# # {workspace_dir:{file_name:{size:20,type:file},dir_name:{size:20,len:4,type:dir}}}
# base_dir = settings.TRAINING_WORKSPACE
# if lookup_dir:
# base_dir = os.path.join(base_dir, lookup_dir)
# if not os.path.exists(base_dir):
# return Response({"Errr:File/Dir not Found"}, status=404)
# data = {"file": {}, "dir": {}}
# if os.path.isdir(base_dir):
# for entry in os.scandir(base_dir):
# if entry.is_file():
# data["file"][entry.name] = {
# "size": entry.stat().st_size,
# }
# elif entry.is_dir():
# subdir_size = get_dir_size(entry.path)
# data["dir"][entry.name] = {
# "len": sum(1 for _ in os.scandir(entry.path)),
# "size": subdir_size,
# }
# elif os.path.isfile(base_dir):
# data["file"][os.path.basename(base_dir)] = {
# "size": os.path.getsize(base_dir)
# }

# return Response(data, status=status.HTTP_201_CREATED)


class TrainingWorkspaceView(APIView):
@method_decorator(cache_page(60 * 15))
# # @method_decorator(vary_on_headers("access-token"))
@method_decorator(vary_on_headers("access-token"))
def get(self, request, lookup_dir):
bucket_name = settings.BUCKET_NAME
encoded_file_path = quote(lookup_dir.strip("/"))
Expand All @@ -888,17 +845,17 @@ def get(self, request, lookup_dir):


class TrainingWorkspaceDownloadView(APIView):
# authentication_classes = [OsmAuthentication]
# permission_classes = [IsOsmAuthenticated]
authentication_classes = [OsmAuthentication]
permission_classes = [IsOsmAuthenticated]

# def dispatch(self, request, *args, **kwargs):
# lookup_dir = kwargs.get("lookup_dir")
# if lookup_dir.endswith("training_accuracy.png"):
# # bypass
# self.authentication_classes = []
# self.permission_classes = []
def dispatch(self, request, *args, **kwargs):
lookup_dir = kwargs.get("lookup_dir")
if lookup_dir.endswith("training_accuracy.png"):
# bypass
self.authentication_classes = []
self.permission_classes = []

# return super().dispatch(request, *args, **kwargs)
return super().dispatch(request, *args, **kwargs)

def get(self, request, lookup_dir):
s3_key = os.path.join(settings.PARENT_BUCKET_FOLDER, lookup_dir)
Expand All @@ -911,59 +868,6 @@ def get(self, request, lookup_dir):
return HttpResponseRedirect(presigned_url)


# class TrainingWorkspaceDownloadView(APIView):
# authentication_classes = [OsmAuthentication]
# permission_classes = [IsOsmAuthenticated]

# def dispatch(self, request, *args, **kwargs):
# lookup_dir = kwargs.get("lookup_dir")
# if lookup_dir.endswith("training_accuracy.png"):
# # bypass
# self.authentication_classes = []
# self.permission_classes = []

# return super().dispatch(request, *args, **kwargs)

# def get(self, request, lookup_dir):
# base_dir = os.path.join(settings.TRAINING_WORKSPACE, lookup_dir)
# if not os.path.exists(base_dir):
# return Response({"Errr: File/Dir not found"}, status=404)
# size = (
# get_dir_size(base_dir)
# if os.path.isdir(base_dir)
# else os.path.getsize(base_dir)
# ) / (1024**2)
# if (
# size > settings.TRAINING_WORKSPACE_DOWNLOAD_LIMIT
# ): # if file is greater than 200 mb exit
# return Response(
# {
# f"Errr: File Size {size} MB Exceed More than {settings.TRAINING_WORKSPACE_DOWNLOAD_LIMIT} MB"
# },
# status=403,
# )

# if os.path.isfile(base_dir):
# response = FileResponse(open(base_dir, "rb"))
# response["Content-Disposition"] = 'attachment; filename="{}"'.format(
# os.path.basename(base_dir)
# )
# return response
# else:
# # TODO : This will take time to zip also based on the reading/writing speed of the dir
# temp = NamedTemporaryFile()
# shutil.make_archive(temp.name, "zip", base_dir)
# # rewind the file so it can be read from the beginning
# temp.seek(0)
# response = StreamingHttpResponse(
# open(temp.name + ".zip", "rb").read(), content_type="application/zip"
# )
# response["Content-Disposition"] = 'attachment; filename="{}.zip"'.format(
# os.path.basename(base_dir)
# )
# return response


class BannerViewSet(viewsets.ModelViewSet):
queryset = Banner.objects.all()
serializer_class = BannerSerializer
Expand Down

0 comments on commit 3e99467

Please sign in to comment.