from django import forms
from django.conf import settings

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


class ContainerAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ContainerAdminForm, self).__init__(*args, **kwargs)

        self.fields['json'] = JSONFormField(
            widget=JSONField(
                attrs={'_model': self._meta.model.__name__}),
            required=False)
        for field in Field.objects.filter(application__contains=
                                          self._meta.model.__name__):
            if field.type == 'checkbox':
                for fo in FieldOption.objects.filter(field=field):
                    self.fields[
                        'json_{}_{}'.format(
                            field.slug, fo.option.slug
                        )] = forms.CharField(required=False)
            else:
                self.fields[
                    'json_{}'.format(field.slug)
                ] = forms.CharField(required=False)

        if settings.OPPS_MIRROR_CHANNEL:
            self.field['mirror_channel'] = forms.CharField(
                widget=forms.HiddenInput(), required=False)

from django import forms
from django.conf import settings

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


class ContainerAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ContainerAdminForm, self).__init__(*args, **kwargs)

        if not settings.OPPS_MIRROR_CHANNEL:
            self.fields['mirror_channel'].widget = forms.HiddenInput()

        self.fields['json'] = JSONFormField(
            widget=JSONField(
                attrs={'_model': self._meta.model.__name__}),
            required=False)
        for field in Field.objects.filter(application__contains=
                                          self._meta.model.__name__):
            if field.type == 'checkbox':
                for fo in FieldOption.objects.filter(field=field):
                    self.fields[
                        'json_{}_{}'.format(
                            field.slug, fo.option.slug
                        )] = forms.CharField(required=False)
            else:
                self.fields[
                    'json_{}'.format(field.slug)
                ] = forms.CharField(required=False)

Fix mirror_channel widget on OPPS_MIRROR_CHANNEL false
Fix mirror_channel widget on OPPS_MIRROR_CHANNEL false

  from django import forms
  from django.conf import settings
  
  from opps.db.models.fields.jsonf import JSONFormField
  from opps.fields.widgets import JSONField
  from opps.fields.models import Field, FieldOption
  
  
  class ContainerAdminForm(forms.ModelForm):
      def __init__(self, *args, **kwargs):
          super(ContainerAdminForm, self).__init__(*args, **kwargs)
+ 
+         if not settings.OPPS_MIRROR_CHANNEL:
+             self.fields['mirror_channel'].widget = forms.HiddenInput()
  
          self.fields['json'] = JSONFormField(
              widget=JSONField(
                  attrs={'_model': self._meta.model.__name__}),
              required=False)
          for field in Field.objects.filter(application__contains=
                                            self._meta.model.__name__):
              if field.type == 'checkbox':
                  for fo in FieldOption.objects.filter(field=field):
                      self.fields[
                          'json_{}_{}'.format(
                              field.slug, fo.option.slug
                          )] = forms.CharField(required=False)
              else:
                  self.fields[
                      'json_{}'.format(field.slug)
                  ] = forms.CharField(required=False)
  
-         if settings.OPPS_MIRROR_CHANNEL:
-             self.field['mirror_channel'] = forms.CharField(
-                 widget=forms.HiddenInput(), required=False)
- 
Fix mirror_channel widget on OPPS_MIRROR_CHANNEL false
## Code Before:
from django import forms
from django.conf import settings

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


class ContainerAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ContainerAdminForm, self).__init__(*args, **kwargs)

        self.fields['json'] = JSONFormField(
            widget=JSONField(
                attrs={'_model': self._meta.model.__name__}),
            required=False)
        for field in Field.objects.filter(application__contains=
                                          self._meta.model.__name__):
            if field.type == 'checkbox':
                for fo in FieldOption.objects.filter(field=field):
                    self.fields[
                        'json_{}_{}'.format(
                            field.slug, fo.option.slug
                        )] = forms.CharField(required=False)
            else:
                self.fields[
                    'json_{}'.format(field.slug)
                ] = forms.CharField(required=False)

        if settings.OPPS_MIRROR_CHANNEL:
            self.field['mirror_channel'] = forms.CharField(
                widget=forms.HiddenInput(), required=False)

## Instruction:
Fix mirror_channel widget on OPPS_MIRROR_CHANNEL false
## Code After:
from django import forms
from django.conf import settings

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


class ContainerAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ContainerAdminForm, self).__init__(*args, **kwargs)

        if not settings.OPPS_MIRROR_CHANNEL:
            self.fields['mirror_channel'].widget = forms.HiddenInput()

        self.fields['json'] = JSONFormField(
            widget=JSONField(
                attrs={'_model': self._meta.model.__name__}),
            required=False)
        for field in Field.objects.filter(application__contains=
                                          self._meta.model.__name__):
            if field.type == 'checkbox':
                for fo in FieldOption.objects.filter(field=field):
                    self.fields[
                        'json_{}_{}'.format(
                            field.slug, fo.option.slug
                        )] = forms.CharField(required=False)
            else:
                self.fields[
                    'json_{}'.format(field.slug)
                ] = forms.CharField(required=False)
