Exemplo n.º 1
0
	def get_data_url(self, max_width = None):
		data_url = b'data:{};base64,{}'
		
		if self.is_image():
			image_file = ImageBufferIO(self.file_data)
			output = ImageBufferIO()
			
			try:
				image = Image.open(image_file)
				
				s = image.size
				
				if max_width and s[0] > max_width:
					ratio = max_width / s[0]
					width = s[0] * ratio
					height = s[1] * ratio
					self._generate_thumbnail(image, output, (width, height))
					
					file_data = output.getvalue()
				else:
					file_data = image_file.getvalue()
			except IOError:
				file_data = self.file_data
				logger.exception('Error when trying to resize image for data: url')
		else:
			file_data = self.file_data
			
		data = bytes(url_quote(file_data.encode('base64')))
		
		return data_url.format(self.content_type, data)
Exemplo n.º 2
0
	def get_data_url(self, max_width = None):
		data_url = b'data:{};base64,{}'
		
		if self.is_image():
			image_file = ImageBufferIO(self.file_data)
			output = ImageBufferIO()
			
			try:
				image = Image.open(image_file)
				
				s = image.size
				
				if max_width and s[0] > max_width:
					ratio = max_width / s[0]
					width = s[0] * ratio
					height = s[1] * ratio
					self._generate_thumbnail(image, output, (width, height))
					
					file_data = output.getvalue()
				else:
					file_data = image_file.getvalue()
			except IOError:
				file_data = self.file_data
				logger.exception('Error when trying to resize image for data: url')
		else:
			file_data = self.file_data
			
		data = bytes(url_quote(file_data.encode('base64')))
		
		return data_url.format(self.content_type, data)
Exemplo n.º 3
0
	def dispatch(self, request, *args, **kwargs):
		try:
			self.request = request
			logger.debug('BEGIN REQUEST *********** {}'.format(request.path))
			if not self._login_requirement_ok(request):
				logger.debug('Login requirement is not ok, redirecting')
				self.error('You were not logged in properly. Please try again')
				return redirect('{}?{}={}'.format(
					reverse(getattr(settings, 'ASYM_FAILED_LOGIN_URL', settings.LOGIN_REDIRECT_URL)),
					REDIRECT_FIELD_NAME,
					request.path,
				))
			
			if hasattr(request, 'user'):
				logger.debug('User is: {}'.format(request.user))
			else:
				logger.debug('No user in request')
			permissions_required = self._merge_attr('permissions_required')
			
			logger.debug('The required permissions are {}'.format(permissions_required))
			
			logger.debug('AsymBaseView: Getting form data')
			self.get_form_data()
			
			# Do any preprocessing, which should also fill out the arguments
			# for the forms
			logger.debug('AsymBaseView: Preprocess')
			self.preprocess(request, *args, **kwargs)
			
			# Create the form instances, and place into context
			logger.debug('AsymBaseView: LoadForms')
			self.load_forms(request)
			
			logger.debug("AsymBaseView: Predispatch")
			self.predispatch(request, *args, **kwargs)
			
			logger.debug("AsymBaseView: Has Access")
			if not self._has_access(request, *args, **kwargs):
				self.error("You do not have permission to view that page")
				return redirect(reverse(getattr(settings, 'ASYM_FAILED_LOGIN_URL')))
			
			try:
				logger.debug('AsymBaseView: dispatch')
				response = super(AsymBaseView, self).dispatch(request, *args, **kwargs)
			except ForceRollback:
				# Ignore these because they're not real exceptions
				response = self.render_to_response()
			except ContentNotRenderedError as e:
				logger.exception('Content not rendered for template {}.'.format(self.template_name))
				self.template_name = '500.djhtml'
				return self.render_to_response()
			
			logger.debug('END REQUEST*********')
			return response
		except DeveloperTODO as e:
			logger.error('{}'.format(e))
			self.template_name = 'todo_error.djhtml'
			return self.render_to_response()
Exemplo n.º 4
0
	def _generate_thumbnail(self, image_file, output, size = None):
		try:
			if Image.isImageType(image_file):
				image = image_file
			else:
				image = Image.open(image_file)
			if image.mode != 'RGB':
				image = image.convert('RGB')
			if size is None:
				size = [self.Constants.PREVIEW_IMAGE_WIDTH, self.Constants.PREVIEW_IMAGE_HEIGHT]
			image.thumbnail(
				size,
				Image.ANTIALIAS
			)
			image.save(output, 'JPEG')
		except IOError:
			logger.exception('Could not generate thumbnail')
			raise PreviewImageGenerationFailed()
Exemplo n.º 5
0
	def _generate_thumbnail(self, image_file, output, size = None):
		try:
			if Image.isImageType(image_file):
				image = image_file
			else:
				image = Image.open(image_file)
			if image.mode != 'RGB':
				image = image.convert('RGB')
			if size is None:
				size = [self.Constants.PREVIEW_IMAGE_WIDTH, self.Constants.PREVIEW_IMAGE_HEIGHT]
			image.thumbnail(
				size,
				Image.ANTIALIAS
			)
			image.save(output, 'JPEG')
		except IOError:
			logger.exception('Could not generate thumbnail')
			raise PreviewImageGenerationFailed()