# -*- coding: utf-8 -*- 

from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import ugettext_lazy as _
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
from django.utils import simplejson

import datetime, hashlib, os.path, os
import random, string
import unicodedata


class Singleton(type):
    """ Singleton metaclass. """

    def __init__(cls, name, bases, dct):
        cls.__instance = None
        type.__init__(cls, name, bases, dct)
 
    def __call__(cls, *args, **kw):
        if cls.__instance is None:
            cls.__instance = type.__call__(cls, *args,**kw)
        return cls.__instance


def encrypt_password(password):
    """ Make password hash. """
    return hashlib.sha256(password).hexdigest()


def make_repo_location(project):
    project_repo_path = os.path.join(settings.REPO_ROOT, project.slug)
    if not os.path.exists(project_repo_path):
        os.mkdir(project_repo_path)

    return project_repo_path, project.slug


def clear_model_dict(data):
    hidden_fields = ['password']

    new_dict = {}
    for key, val in data.items():
        if not key.startswith('_') and key not in hidden_fields:
            new_dict[key] = val

    return new_dict


class LazyEncoder(simplejson.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Promise):
            return force_unicode(obj)
        elif isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%dT%H:%M:%S')
        return super(LazyEncoder, self).default(obj)


def random_name(chars=string.ascii_lowercase, length=16, prefix='',suffix=''):
    if length - len(prefix)-len(suffix) < 0:
        raise ValueError("length - len(prefix)-len(suffix) is < 0")

    filename = ''.join([random.choice(chars) for i in range(length-len(prefix)-len(suffix))])
    filename = prefix + filename + suffix
    return filename


def api_login_required(function):
    def _view_wrapper(request, *args, **kwargs):
        response = {'id': None, 'valid': False, 'errors':[_(u'You have to start session to proceed')]}
        if request.user.is_authenticated() or request.path == '/api/login':
            return function(request, *args, **kwargs)
        else:
            response_data = simplejson.dumps(response, cls=LazyEncoder, indent=4, sort_keys=True)
            return HttpResponse(response_data, mimetype='text/plain')

    return _view_wrapper


def normalize_tagname(tagname):
    value = unicodedata.normalize('NFKD', tagname).encode('ascii', 'ignore')
    return value.lower()

@@ -240,44 +240,8 @@
 code
-%0Afrom django.utils import simplejson
 %0A%0Aim
@@ -375,17 +375,16 @@
 ss. %22%22%22%0A
-%0A
     def 
@@ -664,1761 +664,235 @@
 def 
-encrypt_password(password):%0A    %22%22%22 Make password hash. %22%22%22%0A    return hashlib.sha256(password).hexdigest()%0A%0A%0Adef make_repo_location(project):%0A    project_repo_path = os.path.join(settings.REPO_ROOT, project.slug)%0A    if not os.path.exists(project_repo_path):%0A        os.mkdir(project_repo_path)%0A%0A    return project_repo_path, project.slug%0A%0A%0Adef clear_model_dict(data):%0A    hidden_fields = %5B'password'%5D%0A%0A    new_dict = %7B%7D%0A    for key, val in data.items():%0A        if not key.startswith('_') and key not in hidden_fields:%0A            new_dict%5Bkey%5D = val%0A%0A    return new_dict%0A%0A%0Aclass LazyEncoder(simplejson.JSONEncoder):%0A    def default(self, obj):%0A        if isinstance(obj, Promise):%0A            return force_unicode(obj)%0A        elif isinstance(obj, datetime.datetime):%0A            return obj.strftime('%25Y-%25m-%25dT%25H:%25M:%25S')%0A        return super(LazyEncoder, self).default(obj)%0A%0A%0Adef random_name(chars=string.ascii_lowercase, length=16, prefix='',suffix=''):%0A    if length - len(prefix)-len(suffix) %3C 0:%0A        raise ValueError(%22length - len(prefix)-len(suffix) is %3C 0%22)%0A%0A    filename = ''.join(%5Brandom.choice(chars) for i in range(length-len(prefix)-len(suffix))%5D)%0A    filename = prefix + filename + suffix%0A    return filename%0A%0A%0Adef api_login_required(function):%0A    def _view_wrapper(request, *args, **kwargs):%0A        response = %7B'id': None, 'valid': False, 'errors':%5B_(u'You have to start session to proceed')%5D%7D%0A        if request.user.is_authenticated() or request.path == '/api/login':%0A            return function(request, *args, **kwargs)%0A        else:%0A            response_data = simplejson.dumps(response, cls=LazyEncoder, indent=4, sort_keys=True)%0A            return HttpResponse(response_data, mimetype='text/plain')%0A%0A    return _view_wrapper
+clear_model_dict(data):%0A    hidden_fields = %5B'password'%5D%0A%0A    new_dict = %7B%7D%0A    for key, val in data.items():%0A        if not key.startswith('_') and key not in hidden_fields:%0A            new_dict%5Bkey%5D = val%0A%0A    return new_dict
 %0A%0A%0Ad

Delete nonused methods.