<commit_before># -*- coding: utf-8 -*-

import os
import time
import pickle
import logging
import datetime
import json

from pprint import pprint

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext

from django.http import HttpResponseForbidden
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
from django.utils import simplejson

logger = logging.getLogger(__name__)

import boto.ec2
import boto.ec2.cloudwatch

from django.contrib.auth.models import User
from userprofile.models import Profile as userprofile

from amazon import s3_funcs
from amazon import s3_funcs_shortcuts

from django import forms
from forms import UploadFileForm
from cloud_storage.models import Files
from cloud_storage.models import Uploaded_Files

from django.template.defaultfilters import filesizeformat, upper
from django.contrib.humanize.templatetags.humanize import naturalday
from cloudly.templatetags.cloud_extras import clear_filename, get_file_extension

from userprofile.views import _log_user_activity

from PIL import Image


supported_image_file_types = [ 'bmp','dib','dcx','eps','ps','gif','im','jpg','jpe','jpeg', \
			'pcd','pcx','png','pbm','pgm','ppm','psd','tif','tiff','xbm','xpm',]


def _resize_image(img, box, fit, out):
	# Pre-resize image with factor 2, 4, 8 and fast algorithm
	factor = 1
	while img.size[0]/factor > 2*box[0] and img.size[1]*2/factor > 2*box[1]:
		factor *=2
	if factor > 1:
		img.thumbnail((img.size[0]/factor, img.size[1]/factor), Image.NEAREST)

	# Calculate the cropping box and get the cropped part
	if fit:
		x1 = y1 = 0
		x2, y2 = img.size
		wRatio = 1.0 * x2/box[0]
		hRatio = 1.0 * y2/box[1]
		if hRatio > wRatio:
			y1 = int(y2/2-box[1]*wRatio/2)
			y2 = int(y2/2+box[1]*wRatio/2)
		else:
			x1 = int(x2/2-box[0]*hRatio/2)
			x2 = int(x2/2+box[0]*hRatio/2)
		img = img.crop((x1,y1,x2,y2))

	# Resize the image with best quality algorithm ANTI-ALIAS
	img.thumbnail(box, Image.ANTIALIAS)
	# Save it into a file-like object
	img.save(out, "JPEG", quality=75)


def _work_thumbnail(file_object):
	
	f = file_object
	
	filename = "media/" + str(f.file.file)
	file_type = str(f.file.file).split('.')[-1:][0].lower()

	if(file_type in supported_image_file_types):
		
		image = Image.open(filename)
		thumbnail_dimensions = [100,100]
		thumb_filename = filename.split('.')[:-1][0] + '-thumb' + str(thumbnail_dimensions[0]) + 'x' + str(thumbnail_dimensions[1]) + '.' + file_type
		
		thumb = open(thumb_filename,'wb+')
		try:
			_resize(image, thumbnail_dimensions, True, thumb)
		except:
			print '** failed to convert', 

		thumb.close()
		f.thumbnail_pic1 = thumb_filename
		f.save()

	return f.thumbnail_pic1



def delete_file(request, file_id):

	print '-- delete_file:'

	if not request.user.is_authenticated():
		return HttpResponseRedirect("/")<commit_msg>cosmetic updates to the cloud_storage code<commit_after># -*- coding: utf-8 -*-

import os
import time
import pickle
import logging
import datetime
import json

from pprint import pprint

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext

from django.http import HttpResponseForbidden
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
from django.utils import simplejson

logger = logging.getLogger(__name__)

import boto.ec2
import boto.ec2.cloudwatch

from django.contrib.auth.models import User
from userprofile.models import Profile as userprofile

from amazon import s3_funcs
from amazon import s3_funcs_shortcuts

from django import forms
from forms import UploadFileForm
from cloud_storage.models import Files
from cloud_storage.models import Uploaded_Files

from django.template.defaultfilters import filesizeformat, upper
from django.contrib.humanize.templatetags.humanize import naturalday
from cloudly.templatetags.cloud_extras import clear_filename, get_file_extension

from userprofile.views import _log_user_activity

from PIL import Image


SUPPORTED_IMAGE_FILE_TYPES = [ 'bmp','dib','dcx','eps','ps','gif','im','jpg','jpe','jpeg', \
			'pcd','pcx','png','pbm','pgm','ppm','psd','tif','tiff','xbm','xpm',]


def _resize_image(img, box, fit, out):
	# Pre-resize image with factor 2, 4, 8 and fast algorithm
	factor = 1
	while img.size[0]/factor > 2*box[0] and img.size[1]*2/factor > 2*box[1]:
		factor *=2
	if factor > 1:
		img.thumbnail((img.size[0]/factor, img.size[1]/factor), Image.NEAREST)

	# Calculate the cropping box and get the cropped part
	if fit:
		x1 = y1 = 0
		x2, y2 = img.size
		wRatio = 1.0 * x2/box[0]
		hRatio = 1.0 * y2/box[1]
		if hRatio > wRatio:
			y1 = int(y2/2-box[1]*wRatio/2)
			y2 = int(y2/2+box[1]*wRatio/2)
		else:
			x1 = int(x2/2-box[0]*hRatio/2)
			x2 = int(x2/2+box[0]*hRatio/2)
		img = img.crop((x1,y1,x2,y2))

	# Resize the image with best quality algorithm ANTI-ALIAS
	img.thumbnail(box, Image.ANTIALIAS)
	# Save it into a file-like object
	img.save(out, "JPEG", quality=75)


def _work_thumbnail(file_object):
	
	f = file_object
	
	filename = "media/" + str(f.file.file)
	file_type = str(f.file.file).split('.')[-1:][0].lower()

	if(file_type in SUPPORTED_IMAGE_FILE_TYPES):
		
		image = Image.open(filename)
		thumbnail_dimensions = [100,100]
		thumb_filename = filename.split('.')[:-1][0] + '-thumb' + str(thumbnail_dimensions[0]) + 'x' + str(thumbnail_dimensions[1]) + '.' + file_type
		
		thumb = open(thumb_filename,'wb+')
		try:
			_resize(image, thumbnail_dimensions, True, thumb)
		except:
			print '** failed to convert', 

		thumb.close()
		f.thumbnail_pic1 = thumb_filename
		f.save()

	return f.thumbnail_pic1



def delete_file(request, file_id):

	print '-- delete_file:'

	if not request.user.is_authenticated():
		return HttpResponseRedirect("/")
{'fraction_non_alphanumeric': 0.12612612612612611, 'fraction_numerical': 0.024817270100288967, 'mean_word_length': 4.860557768924303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1964633', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2062, 'n_words': 508}