|
# -*- coding: utf-8 -*-
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
import os
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=@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 = 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)
# photo_path = 'photo/{0}/{1}/{2}{3}'.format(lensman_id, session_id, photo_id, extension)
photo_name = '{0}{1}'.format(photo_id, extension)
photo_path = 'photo/{0}'.format(photo_name)
if default_storage.exists(photo_path):
default_storage.delete(photo_path)
default_storage.save(photo_path, photo)
photo, created = PhotosInfo.objects.get_or_create(
lensman_id=lensman_id,
session_id=session_id,
photo_id=photo_id,
photo_name=photo_name,
photo_path=photo_path
)
return JsonResponse({
'status': 200,
'message': u'照片上传成功',
'data': photo.data,
})
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.photo_url})
def photo_medium(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
def photo_large(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
def photo_raw(request, photo):
photo = PhotosInfo.objects.get(photo_id=photo)
return render(request, 'photo/photo_detail.html', {'photo_url': photo.photo_url})
class PhotoInfoViewSet(viewsets.ModelViewSet):
queryset = PhotosInfo.objects.all().order_by('-created_at')
serializer_class = PhotosInfoSerializer
|