customStorage manager in Sistemas app and zip views to upload the zip

This commit is contained in:
fjrodriguez
2023-02-10 07:39:25 -06:00
parent b73a4d50d0
commit ddc4be05a2
6 changed files with 120 additions and 11 deletions

View File

@@ -28,6 +28,49 @@ import os
import mimetypes
import traceback
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from .customStorage import CustomStorage
from django.contrib.auth.decorators import login_required
@login_required
def uploadZipViewHTML(request):
if request.method=="GET":
user = request.user
if user.is_superuser:
token= Token.objects.get(user=user)
else:
redirect('index')
context = {
'token':token.key,
}
template_name= 'Sistemas/manageSystem/uploadZip.html'
return render(request, template_name,context)
return HttpResponse('Only GET request are allowed')
class UploadZipVersionView(APIView):
"""API CLASS for upload the CFDI Version into the server"""
permissions_classes=[IsAuthenticated,]
#authentication_classes = (TokenAuthentication,) #this is by default in settings.py
def post(self,request,format=None):
try:
zip_file=request.FILES['zip_file']
except KeyError:
return Response({'error':'Zip file is required'},status=status.HTTP_400_BAD_REQUEST)
try:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
download_system_dir = os.path.join(BASE_DIR,'downloadSystems')
custom_storage = CustomStorage(location=download_system_dir)
print('path exist? ',os.path.exists(download_system_dir))
fileName = custom_storage.save(zip_file.name, ContentFile(zip_file.read()))
except Exception as ex:
BitacoraErrores.objects.create(level=2, message=str(ex), traceback=traceback.format_exc(), view='Sistemas.UploadZipVersionView')
return Response({'error':str(ex)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({'message':'File uploaded successfully'}, status=status.HTTP_201_CREATED)
def download_version_FromServer(request):
try:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -38,8 +81,7 @@ def download_version_FromServer(request):
with open(filepath, 'rb') as path:
mime_type, _ = mimetypes.guess_type(filepath)
response = HttpResponse(path.read(),content_type=mime_type)
response['Content-Disposition']= f'attachment; filename={filename}'
response['Content-Disposition']= f'attachment; filename={filename}'
return response
except Exception as ex:
@@ -165,7 +207,7 @@ class CheckVersionView(APIView):
version = request.data.get('version')
client_version = [int(x) for x in version.split(".")]
print('client_version: ',client_version)
#print('client_version: ',client_version)
try:
ver = Sistema.objects.get(nombre_sistema="CFDI")
server_version = [int(x) for x in ver.version.split(".")]