extract validators and decorator to utils
authorMaciej Tronowski <mtro@man.poznan.pl>
Wed, 22 Apr 2015 11:06:33 +0000 (13:06 +0200)
committerMaciej Tronowski <mtro@man.poznan.pl>
Wed, 22 Apr 2015 11:06:33 +0000 (13:06 +0200)
filex/forms.py
filex/models.py
filex/uploadhandler.py
filex/utils.py [new file with mode: 0644]
filex/validators.py [deleted file]

index 4151a16..4f7c8dd 100644 (file)
@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError
 from django.utils.http import urlquote
 
 from filex.models import Favorite
-from filex.validators import host_validator, path_validator, name_validator
+from filex.utils import host_validator, path_validator, name_validator
 
 
 def clean_path(path):
index 235a3f4..82e73b4 100644 (file)
@@ -2,7 +2,7 @@
 from django.conf import settings
 from django.db import models
 
-from filex.validators import host_validator, path_validator
+from filex.utils import host_validator, path_validator
 
 
 class Favorite(models.Model):
index ee34a91..7e57a36 100644 (file)
@@ -1,13 +1,10 @@
-from functools import wraps
 import os
 
 from django.core.files.uploadedfile import UploadedFile
 from django.core.files.uploadhandler import FileUploadHandler, StopUpload, StopFutureHandlers
-from django.http import JsonResponse
-from django.views.decorators.csrf import csrf_exempt, csrf_protect
 
 from filex.forms import HostPathForm
-from filex.ftp import FTPOperation, FTPError
+from filex.ftp import FTPOperation
 
 
 class FtpUploadHandler(FileUploadHandler):
@@ -46,22 +43,3 @@ class FtpUploadHandler(FileUploadHandler):
 
         return UploadedFile(name=self.file_name, size=file_size, charset=self.charset,
                             content_type=self.content_type, content_type_extra=self.content_type_extra)
-
-
-def with_ftp_upload_handler(view_func):
-    @wraps(view_func)
-    def wrapped_view(request, *args, **kwargs):
-        request.upload_handlers = [FtpUploadHandler(request)]
-
-        try:
-            return csrf_protect(view_func)(request, *args, **kwargs)
-        except FTPError as e:
-            status = 400
-            if 'No such file or directory' in e.message:
-                status = 404
-            elif 'Permission denied' in e.message:
-                status = 403
-
-            return JsonResponse({'error': e.message}, status=status)
-
-    return csrf_exempt(wrapped_view)
diff --git a/filex/utils.py b/filex/utils.py
new file mode 100644 (file)
index 0000000..9a96949
--- /dev/null
@@ -0,0 +1,34 @@
+from functools import wraps
+
+from django.core.validators import RegexValidator
+from django.http import JsonResponse
+from django.views.decorators.csrf import csrf_protect, csrf_exempt
+
+from filex.ftp import FTPError
+from filex.uploadhandler import FtpUploadHandler
+
+
+msg = u'Invalid value'
+host_validator = RegexValidator(r'^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+'
+                                r'(?:[a-zA-Z]{2,6}\.?|[a-zA-Z0-9-]{2,}(?<!-)\.?))(?::\d+)?$', msg)
+path_validator = RegexValidator(r'^~?(?:/[^/\0]*)*$', msg)
+name_validator = RegexValidator(r'^[^/\0]+$', msg)
+
+
+def with_ftp_upload_handler(view_func):
+    @wraps(view_func)
+    def wrapped_view(request, *args, **kwargs):
+        request.upload_handlers = [FtpUploadHandler(request)]
+
+        try:
+            return csrf_protect(view_func)(request, *args, **kwargs)
+        except FTPError as e:
+            status = 400
+            if 'No such file or directory' in e.message:
+                status = 404
+            elif 'Permission denied' in e.message:
+                status = 403
+
+            return JsonResponse({'error': e.message}, status=status)
+
+    return csrf_exempt(wrapped_view)
diff --git a/filex/validators.py b/filex/validators.py
deleted file mode 100644 (file)
index b1a621b..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-from django.core.validators import RegexValidator
-
-msg = u'Invalid value'
-host_validator = RegexValidator(r'^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+'
-                                r'(?:[a-zA-Z]{2,6}\.?|[a-zA-Z0-9-]{2,}(?<!-)\.?))(?::\d+)?$', msg)
-path_validator = RegexValidator(r'^~?(?:/[^/\0]*)*$', msg)
-name_validator = RegexValidator(r'^[^/\0]+$', msg)