本文整理汇总了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)