def get_image(request, id):
try:
db_image = DBImage.objects.get(id=id)
try:
server = ImageServer.objects.get(id=db_image.server)
if server.server_name != settings.SERVER_NAME:
return HttpResponseBadRequest("Image Not On This Server")
except ImageServer.DoesNotExist:
return HttpResponseServerError("Image Server Does Not Exist!")
image_data = open(db_image.path, 'rb').read()
mime_type = db_image.mime_type
return HttpResponse(image_data, content_type=mime_type)
except DBImage.DoesNotExist:
raise Http404("Image Not Found")
except IOError:
return HttpResponseGone("Image Moved")
python类HttpResponseGone()的实例源码
def get_image(request, id):
try:
db_image = DBImage.objects.get(id=id)
mime_type = db_image.mime_type
try:
server = ImageServer.objects.get(id=db_image.server)
ip = server.server_ip
port = request.META['SERVER_PORT']
url_ext = reverse('image_server:get_image', args=(str(id) ,))
remote_url = 'http://' + ip + ':' + str(port) + url_ext
image_data = urllib.request.urlopen(remote_url).read()
return HttpResponse(image_data, content_type=mime_type)
except ImageServer.DoesNotExist:
return HttpResponseServerError("Image Server Does Not Exist!")
except DBImage.DoesNotExist:
raise Http404("Image Not Found")
except:
return HttpResponseGone("Image Moved")
def get_thumbnail(request, id):
try:
db_image = DBImage.objects.get(id=id)
mime_type = db_image.mime_type
try:
server = ImageServer.objects.get(id=db_image.server)
ip = server.server_ip
port = request.META['SERVER_PORT']
url_ext = reverse('image_server:get_thumbnail', args=(str(id) ,))
remote_url = 'http://' + ip + ':' + str(port) + url_ext
print(remote_url)
image_data = urllib.request.urlopen(remote_url).read()
return HttpResponse(image_data, content_type=mime_type)
except ImageServer.DoesNotExist:
return HttpResponseServerError("Image Server Does Not Exist!")
except DBImage.DoesNotExist:
raise Http404("Image Not Found")
except Exception as e:
print(e)
return HttpResponseGone("Image Moved")
def get(self, request, *args, **kwargs):
url = self.get_redirect_url(*args, **kwargs)
if url:
if self.permanent:
return http.HttpResponsePermanentRedirect(url)
else:
return http.HttpResponseRedirect(url)
else:
logger.warning('Gone: %s', request.path,
extra={
'status_code': 410,
'request': request
})
return http.HttpResponseGone()
def gone_view(request, *args, **kwargs):
return HttpResponseGone()
def apiv1_gone(request):
return HttpResponseGone(apiv1_gone_msg)
def process_response(request, response):
if response.status_code != 404:
return response
full_path = request.get_full_path()
redirect = None
try:
redirect = Redirect.objects.get(old_path=full_path)
except Redirect.DoesNotExist:
pass
if settings.APPEND_SLASH and not request.path.endswith('/'):
# Try appending a trailing slash.
path_len = len(request.path)
full_path = full_path[:path_len] + '/' + full_path[path_len:]
try:
redirect = Redirect.objects.get(old_path=full_path)
except Redirect.DoesNotExist:
pass
if redirect is None:
return response
if redirect.new_path == '':
return http.HttpResponseGone()
redirect.last_usage = now()
redirect.save()
if redirect.permanent:
return http.HttpResponsePermanentRedirect(redirect.new_path)
else:
return http.HttpResponseRedirect(redirect.new_path)
def get(self, request, *args, **kwargs):
url = self.get_redirect_url(*args, **kwargs)
if url:
if self.permanent:
return http.HttpResponsePermanentRedirect(url)
else:
return http.HttpResponseRedirect(url)
else:
logger.warning(
'Gone: %s', request.path,
extra={'status_code': 410, 'request': request}
)
return http.HttpResponseGone()
chapter_16_example_15.py 文件源码
项目:two-scoops-of-django-1.11
作者: twoscoops
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def apiv1_gone(request):
return HttpResponseGone(apiv1_gone_msg)
def get(self, request, *args, **kwargs):
url = self.get_redirect_url(*args, **kwargs)
if url:
if self.permanent:
return http.HttpResponsePermanentRedirect(url)
else:
return http.HttpResponseRedirect(url)
else:
logger.warning('Gone: %s', request.path,
extra={
'status_code': 410,
'request': request
})
return http.HttpResponseGone()
def process_response(self, request, response):
if response.status_code != 404:
return response # No need to check for a redirect for non-404 responses.
full_path = request.get_full_path()
current_site = get_current_site(request)
r = None
try:
r = Redirect.objects.get(site=current_site, old_path=full_path)
except Redirect.DoesNotExist:
pass
if settings.APPEND_SLASH and not request.path.endswith('/'):
# Try appending a trailing slash.
path_len = len(request.path)
full_path = full_path[:path_len] + '/' + full_path[path_len:]
try:
r = Redirect.objects.get(site=current_site, old_path=full_path)
except Redirect.DoesNotExist:
pass
if r is not None:
if r.new_path == '':
return http.HttpResponseGone()
return http.HttpResponsePermanentRedirect(r.new_path)
# No redirect was found. Return the response.
return response
def get(self, request, *args, **kwargs):
url = self.get_redirect_url(*args, **kwargs)
if url:
if self.permanent:
return http.HttpResponsePermanentRedirect(url)
else:
return http.HttpResponseRedirect(url)
else:
logger.warning('Gone: %s', request.path,
extra={
'status_code': 410,
'request': request
})
return http.HttpResponseGone()
def get_thumbnail(request, id):
try:
db_image = DBImage.objects.get(id=id)
thumbnail = db_image.get_thumbnail()
image_data = thumbnail.file.read()
mime_type = thumbnail.mime_type
return HttpResponse(image_data, content_type=mime_type)
except DBImage.DoesNotExist:
raise Http404("Image Not Found")
except IOError:
return HttpResponseGone("Image Moved")
def get(self, request, *args, **kwargs):
url = self.get_redirect_url(*args, **kwargs)
if url:
if self.permanent:
return http.HttpResponsePermanentRedirect(url)
else:
return http.HttpResponseRedirect(url)
else:
logger.warning('Gone: %s', request.path,
extra={
'status_code': 410,
'request': request
})
return http.HttpResponseGone()