lines-num lines-num-old">
+ 'pk': self.pk,
+ 'uuid': self.uuid,
+ 'lensman_id': self.lensman_id,
+ }
+
+ data = property(_data)
+
+
class PhotosInfo(CreateUpdateMixin):
lensman_id = models.CharField(_(u'lensman_id'), max_length=255, blank=True, null=True, help_text=u'摄影师唯一标识', db_index=True)
session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识', db_index=True)
photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'照片唯一标识', db_index=True, unique=True)
+ photo_name = models.CharField(_(u'photo_name'), max_length=255, blank=True, null=True, help_text=u'照片存放名称')
photo_path = models.CharField(_(u'photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径')
class Meta:
@@ -25,7 +47,8 @@ class PhotosInfo(CreateUpdateMixin):
@property
def photo_url(self):
- return u'{0}/media/{1}'.format(settings.DOMAIN, self.photo_path) if self.photo_path else ''
+ # return u'{0}/media/{1}'.format(settings.DOMAIN, self.photo_path) if self.photo_path else ''
+ return u'{0}/p/{1}'.format(settings.DOMAIN, self.photo_name) if self.photo_name else ''
def _data(self):
return {
@@ -1,17 +1,52 @@ |
||
| 1 | 1 |
# -*- coding: utf-8 -*- |
| 2 | 2 |
|
| 3 | 3 |
from django.core.files.storage import default_storage |
| 4 |
+from django.db import transaction |
|
| 4 | 5 |
from django.http import JsonResponse |
| 5 | 6 |
|
| 6 | 7 |
from rest_framework import viewsets |
| 7 | 8 |
|
| 8 | 9 |
from account.models import LensmanInfo |
| 9 |
-from photo.models import PhotosInfo |
|
| 10 |
+from photo.models import UUIDInfo, PhotosInfo |
|
| 10 | 11 |
from photo.serializers import PhotosInfoSerializer |
| 11 | 12 |
|
| 13 |
+from utils.uuid_utils import curtailUUID |
|
| 14 |
+ |
|
| 12 | 15 |
import os |
| 13 | 16 |
|
| 14 | 17 |
|
| 18 |
+def uuid_init(request): |
|
| 19 |
+ num = int(request.GET.get('num', 1000))
|
|
| 20 |
+ |
|
| 21 |
+ for i in xrange(num): |
|
| 22 |
+ UUIDInfo.objects.create(uuid=curtailUUID()) |
|
| 23 |
+ |
|
| 24 |
+ return JsonResponse({
|
|
| 25 |
+ 'status': 200, |
|
| 26 |
+ 'message': u'UUID 更新成功', |
|
| 27 |
+ 'data': '', |
|
| 28 |
+ }) |
|
| 29 |
+ |
|
| 30 |
+ |
|
| 31 |
+# curl -X POST -F lensman_id=123 -F num=100 http://xfoto.com.cn/api/uuid |
|
| 32 |
+@transaction.atomic |
|
| 33 |
+def uuid(request): |
|
| 34 |
+ lensman_id = request.POST.get('lensman_id', '')
|
|
| 35 |
+ num = int(request.POST.get('num', 100))
|
|
| 36 |
+ |
|
| 37 |
+ uuids = UUIDInfo.objects.select_for_update().filter(status=True)[:num] |
|
| 38 |
+ for uuid in uuids: |
|
| 39 |
+ uuid.lensman_id = lensman_id |
|
| 40 |
+ uuid.status = False |
|
| 41 |
+ uuid.save() |
|
| 42 |
+ |
|
| 43 |
+ return JsonResponse({
|
|
| 44 |
+ 'status': 200, |
|
| 45 |
+ 'message': u'获取唯一标识成功', |
|
| 46 |
+ 'data': [uuid.uuid for uuid in uuids], |
|
| 47 |
+ }) |
|
| 48 |
+ |
|
| 49 |
+ |
|
| 15 | 50 |
# [How to do a PUT request with curl?](http://stackoverflow.com/questions/13782198/how-to-do-a-put-request-with-curl) |
| 16 | 51 |
# Unfortunately, the -T is no substitute for -X PUT if you want to specify parameters with -d or -F. |
| 17 | 52 |
# -T sends the content of a file via PUT. To achieve the GET after a redirect, add the parameter --location |
@@ -23,17 +58,16 @@ import os |
||
| 23 | 58 |
# name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, |
| 24 | 59 |
# while the < makes a text field and just get the contents for that text field from a file. |
| 25 | 60 |
# |
| 26 |
-# curl -X POST -F lensman_id=123 -F session_id=456 -F photo_id=789 -F photo=@7056288a9ddf2db294cf50a943920989.jpg;filename=789 http://xfoto.com.cn/api/photos/upload |
|
| 61 |
+# curl -X POST -F lensman_id=123 -F session_id=456 -F photo=@7056288a9ddf2db294cf50a943920989.jpg;filename=789 http://xfoto.com.cn/api/photos/upload |
|
| 27 | 62 |
def upload_photo(request): |
| 28 | 63 |
lensman_id = request.POST.get('lensman_id', '')
|
| 29 | 64 |
session_id = request.POST.get('session_id', '')
|
| 30 |
- photo_id = request.POST.get('photo_id', '')
|
|
| 31 | 65 |
|
| 32 | 66 |
photo = request.FILES.get('photo', '')
|
| 33 | 67 |
|
| 34 |
- if not (lensman_id and session_id and photo_id and photo): |
|
| 68 |
+ if not (lensman_id and session_id and photo): |
|
| 35 | 69 |
return JsonResponse({
|
| 36 |
- 'status': 400, |
|
| 70 |
+ 'status': 4010, |
|
| 37 | 71 |
'message': u'参数错误', |
| 38 | 72 |
}) |
| 39 | 73 |
|
@@ -41,12 +75,16 @@ def upload_photo(request): |
||
| 41 | 75 |
LensmanInfo.objects.get(lensman_id=lensman_id) |
| 42 | 76 |
except LensmanInfo.DoesNotExist: |
| 43 | 77 |
return JsonResponse({
|
| 44 |
- 'status': 400, |
|
| 45 |
- 'message': u'参数错误', |
|
| 78 |
+ 'status': 4011, |
|
| 79 |
+ 'message': u'摄影师不存在', |
|
| 46 | 80 |
}) |
| 47 | 81 |
|
| 82 |
+ photo_id = curtailUUID() |
|
| 83 |
+ |
|
| 48 | 84 |
_, extension = os.path.splitext(photo.name) |
| 49 |
- photo_path = 'photo/{0}/{1}/{2}{3}'.format(lensman_id, session_id, photo_id, extension)
|
|
| 85 |
+ # photo_path = 'photo/{0}/{1}/{2}{3}'.format(lensman_id, session_id, photo_id, extension)
|
|
| 86 |
+ photo_name = '{0}{1}'.format(photo_id, extension)
|
|
| 87 |
+ photo_path = 'photo/{0}'.format(photo_name)
|
|
| 50 | 88 |
|
| 51 | 89 |
if default_storage.exists(photo_path): |
| 52 | 90 |
default_storage.delete(photo_path) |
@@ -56,6 +94,7 @@ def upload_photo(request): |
||
| 56 | 94 |
lensman_id=lensman_id, |
| 57 | 95 |
session_id=session_id, |
| 58 | 96 |
photo_id=photo_id, |
| 97 |
+ photo_name=photo_name, |
|
| 59 | 98 |
photo_path=photo_path |
| 60 | 99 |
) |
| 61 | 100 |
|
@@ -9,4 +9,5 @@ ipython==4.0.0 |
||
| 9 | 9 |
pep8==1.6.2 |
| 10 | 10 |
pillow==2.9.0 |
| 11 | 11 |
pytz==2015.7 |
| 12 |
+shortuuid==0.4.2 |
|
| 12 | 13 |
uWSGI==2.0.11.1 |
@@ -0,0 +1,16 @@ |
||
| 1 |
+# -*- coding: utf-8 -*- |
|
| 2 |
+ |
|
| 3 |
+from photo.models import UUIDInfo |
|
| 4 |
+ |
|
| 5 |
+import shortuuid |
|
| 6 |
+ |
|
| 7 |
+ |
|
| 8 |
+def curtailUUID(length=10): |
|
| 9 |
+ flag = True |
|
| 10 |
+ while flag: |
|
| 11 |
+ uuid = shortuuid.uuid()[-length:] |
|
| 12 |
+ try: |
|
| 13 |
+ UUIDInfo.objects.get(uuid=uuid) |
|
| 14 |
+ except UUIDInfo.DoesNotExist: |
|
| 15 |
+ flag = False |
|
| 16 |
+ return uuid |