diff --git a/Clientes/views.py b/Clientes/views.py index 1c632e9..389820d 100644 --- a/Clientes/views.py +++ b/Clientes/views.py @@ -42,20 +42,15 @@ def read_env_file(): #env_file = os.listdir(settings.BASE_DIR) storage = FileSystemStorage(location=settings.BASE_DIR) env_file= os.path.join(settings.BASE_DIR,'.env') - - lista =['asds', 'asdasd','sss','com' ,'pol.com'] if storage.exists(env_file): with open(env_file, 'r') as file: data = file.read() data =re.findall('"([^"]*)"',data) - - for x in lista: data.append(x) res= ' '.join(data) - final = '"'+res+'"' - + final = '"'+res+'"' with open(env_file, 'w') as newFile: newFile.write("export hosts="+final) except: diff --git a/Sistemas/customStorage.py b/Sistemas/customStorage.py new file mode 100644 index 0000000..d25aa8d --- /dev/null +++ b/Sistemas/customStorage.py @@ -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) \ No newline at end of file diff --git a/Sistemas/urls.py b/Sistemas/urls.py index 86f891c..402ae96 100644 --- a/Sistemas/urls.py +++ b/Sistemas/urls.py @@ -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"), ] \ No newline at end of file diff --git a/Sistemas/views.py b/Sistemas/views.py index e171bd9..8593444 100644 --- a/Sistemas/views.py +++ b/Sistemas/views.py @@ -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(".")] diff --git a/Templates/Sistemas/manageSystem/uploadZip.html b/Templates/Sistemas/manageSystem/uploadZip.html new file mode 100644 index 0000000..659cd28 --- /dev/null +++ b/Templates/Sistemas/manageSystem/uploadZip.html @@ -0,0 +1,51 @@ +{% extends 'base.html' %} + +{% block title %} +| Sistemas +{% endblock title %} + + +{% block content %} +
+ + +
+ +{% endblock content %} + + + +{% block scripts %} + + +{% endblock scripts %} + \ No newline at end of file diff --git a/Templates/sidebar.html b/Templates/sidebar.html index 811263f..81bf89a 100644 --- a/Templates/sidebar.html +++ b/Templates/sidebar.html @@ -69,7 +69,7 @@ {% endif %} - Something else here + Actualizar CFDI Version