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

archivation added #67

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion makedoc/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from datetime import date
import zipfile
from datetime import date, datetime

import openpyxl
from openpyxl.styles import Alignment, Border, Font, Side
Expand All @@ -19,6 +20,7 @@ def __init__(self, validated_data):
self.rw = 0
self.service_note = 0
self.transport_sheet = 0
self.archive_list = []

def update_documents(self):
delivery_type = DataService.get_delivery_type(self.validated_data)
Expand Down Expand Up @@ -195,6 +197,7 @@ def save_workbook(self, wb, user, client):
f"{self.docname} {client.last_application_number} {client.client_name} \
{date.today().strftime('%d.%m.%Y')}.xlsx",
)
self.archive_list.append(new_file_path)
wb.save(new_file_path)

def apply_styles(self, ws):
Expand Down Expand Up @@ -373,3 +376,17 @@ def fill_contract_info_transport_sheet(self, ws, client):
)
ws.cell(row=5, column=1, value=client.client_name)
ws.cell(row=7, column=8, value=get_formatted_date_agreement())

def archive_and_remove_files(self, request):
client = DataService.get_client(self.validated_data)
user = DataService.get_user(request)

archive_name = f"makedoc/tempdoc/{user.full_name}/\
{client.client_name} {datetime.today().strftime('%d.%m.%Y %H:%M:%S')}.zip"

with zipfile.ZipFile(archive_name, "w", zipfile.ZIP_DEFLATED) as zipf:
for file_path in self.archive_list:
if os.path.isfile(file_path):
arcname = os.path.basename(file_path)
zipf.write(file_path, arcname)
os.remove(file_path)
2 changes: 2 additions & 0 deletions makedoc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def get(self, request, *args, **kwargs):
if docs.transport_sheet:
docs.form_transport_sheet(request)

docs.archive_and_remove_files(request)

return Response(
{"message": "Документы сохранены", "data": validated_data}, status=status.HTTP_200_OK
)
Expand Down
Loading