# Path: editlive/utils.py
# def get_adaptor(request, obj, field_name, field_value=None, kwargs={}, adaptor=None):
#     # Related field
#     if field_name.endswith('_set'):
#         if adaptor is None:
#             adaptor = get_default_adaptor('stacked') #  TODO: customizable
#         path_module, class_adaptor = ('.'.join(adaptor.split('.')[:-1]), \
#                                         adaptor.split('.')[-1])
#         Adaptor = getattr(import_module(path_module), class_adaptor)
#         return Adaptor(request, obj, field_name, field_value, kwargs=kwargs)
# 
#     else:  # Vanilla field
# 
#         if '_set-' in field_name:  # Formset field
#             manager, pos, field_name = filter(None, \
#                     re.split(r'(\w+)_set-(\d+)-(\w+)', field_name))
# 
#         field = obj._meta.get_field_by_name(field_name)[0]
#         if adaptor is None:
#             adaptor = get_default_adaptor(field)
#         path_module, class_adaptor = ('.'.join(adaptor.split('.')[:-1]), \
#                                         adaptor.split('.')[-1])
#         Adaptor = getattr(import_module(path_module), class_adaptor)
# 
#         return Adaptor(request, field, obj, field_name, \
#                 field_value=field_value, kwargs=kwargs)


from django import template
from django.utils.log import getLogger
from django.template import Variable, VariableDoesNotExist
from editlive.utils import get_adaptor
    field_name = context_variable.split('.')[-1]
    # Ex: "editlive.adaptors.TextAdaptor"
    adaptor_str = kwargs.get('adaptor', None)
    try:
        context_obj = Variable(context_variable.split('.')[0])\
                        .resolve(context)
        request = Variable('request').resolve(context)
        if kwargs.get('formset'):
            try:
                kwargs['field-index'] = Variable('forloop.counter0')\
                                            .resolve(context)
            except VariableDoesNotExist:
                kwargs['field-index'] = 0

        adaptor = get_adaptor(request, context_obj, field_name, field_value=field_value,\
                                kwargs=kwargs, adaptor=adaptor_str)
        return adaptor.render()
    except VariableDoesNotExist:
        logger.warning("editlive: the template variable \"%s\" doesn't exists." % context_variable)
        return u''


def prefix(value, string):
    return '%s%s' % (value, string)
register.simple_tag(prefix)
register.filter('prefix', prefix)


def suffix(value, string):
    return '%s%s' % (value, string)

# Path: editlive/utils.py
# def get_adaptor(request, obj, field_name, field_value=None, kwargs={}, adaptor=None):
#     # Related field
#     if field_name.endswith('_set'):
#         if adaptor is None:
#             adaptor = get_default_adaptor('stacked') #  TODO: customizable
#         path_module, class_adaptor = ('.'.join(adaptor.split('.')[:-1]), \
#                                         adaptor.split('.')[-1])
#         Adaptor = getattr(import_module(path_module), class_adaptor)
#         return Adaptor(request, obj, field_name, field_value, kwargs=kwargs)
# 
#     else:  # Vanilla field
# 
#         if '_set-' in field_name:  # Formset field
#             manager, pos, field_name = filter(None, \
#                     re.split(r'(\w+)_set-(\d+)-(\w+)', field_name))
# 
#         field = obj._meta.get_field_by_name(field_name)[0]
#         if adaptor is None:
#             adaptor = get_default_adaptor(field)
#         path_module, class_adaptor = ('.'.join(adaptor.split('.')[:-1]), \
#                                         adaptor.split('.')[-1])
#         Adaptor = getattr(import_module(path_module), class_adaptor)
# 
#         return Adaptor(request, field, obj, field_name, \
#                 field_value=field_value, kwargs=kwargs)


# Path: editlive/templatetags/editlive_tags.py
from django import template
from django.utils.log import getLogger
from django.template import Variable, VariableDoesNotExist
from editlive.utils import get_adaptor

    field_name = context_variable.split('.')[-1]
    # Ex: "editlive.adaptors.TextAdaptor"
    adaptor_str = kwargs.get('adaptor', None)
    try:
        context_obj = Variable(context_variable.split('.')[0])\
                        .resolve(context)
        request = Variable('request').resolve(context)
        if kwargs.get('formset'):
            try:
                kwargs['field-index'] = Variable('forloop.counter0')\
                                            .resolve(context)
            except VariableDoesNotExist:
                kwargs['field-index'] = 0

        adaptor = get_adaptor(request, context_obj, field_name, field_value=field_value,\
                                kwargs=kwargs, adaptor=adaptor_str)
        return adaptor.render()
    except VariableDoesNotExist:
        logger.warning("editlive: the template variable \"%s\" doesn't exists." % context_variable)
        return u''


def prefix(value, string):
    return '%s%s' % (value, string)
register.simple_tag(prefix)
register.filter('prefix', prefix)


def suffix(value, string):
    return '%s%s' % (value, string)

register.simple_tag(suffix)