# Path: djangojames/models/utils.py
# def unique_text(text, model_klass, field, exclude_fields_dict=None):
#     
#     kwargs = {'%s__istartswith' % field:text}
#     # conact queryset
#     qs = model_klass.objects.filter(**kwargs)
#     if exclude_fields_dict:
#         qs = qs.exclude(**exclude_fields_dict)
#         
#     # extract the existing values and get their postfix
#     others = [int(getattr(other, field).replace(text, "0")) for other in qs if getattr(other, field).replace(text, "0").isdigit()]
#     # do we need a postfix?
#     if len(others) > 0 and 0 in others:
#         text = "%s%d" % (text, max(others) + 1)
#     return text    


from django.db.models.signals import pre_save
from django.template.defaultfilters import slugify
from djangojames.models.utils import unique_text
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2012 ITerativ GmbH. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Created on Mar 13, 2012
# @author: github.com/maersu

  
class UniqeSluger(object):
    def __init__(self, source, target):
        self.target = target
        self.source = source
        
    def __call__(self, instance, **kwargs):
        source_field = getattr(instance, self.source)

# Path: djangojames/models/utils.py
# def unique_text(text, model_klass, field, exclude_fields_dict=None):
#     
#     kwargs = {'%s__istartswith' % field:text}
#     # conact queryset
#     qs = model_klass.objects.filter(**kwargs)
#     if exclude_fields_dict:
#         qs = qs.exclude(**exclude_fields_dict)
#         
#     # extract the existing values and get their postfix
#     others = [int(getattr(other, field).replace(text, "0")) for other in qs if getattr(other, field).replace(text, "0").isdigit()]
#     # do we need a postfix?
#     if len(others) > 0 and 0 in others:
#         text = "%s%d" % (text, max(others) + 1)
#     return text    


# Path: djangojames/models/decorators.py
from django.db.models.signals import pre_save
from django.template.defaultfilters import slugify
from djangojames.models.utils import unique_text

# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2012 ITerativ GmbH. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Created on Mar 13, 2012
# @author: github.com/maersu

  
class UniqeSluger(object):
    def __init__(self, source, target):
        self.target = target
        self.source = source
        
    def __call__(self, instance, **kwargs):
        source_field = getattr(instance, self.source)

        targte_value = unique_text(slugify(source_field),