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

Feat/dev add list user files view #72

Merged
merged 5 commits into from
Jun 23, 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
4 changes: 4 additions & 0 deletions makedoc/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ class DataDocSerializer(serializers.Serializer[Any]):
factory_id = serializers.IntegerField()
destination = serializers.CharField()
delivery_cost = serializers.IntegerField(min_value=0)


class FileNameSerializer(serializers.Serializer[Any]):
file_name = serializers.CharField(required=False, allow_blank=True)
5 changes: 3 additions & 2 deletions makedoc/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
get_formatted_date_agreement,
get_formatted_date_shipment,
)

from .data_service import DataService


Expand Down Expand Up @@ -194,8 +193,10 @@ def add_workbook_to_archive_xlsx(self, wb, user, client):
tempdir = os.path.join("makedoc", "tempdoc", str(user.id))
os.makedirs(tempdir, exist_ok=True)

client_name = client.client_name.replace(' ', '_')
date_today = datetime.today().strftime('%d.%m.%Y_%H:%M:%S')
self.archive_name = (
f"{client.client_name} {datetime.today().strftime('%d.%m.%Y %H:%M:%S')}.zip"
f"{client_name}_{date_today}.zip"
)
archive_path = f"{tempdir}/{self.archive_name}"

Expand Down
2 changes: 1 addition & 1 deletion makedoc/tests/test_views_makedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ def test__create_docs_view__authorized_user_get_no_data(authorized_client):
@pytest.mark.django_db
def test__download_doc_view__returns_404_when_no_file_exists(authorized_client):
url = reverse("downloadfile")
response = authorized_client.get(url)
response = authorized_client.post(url, {}, format="json")
assert response.status_code == 404
assert response.json() == {"error": "No file found"}
8 changes: 4 additions & 4 deletions makedoc/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.urls import path

from .views import DataDocView, CreateDocsView, DownloadDocView
from .views import DataDocAPIView, CreateDocsAPIView, DownloadDocAPIView

urlpatterns = [
path("filemake/", CreateDocsView.as_view(), name="file"),
path("downloadfile/", DownloadDocView.as_view(), name="downloadfile"),
path("data/", DataDocView.as_view(), name="data"),
path("filemake/", CreateDocsAPIView.as_view(), name="file"),
path("downloadfile/", DownloadDocAPIView.as_view(), name="downloadfile"),
path("data/", DataDocAPIView.as_view(), name="data"),
]
42 changes: 31 additions & 11 deletions makedoc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
from rest_framework.views import APIView

from makedoc.services import Documents
from .serializers import DataDocSerializer, DocumentsSimpleSerializer
from .serializers import DataDocSerializer, DocumentsSimpleSerializer, FileNameSerializer


class CreateDocsView(APIView):
class CreateDocsAPIView(APIView):
"""
Responsible for creating documents based on data from the cache.
"""
serializer_class = DocumentsSimpleSerializer
permission_classes = [IsAuthenticated]

Expand Down Expand Up @@ -49,15 +52,32 @@ def get(self, request, *args, **kwargs):
return Response({"message": "Documents saved"}, status=status.HTTP_200_OK)


class DownloadDocView(APIView):
# Загрузка документа
class DownloadDocAPIView(APIView):
"""
To download a file:

- No parameters: get the last created file.

- With the parameter {"file_name": "name your file"} - load a specific file from the list.
"""
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
cache_key = f"last_created_file_user:{request.user.id}"
file_name = cache.get(cache_key)
def post(self, request, *args, **kwargs):
serializer = FileNameSerializer(data=request.data)
if not serializer.is_valid():
return Response({"error": "Incorrect file name"}, status=status.HTTP_404_NOT_FOUND)

file_name = serializer.validated_data.get("file_name")

if not file_name:
return Response({"error": "No file found"}, status=status.HTTP_404_NOT_FOUND)
cache_key = f"last_created_file_user:{request.user.id}"
file_name = cache.get(cache_key)

if not file_name:
return Response({"error": "No file found"}, status=status.HTTP_404_NOT_FOUND)

cache.delete(cache_key)

file_path = os.path.join("makedoc", "tempdoc", str(request.user.id), file_name)

Expand All @@ -66,16 +86,16 @@ def get(self, request, *args, **kwargs):

response = FileResponse(open(file_path, "rb"))
response["Content-Disposition"] = f'attachment; filename="{os.path.basename(file_name)}"'

cache.delete(cache_key)

response["message"] = "File successfully downloaded"
response.status_code = status.HTTP_200_OK

return response


class DataDocView(generics.GenericAPIView[Any]):
class DataDocAPIView(generics.GenericAPIView[Any]):
"""
Responsible for receiving data, validating it and storing it in cache.
"""
serializer_class = DataDocSerializer
permission_classes = (IsAuthenticated,)

Expand Down
2 changes: 2 additions & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
UserCreateView,
UserUpdatePasswordView,
UserUpdateView,
ListUserFilesAPIView
)

urlpatterns = [
Expand All @@ -17,6 +18,7 @@
path("logout/", LogoutView.as_view(), name="logout"),
path("edit/", UserUpdateView.as_view(), name="edit"),
path("edit_password/", UserUpdatePasswordView.as_view(), name="edit_password"),
path("listuserfiles/", ListUserFilesAPIView.as_view(), name="list_user_files"),
path("departments/", DepartmentListView.as_view(), name="departments"),
path("positions/", PositionListView.as_view(), name="positions"),
path("registration/", UserCreateView.as_view(), name="registration"),
Expand Down
28 changes: 28 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os.path

from django.dispatch import receiver
from django_rest_passwordreset.signals import reset_password_token_created
from rest_framework import generics, status
Expand Down Expand Up @@ -70,6 +72,32 @@ class UserUpdatePasswordView(UserRelatedView):
serializer_class = UserUpdatePasswordSerializer


class ListUserFilesAPIView(APIView):
"""
Responsible for displaying a list of documents of an authorized user.
"""
permission_classes = (IsAuthenticated,)

def get(self, request, *args, **kwargs):
user_folder = os.path.join("makedoc", "tempdoc", str(request.user.id))

if not os.path.exists(user_folder):
return Response({"error": "User folder not found"}, status=status.HTTP_404_NOT_FOUND)

try:
files = os.listdir(user_folder)
except OSError as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

files_list = []
for file_name in files:
file_path = os.path.join(user_folder, file_name)
if os.path.isfile(file_path):
files_list.append(file_name)

return Response({"files": files_list}, status=status.HTTP_200_OK)


# Сброс пароля
@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
Expand Down
Loading