import os
import settings
import string
import random

# move uploaded file 'f' to file path 'dest'
def handle_uploaded_file(f, dest):
	destination = open(dest, 'wb+')
	for chunk in f.chunks():
		destination.write(chunk)
	destination.close()

# generate random string in length n
def generate_random_string(n, uppercase=False, allcase=False):
	if allcase:
		return ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(n))
	else:
		if uppercase:
			return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(n))
		else:	
			return ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(n))

# force-make directory (ignore exception)
def force_make_dir(dir):
	# set media root permission
	#media_path_permission()
	try:
		os.makedirs(dir)
	except OSError:
		pass

def media_url2path(url):
	return settings.MEDIA_ROOT + url.split(settings.MEDIA_URL)[1]