# Path: djam/forms.py
# class GFKField(forms.MultiValueField):
#     widget = GFKWidget
# 
#     def __init__(self, model, content_object_field, *args, **kwargs):
#         for field in model._meta.virtual_fields:
#             if field.name == content_object_field:
#                 break
#         else:
#             raise FieldDoesNotExist("No virtual field called {0} exists."
#                                     "".format(content_object_field))
# 
#         if not isinstance(field, GenericForeignKey):
#             raise FieldDoesNotExist("No generic foreign key called {0}"
#                                     "exists.".format(content_object_field))
# 
#         fields = (
#             model._meta.get_field(field.ct_field).formfield(),
#             model._meta.get_field(field.fk_field).formfield()
#         )
#         super(GFKField, self).__init__(fields, *args, **kwargs)
#         widgets = self.widget.widgets = tuple(f.widget for f in fields)
#         widgets[0].attrs['data-required'] = int(self.required)
# 
#     def compress(self, data_list):
#         ct, pk = data_list
#         model = ct.model_class()
#         try:
#             return model.objects.get(pk=pk)
#         except model.DoesNotExist:
#             raise ValidationError("Choose a valid object id.")
# 
# class GFKForm(forms.ModelForm):
#     def __init__(self, *args, **kwargs):
#         super(GFKForm, self).__init__(*args, **kwargs)
#         object_data = {}
#         for name, field in self.fields.iteritems():
#             if isinstance(field, GFKField):
#                 object_data[name] = getattr(self.instance, name, None)
#         object_data.update(self.initial)
#         self.initial = object_data
# 
#     def _post_clean(self):
#         for name, field in self.fields.iteritems():
#             if isinstance(field, GFKField) and name in self.cleaned_data:
#                 setattr(self.instance, name, self.cleaned_data[name])
#         super(GFKForm, self)._post_clean()


from djam.forms import GFKField, GFKForm
from test_project.example_app.models import ExampleModel
import floppyforms as forms



class HelloWorldForm(forms.Form):
    name = forms.SlugField()


class ExampleModelForm(GFKForm):

# Path: djam/forms.py
# class GFKField(forms.MultiValueField):
#     widget = GFKWidget
# 
#     def __init__(self, model, content_object_field, *args, **kwargs):
#         for field in model._meta.virtual_fields:
#             if field.name == content_object_field:
#                 break
#         else:
#             raise FieldDoesNotExist("No virtual field called {0} exists."
#                                     "".format(content_object_field))
# 
#         if not isinstance(field, GenericForeignKey):
#             raise FieldDoesNotExist("No generic foreign key called {0}"
#                                     "exists.".format(content_object_field))
# 
#         fields = (
#             model._meta.get_field(field.ct_field).formfield(),
#             model._meta.get_field(field.fk_field).formfield()
#         )
#         super(GFKField, self).__init__(fields, *args, **kwargs)
#         widgets = self.widget.widgets = tuple(f.widget for f in fields)
#         widgets[0].attrs['data-required'] = int(self.required)
# 
#     def compress(self, data_list):
#         ct, pk = data_list
#         model = ct.model_class()
#         try:
#             return model.objects.get(pk=pk)
#         except model.DoesNotExist:
#             raise ValidationError("Choose a valid object id.")
# 
# class GFKForm(forms.ModelForm):
#     def __init__(self, *args, **kwargs):
#         super(GFKForm, self).__init__(*args, **kwargs)
#         object_data = {}
#         for name, field in self.fields.iteritems():
#             if isinstance(field, GFKField):
#                 object_data[name] = getattr(self.instance, name, None)
#         object_data.update(self.initial)
#         self.initial = object_data
# 
#     def _post_clean(self):
#         for name, field in self.fields.iteritems():
#             if isinstance(field, GFKField) and name in self.cleaned_data:
#                 setattr(self.instance, name, self.cleaned_data[name])
#         super(GFKForm, self)._post_clean()


# Path: test_project/test_project/example_app/forms.py
from djam.forms import GFKField, GFKForm
from test_project.example_app.models import ExampleModel
import floppyforms as forms




class HelloWorldForm(forms.Form):
    name = forms.SlugField()


class ExampleModelForm(GFKForm):

    content_object = GFKField(ExampleModel, 'content_object')