|
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.files.storage import default_storage
from django.db import transaction
from django.http import JsonResponse
from django.shortcuts import render, redirect
from rest_framework import viewsets
from account.models import LensmanInfo
from photo.models import UUIDInfo, PhotosInfo
from photo.serializers import PhotosInfoSerializer
from utils.uuid_utils import curtailUUID
from utils.watermark_utils import watermark_wrap
import os
import shortuuid
def uuid_init(request):
num = int(request.GET.get('num', 1000))
for i in xrange(num):
UUIDInfo.objects.create(uuid=curtailUUID(UUIDInfo))
return JsonResponse({
'status': 200,
'message': u'UUID 更新成功',
'data': '',
})
# curl -X POST -F user=xxxxxxx -F num=100 http://api.xfoto.com.cn/uuid
@transaction.atomic
def uuid(request):
lensman_id = request.POST.get('user', '')
num = int(request.POST.get('num', 100))
uuids = UUIDInfo.objects.select_for_update().filter(status=True)[:num]
for uuid in uuids:
uuid.lensman_id = lensman_id
uuid.status = False
uuid.save()
return JsonResponse({
'status': 200,
'message': u'获取唯一标识成功',
'data': [uuid.uuid for uuid in uuids],
})
# [How to do a PUT request with curl?](http://stackoverflow.com/questions/13782198/how-to-do-a-put-request-with-curl)
# Unfortunately, the -T is no substitute for -X PUT if you want to specify parameters with -d or -F.
# -T sends the content of a file via PUT. To achieve the GET after a redirect, add the parameter --location
#
# -F, --form <name=content>
# (HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data
# using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the
# 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file
# name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload,
# while the < makes a text field and just get the contents for that text field from a file.
#
# curl -X POST -F user=xxxxxxx -F session=xxxxxxx -F photo_id=xxxxxxx -F photo=@xxxxxxx.jpg http://api.xfoto.com.cn/photos/upload
def upload_photo(request):
lensman_id = request.POST.get('user', '')
session_id = request.POST.get('session', '')
photo_id = request.POST.get('photo_id', '')
photo = request.FILES.get('photo', '')
if not (lensman_id and session_id and photo):
return JsonResponse({
'status': 4010,
'message': u'参数错误',
})
try:
LensmanInfo.objects.get(lensman_id=lensman_id)
except LensmanInfo.DoesNotExist:
return JsonResponse({
'status': 4011,
'message': u'摄影师不存在',
})
# photo_id = curtailUUID(PhotosInfo, 'photo_id')
_, extension = os.path.splitext(photo.name)
m_photo_path = 'photo/{uuid}{extension}'.format(uuid=shortuuid.uuid(), extension=extension)
if default_storage.exists(m_photo_path):
default_storage.delete(m_photo_path)
default_storage.save(m_photo_path, photo)
p_photo_path = 'photo/{uuid}{extension}'.format(uuid=shortuuid.uuid(), extension=extension)
watermark_wrap(
os.path.join(settings.MEDIA_ROOT, m_photo_path).replace('\\', '/'),
settings.WATERMARK_LOGO,
os.path.join(settings.MEDIA_ROOT, p_photo_path).replace('\\', '/')
)
photo, created = PhotosInfo.objects.get_or_create(
lensman_id=lensman_id,
session_id=session_id,
photo_id=photo_id,
p_photo_path=p_photo_path,
m_photo_path=m_photo_path,
)
return JsonResponse({
'status': 200,
'message': u'照片上传成功',
'data': photo.data,
})
def session_detail_api(request, session):
photos = PhotosInfo.objects.filter(session_id=session)
return JsonResponse({
'status': 200,
'message': u'获取照片列表成功',
'data': {
'count': photos.count(),
'photos': [p.detail for p in photos]
}
})
def session_detail(request, session):
photos = PhotosInfo.objects.filter(session_id=session)
return render(request, 'photo/session_detail.html', {'photos': photos})
def photo_standard(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.p_photo_url})
def photo_standard_api(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return JsonResponse({
'status': 200,
'message': u'获取照片详情成功',
'data': {
'photo': photo.detail
}
})
def photo_medium(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.m_photo_url})
def photo_large(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.l_photo_url})
def photo_raw(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.r_photo_url})
class PhotoInfoViewSet(viewsets.ModelViewSet):
queryset = PhotosInfo.objects.all().order_by('-created_at')
serializer_class = PhotosInfoSerializer
|