customStorage manager in Sistemas app and zip views to upload the zip
This commit is contained in:
18
Sistemas/customStorage.py
Normal file
18
Sistemas/customStorage.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
import os
|
||||
class CustomStorage(FileSystemStorage):
|
||||
def __init__(self, location):
|
||||
self.location=location
|
||||
super(CustomStorage,self).__init__(location=location, base_url=None)
|
||||
|
||||
def save(self, name, content):
|
||||
if self.exists(name):
|
||||
self.delete(name)
|
||||
return super()._save(name,content)
|
||||
|
||||
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
download_system_dir = os.path.join(BASE_DIR,'downloadSystems')
|
||||
custom_stoarge = CustomStorage(location=download_system_dir)
|
||||
@@ -14,10 +14,11 @@ from .views import (
|
||||
AuthenticateDeviceView,
|
||||
LogoutView,
|
||||
CheckVersionView,
|
||||
UploadZipVersionView,
|
||||
|
||||
#function
|
||||
download_version_FromServer,
|
||||
|
||||
uploadZipViewHTML
|
||||
)
|
||||
urlpatterns = [
|
||||
path('',SistemasXCliente_ListView.as_view(),name='lista_sistmas'),
|
||||
@@ -28,4 +29,6 @@ urlpatterns = [
|
||||
path('usuariosConectados/', UsersConnectedList.as_view(), name='lista_usuarios'),
|
||||
path('checkVersion/', CheckVersionView.as_view(), name='checkVersion'),
|
||||
path('download_version_FromServer/',download_version_FromServer, name='downloadVersionFromServer'),
|
||||
path('uploadZip/', UploadZipVersionView.as_view(), name='uploadZip'),
|
||||
path('uploadZip2/', uploadZipViewHTML, name="uploadZipViewHTML"),
|
||||
]
|
||||
@@ -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(".")]
|
||||
|
||||
Reference in New Issue
Block a user