from django import forms

from .models import Post, Album, Link

from opps.core.widgets import OppsEditor

from opps.db.models.fields.jsonf import JSONFormField
from opps.fields.widgets import JSONField
from opps.fields.models import Field, FieldOption


class PostAdminForm(forms.ModelForm):
    json = JSONFormField(widget=JSONField(attrs={'_model': 'Post'}), required=False)

    multiupload_link = '/fileupload/image/'

    def __init__(self, *args, **kwargs):
        super(PostAdminForm, self).__init__(*args, **kwargs)

        for field in Field.objects.filter(
            application__contains=self._meta.model.__name__):
            for fo in FieldOption.objects.filter(field=field):
                self.fields[
                    'json_{}_{}'.format(
                        field.slug, fo.option.slug
                    )] = forms.CharField(required=False)

    class Meta:
        model = Post
        widgets = {'content': OppsEditor()}


class AlbumAdminForm(forms.ModelForm):

    multiupload_link = '/fileupload/image/'

    class Meta:
        model = Album
        widgets = {
            'headline': OppsEditor()
        }


class LinkAdminForm(forms.ModelForm):
    class Meta:
        model = Link

from opps.core.widgets import OppsEditor
from opps.containers.forms import ContainerAdminForm

from .models import Post, Album, Link


class PostAdminForm(ContainerAdminForm):
    multiupload_link = '/fileupload/image/'

    class Meta:
        model = Post
        widgets = {'content': OppsEditor()}


class AlbumAdminForm(ContainerAdminForm):
    multiupload_link = '/fileupload/image/'

    class Meta:
        model = Album
        widgets = {
            'headline': OppsEditor()
        }


class LinkAdminForm(ContainerAdminForm):
    class Meta:
        model = Link

Extend all admin form to Container Admin Form (json field)
Extend all admin form to Container Admin Form (json field)

- from django import forms
+ from opps.core.widgets import OppsEditor
+ from opps.containers.forms import ContainerAdminForm
  
  from .models import Post, Album, Link
  
- from opps.core.widgets import OppsEditor
  
- from opps.db.models.fields.jsonf import JSONFormField
- from opps.fields.widgets import JSONField
- from opps.fields.models import Field, FieldOption
- 
- 
- class PostAdminForm(forms.ModelForm):
+ class PostAdminForm(ContainerAdminForm):
-     json = JSONFormField(widget=JSONField(attrs={'_model': 'Post'}), required=False)
- 
      multiupload_link = '/fileupload/image/'
- 
-     def __init__(self, *args, **kwargs):
-         super(PostAdminForm, self).__init__(*args, **kwargs)
- 
-         for field in Field.objects.filter(
-             application__contains=self._meta.model.__name__):
-             for fo in FieldOption.objects.filter(field=field):
-                 self.fields[
-                     'json_{}_{}'.format(
-                         field.slug, fo.option.slug
-                     )] = forms.CharField(required=False)
  
      class Meta:
          model = Post
          widgets = {'content': OppsEditor()}
  
  
- class AlbumAdminForm(forms.ModelForm):
+ class AlbumAdminForm(ContainerAdminForm):
- 
      multiupload_link = '/fileupload/image/'
  
      class Meta:
          model = Album
          widgets = {
              'headline': OppsEditor()
          }
  
  
- class LinkAdminForm(forms.ModelForm):
+ class LinkAdminForm(ContainerAdminForm):
      class Meta:
          model = Link
  
Extend all admin form to Container Admin Form (json field)
## Code Before:
from django import forms

from .models import Post, Album, Link

from opps.core.widgets import OppsEditor

from opps.db.models.fields.jsonf import JSONFormField
from opps.fields.widgets import JSONField
from opps.fields.models import Field, FieldOption


class PostAdminForm(forms.ModelForm):
    json = JSONFormField(widget=JSONField(attrs={'_model': 'Post'}), required=False)

    multiupload_link = '/fileupload/image/'

    def __init__(self, *args, **kwargs):
        super(PostAdminForm, self).__init__(*args, **kwargs)

        for field in Field.objects.filter(
            application__contains=self._meta.model.__name__):
            for fo in FieldOption.objects.filter(field=field):
                self.fields[
                    'json_{}_{}'.format(
                        field.slug, fo.option.slug
                    )] = forms.CharField(required=False)

    class Meta:
        model = Post
        widgets = {'content': OppsEditor()}


class AlbumAdminForm(forms.ModelForm):

    multiupload_link = '/fileupload/image/'

    class Meta:
        model = Album
        widgets = {
            'headline': OppsEditor()
        }


class LinkAdminForm(forms.ModelForm):
    class Meta:
        model = Link

## Instruction:
Extend all admin form to Container Admin Form (json field)
## Code After:
from opps.core.widgets import OppsEditor
from opps.containers.forms import ContainerAdminForm

from .models import Post, Album, Link


class PostAdminForm(ContainerAdminForm):
    multiupload_link = '/fileupload/image/'

    class Meta:
        model = Post
        widgets = {'content': OppsEditor()}


class AlbumAdminForm(ContainerAdminForm):
    multiupload_link = '/fileupload/image/'

    class Meta:
        model = Album
        widgets = {
            'headline': OppsEditor()
        }


class LinkAdminForm(ContainerAdminForm):
    class Meta:
        model = Link
