|
1 | 1 | import os |
| 2 | +from datetime import datetime |
2 | 3 |
|
3 | 4 | from django.http import JsonResponse |
4 | 5 | from django.utils.decorators import method_decorator |
5 | 6 | from django.views import View |
6 | 7 | from django.views.decorators.csrf import csrf_exempt |
7 | 8 |
|
8 | | -from .config import (IMAGE_NAME, IMAGE_NAME_ORIGINAL, IMAGE_NAME_POSTFIX, |
9 | | - IMAGE_UPLOAD_PATH) |
| 9 | +from .config import (IMAGE_NAME, IMAGE_NAME_ORIGINAL, IMAGE_UPLOAD_PATH, |
| 10 | + IMAGE_UPLOAD_PATH_DATE) |
10 | 11 | from .utils import storage |
11 | 12 |
|
12 | 13 |
|
@@ -34,20 +35,23 @@ def post(self, request): |
34 | 35 | {'success': 0, 'message': 'You can only upload images.'} |
35 | 36 | ) |
36 | 37 |
|
37 | | - # filesize = len(file['content']) |
38 | | - # filetype = file['content-type'] |
| 38 | + # filesize = len(the_file['content']) |
| 39 | + # filetype = the_file['content-type'] |
39 | 40 |
|
40 | 41 | filename, extension = os.path.splitext(the_file.name) |
41 | 42 |
|
42 | | - if IMAGE_NAME_ORIGINAL: |
43 | | - filename = filename + IMAGE_NAME_POSTFIX |
44 | | - else: |
45 | | - filename = IMAGE_NAME |
| 43 | + if IMAGE_NAME_ORIGINAL is False: |
| 44 | + filename = IMAGE_NAME(filename=filename, file=the_file) |
46 | 45 |
|
47 | 46 | filename += extension |
48 | 47 |
|
| 48 | + upload_path = IMAGE_UPLOAD_PATH |
| 49 | + |
| 50 | + if IMAGE_UPLOAD_PATH_DATE: |
| 51 | + upload_path += datetime.now().strftime(IMAGE_UPLOAD_PATH_DATE) |
| 52 | + |
49 | 53 | path = storage.save( |
50 | | - os.path.join(IMAGE_UPLOAD_PATH, filename), the_file |
| 54 | + os.path.join(upload_path, filename), the_file |
51 | 55 | ) |
52 | 56 | link = storage.url(path) |
53 | 57 |
|
|
0 commit comments