本文整理匯總了Python中photos.models.Photo.attach方法的典型用法代碼示例。如果您正苦於以下問題:Python Photo.attach方法的具體用法?Python Photo.attach怎麽用?Python Photo.attach使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類photos.models.Photo
的用法示例。
在下文中一共展示了Photo.attach方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: upload_photo
# 需要導入模塊: from photos.models import Photo [as 別名]
# 或者: from photos.models.Photo import attach [as 別名]
#.........這裏部分代碼省略.........
if user_id:
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
response = {
'status': 0,
'message': "User does not exit: %s" % user_id,
}
return HttpResponse(json.dumps(response), mimetype=mimetype)
user_id = user.id
if user.is_anonymous():
user = None
user_id = 'site'
chunk = filename.split('.')
extension = ''
if len(chunk) > 1:
extension = chunk[len(chunk)-1]
extension = extension.lower()
if extension not in ['mp4', 'jpg', 'gif', 'png', 'jpeg']:
response = {
'status': 0,
'message': "Extension error!",
}
return HttpResponse(json.dumps(response), mimetype=mimetype)
if extension == 'mp4':
video = VideoStream.objects.create(videoupload=file, user=user)
response = {
'status': 1,
'id': video.id,
'url': video.get_absolute_url(),
'type': 'video',
}
else:
if settings.DEFAULT_FILE_STORAGE == "storages.backends.s3boto.S3BotoStorage":
# Now we adapt to s3 storage so need directly use rawdata
photo = Photo(image=file, user=user)
else:
# Origin method: only can work with local disk,
relative_path = "photos/%s_%s_%s" % (user_id, int(time.time()), filename)
full_path = os.path.join(settings.MEDIA_ROOT, relative_path)
# Write the file to disk
destination = open(full_path, 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
# Create the photo object
photo = Photo(image=relative_path, user=user)
photo.save()
# Photo.objects.filter(pk=photo.pk).update(user=user.id)
# Try to use the spec provided
spec = request.GET.get('spec', None)
if not spec:
response = {
'status': 0,
'message': "No imagespec specified.",
}
return HttpResponse(json.dumps(response), mimetype=mimetype)
if not hasattr(photo, spec):
response = {
'status': 0,
'message': "Imagespec %s does not exist." % spec,
}
return HttpResponse(json.dumps(response), mimetype=mimetype)
type = request.GET.get('type', None)
id = request.GET.get('id', None)
if type == 'event' and id:
from events.models import Event
try:
event = Event.objects.get(id=id)
photo.attach(event)
photo.event = event
event.add_photo(photo)
except Event.DoesNotExist:
raise Http404
response = {
'status': 1,
'id': photo.id,
'url': getattr(photo, spec).url,
'type': 'photo',
}
else:
response = {
'status': 0,
'message': "No file could be found.",
}
else:
response = {
'status': 0,
'message': "No file uploaded. Please try again.",
}
return HttpResponse(json.dumps(response), mimetype=mimetype)