# Path: editlive/utils.py
# def get_dict_from_obj(obj):
#     obj_dict = obj.__dict__
#     obj_dict_result = obj_dict.copy()
#     for key, value in obj_dict.items():
#         if '_id' in key and is_managed_field(obj, key.replace('_id', '')):
#             key2 = key.replace('_id', '')
#             obj_dict_result[key2] = obj_dict_result[key]
#             del obj_dict_result[key]
#     manytomany_list = obj._meta.many_to_many
#     for manytomany in manytomany_list:
#         val = manytomany.value_from_object(obj)
#         if isinstance(val, collections.Iterable):
#             ids = [obj_rel.id for obj_rel in val]
#             if ids:
#                 obj_dict_result[manytomany.name] = ids
# 
#     return obj_dict_result
# 
# def encodeURI(uri):
#     """
#     We really only need to escape " (double quotes) and non-ascii characters..
#     """
#     s = u"""!#$&'()*+,-./:;<=>?@[\]^_{|}~ """
#     return urllib.quote(unicode(uri).encode('utf8'), safe=s.encode('ascii'))
# 
# def apply_filters(value, filters, load_tags=None):
#     if filters:
#         filters_str = '|%s' % '|'.join(filters)
#         load_tags = load_tags or []
#         if load_tags:
#             load_tags_str = "{%% load %s %%}" % ' '.join(load_tags)
#         else:
#             load_tags_str = ""
#         ctx = template.Context({'value': value})
#         value = template.Template("""%s{{ value%s }}""" % (\
#                 load_tags_str, filters_str)).render(ctx)
#     return value
# 
# def import_class(classpath, package=None):
#     classname = classpath.split('.')[-1]
#     classpath = '.'.join(classpath.split('.')[:-1])
#     return getattr(import_module(classpath), classname, None)
# 
# def get_dynamic_modelform(**kwargs):
#     form = type('DynamicForm', (ModelForm, ), {})
#     meta = type('Meta', (object, ), {})
#     meta.exclude = kwargs.get('exclude', None)
#     form.Meta = meta
#     return form


import re
from datetime import datetime
from django.utils.safestring import mark_safe
from django.forms.models import modelform_factory
from editlive.utils import (get_dict_from_obj, encodeURI,\
        apply_filters, import_class, get_dynamic_modelform)
    def __init__(self, request, obj, manager, value, initial={}, **kwargs):
        self.request = request
        self.kwargs = kwargs['kwargs']
        self.manager_name = manager
        self.manager = getattr(obj, manager)
        self.model = self.manager.model
        self.obj = self.model()
        self.value = value
        # TODO:
        # fix this shit..
        # http://stackoverflow.com/questions/12462616/
        # finding-the-relation-field-of-a-related-object-in-django
        self.query_field = self.manager.core_filters.keys()[0].split('__')[0]
        self.form_class = self.get_form()
        self.initial = initial
        self.initial[self.query_field] = obj.pk

        self.form = self.form_class(**{
            'instance': self.obj,
            'initial': initial,
            'prefix': self.manager_name[:-4],
        })

    def get_object_list(self):
        return self.manager.all()

    def get_form(self):
        fkwargs = {}
        if self.kwargs.get('form'):
            # Custom form

# Path: editlive/utils.py
# def get_dict_from_obj(obj):
#     obj_dict = obj.__dict__
#     obj_dict_result = obj_dict.copy()
#     for key, value in obj_dict.items():
#         if '_id' in key and is_managed_field(obj, key.replace('_id', '')):
#             key2 = key.replace('_id', '')
#             obj_dict_result[key2] = obj_dict_result[key]
#             del obj_dict_result[key]
#     manytomany_list = obj._meta.many_to_many
#     for manytomany in manytomany_list:
#         val = manytomany.value_from_object(obj)
#         if isinstance(val, collections.Iterable):
#             ids = [obj_rel.id for obj_rel in val]
#             if ids:
#                 obj_dict_result[manytomany.name] = ids
# 
#     return obj_dict_result
# 
# def encodeURI(uri):
#     """
#     We really only need to escape " (double quotes) and non-ascii characters..
#     """
#     s = u"""!#$&'()*+,-./:;<=>?@[\]^_{|}~ """
#     return urllib.quote(unicode(uri).encode('utf8'), safe=s.encode('ascii'))
# 
# def apply_filters(value, filters, load_tags=None):
#     if filters:
#         filters_str = '|%s' % '|'.join(filters)
#         load_tags = load_tags or []
#         if load_tags:
#             load_tags_str = "{%% load %s %%}" % ' '.join(load_tags)
#         else:
#             load_tags_str = ""
#         ctx = template.Context({'value': value})
#         value = template.Template("""%s{{ value%s }}""" % (\
#                 load_tags_str, filters_str)).render(ctx)
#     return value
# 
# def import_class(classpath, package=None):
#     classname = classpath.split('.')[-1]
#     classpath = '.'.join(classpath.split('.')[:-1])
#     return getattr(import_module(classpath), classname, None)
# 
# def get_dynamic_modelform(**kwargs):
#     form = type('DynamicForm', (ModelForm, ), {})
#     meta = type('Meta', (object, ), {})
#     meta.exclude = kwargs.get('exclude', None)
#     form.Meta = meta
#     return form


# Path: editlive/adaptors/base.py
import re
from datetime import datetime
from django.utils.safestring import mark_safe
from django.forms.models import modelform_factory
from editlive.utils import (get_dict_from_obj, encodeURI,\
        apply_filters, import_class, get_dynamic_modelform)

    def __init__(self, request, obj, manager, value, initial={}, **kwargs):
        self.request = request
        self.kwargs = kwargs['kwargs']
        self.manager_name = manager
        self.manager = getattr(obj, manager)
        self.model = self.manager.model
        self.obj = self.model()
        self.value = value
        # TODO:
        # fix this shit..
        # http://stackoverflow.com/questions/12462616/
        # finding-the-relation-field-of-a-related-object-in-django
        self.query_field = self.manager.core_filters.keys()[0].split('__')[0]
        self.form_class = self.get_form()
        self.initial = initial
        self.initial[self.query_field] = obj.pk

        self.form = self.form_class(**{
            'instance': self.obj,
            'initial': initial,
            'prefix': self.manager_name[:-4],
        })

    def get_object_list(self):
        return self.manager.all()

    def get_form(self):
        fkwargs = {}
        if self.kwargs.get('form'):
            # Custom form

            fkwargs['form'] = import_class(self.kwargs.get('form'))