# 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)
# 
# def import_class(classpath, package=None):
#     classname = classpath.split('.')[-1]
#     classpath = '.'.join(classpath.split('.')[:-1])
#     return getattr(import_module(classpath), classname, None)


from django.utils import simplejson
from django.db.models import get_model
from django.shortcuts import get_object_or_404
from django.forms.models import modelform_factory
from dajaxice.decorators import dajaxice_register
from editlive.utils import get_adaptor, import_class
    load_tags = kwargs.get('load_tags')

    if tpl_filters:
        adaptor.template_filters = tpl_filters.split('|')
        if load_tags:
            adaptor.load_tags = load_tags.split('|')

    adaptor.set_value(field_value)

    return simplejson.dumps(adaptor.save())


@dajaxice_register(method='POST')
def save_form(request, **kwargs):
    meta = kwargs.get('meta')
    model = get_model(*meta.get('model').split('.'))
    fkwargs = {}

    if meta.get('formClass'):
        form_class = import_class(meta.get('formClass'))
    else:
        form_class = modelform_factory(model)

    if meta.get('formPrefix'):
        fkwargs['prefix'] = meta.get('formPrefix')

    form = form_class(kwargs.get('data'), **fkwargs)

    if form.is_valid():
        form.save()

# 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)
# 
# def import_class(classpath, package=None):
#     classname = classpath.split('.')[-1]
#     classpath = '.'.join(classpath.split('.')[:-1])
#     return getattr(import_module(classpath), classname, None)


# Path: editlive/ajax.py
from django.utils import simplejson
from django.db.models import get_model
from django.shortcuts import get_object_or_404
from django.forms.models import modelform_factory
from dajaxice.decorators import dajaxice_register
from editlive.utils import get_adaptor, import_class

    load_tags = kwargs.get('load_tags')

    if tpl_filters:
        adaptor.template_filters = tpl_filters.split('|')
        if load_tags:
            adaptor.load_tags = load_tags.split('|')

    adaptor.set_value(field_value)

    return simplejson.dumps(adaptor.save())


@dajaxice_register(method='POST')
def save_form(request, **kwargs):
    meta = kwargs.get('meta')
    model = get_model(*meta.get('model').split('.'))
    fkwargs = {}

    if meta.get('formClass'):
        form_class = import_class(meta.get('formClass'))
    else:
        form_class = modelform_factory(model)

    if meta.get('formPrefix'):
        fkwargs['prefix'] = meta.get('formPrefix')

    form = form_class(kwargs.get('data'), **fkwargs)

    if form.is_valid():
        form.save()

        out = {'error': False}