import warnings

from django.contrib import admin

from .forms import TranslationFormSet
from . import settings


class TranslationStackedInline(admin.StackedInline):
    def __init__(self, *args, **kwargs):
        super(TranslationStackedInline, self).__init__(*args, **kwargs)

        if settings.AUTO_HIDE_LANGUAGE:
            self.exclude = ('language_code', )
            self.can_delete = False

    extra = 1
    formset = TranslationFormSet
    max_num = len(settings.LANGUAGES)


class TranslationTabularInline(admin.TabularInline):
    def __init__(self, *args, **kwargs):
        super(TranslationTabularInline, self).__init__(*args, **kwargs)

        if settings.AUTO_HIDE_LANGUAGE:
            self.exclude = ('language_code', )
            self.can_delete = False

    extra = 1
    formset = TranslationFormSet
    max_num = len(settings.LANGUAGES)


class TranslationInline(TranslationStackedInline):
    def __init__(self, *args, **kwargs):
        warnings.warn(DeprecationWarning(
            "TranslationInline is deprecated; "
            "use TranslationStackedInline or TranslationTabularInline instead."
        ))

        return super(TranslationInline, self).__init__(*args, **kwargs)

import warnings

from django.contrib import admin

from .forms import TranslationFormSet
from . import settings


class TranslationInlineMixin(object):
    def __init__(self, *args, **kwargs):
        super(TranslationInlineMixin, self).__init__(*args, **kwargs)

        if settings.AUTO_HIDE_LANGUAGE:
            self.exclude = ('language_code', )
            self.can_delete = False

    extra = 1
    formset = TranslationFormSet
    max_num = len(settings.LANGUAGES)


class TranslationStackedInline(TranslationInlineMixin, admin.StackedInline):
    pass


class TranslationTabularInline(TranslationInlineMixin, admin.TabularInline):
    pass


class TranslationInline(TranslationStackedInline):
    def __init__(self, *args, **kwargs):
        warnings.warn(DeprecationWarning(
            "TranslationInline is deprecated; "
            "use TranslationStackedInline or TranslationTabularInline instead."
        ))

        return super(TranslationInline, self).__init__(*args, **kwargs)

Use a Mixin for Admin inlines; less code duplication.
Use a Mixin for Admin inlines; less code duplication.

  import warnings
  
  from django.contrib import admin
  
  from .forms import TranslationFormSet
  from . import settings
  
  
- class TranslationStackedInline(admin.StackedInline):
+ class TranslationInlineMixin(object):
      def __init__(self, *args, **kwargs):
-         super(TranslationStackedInline, self).__init__(*args, **kwargs)
+         super(TranslationInlineMixin, self).__init__(*args, **kwargs)
  
          if settings.AUTO_HIDE_LANGUAGE:
              self.exclude = ('language_code', )
              self.can_delete = False
  
      extra = 1
      formset = TranslationFormSet
      max_num = len(settings.LANGUAGES)
  
  
+ class TranslationStackedInline(TranslationInlineMixin, admin.StackedInline):
+     pass
- class TranslationTabularInline(admin.TabularInline):
-     def __init__(self, *args, **kwargs):
-         super(TranslationTabularInline, self).__init__(*args, **kwargs)
  
-         if settings.AUTO_HIDE_LANGUAGE:
-             self.exclude = ('language_code', )
-             self.can_delete = False
  
+ class TranslationTabularInline(TranslationInlineMixin, admin.TabularInline):
+     pass
-     extra = 1
-     formset = TranslationFormSet
-     max_num = len(settings.LANGUAGES)
  
  
  class TranslationInline(TranslationStackedInline):
      def __init__(self, *args, **kwargs):
          warnings.warn(DeprecationWarning(
              "TranslationInline is deprecated; "
              "use TranslationStackedInline or TranslationTabularInline instead."
          ))
  
          return super(TranslationInline, self).__init__(*args, **kwargs)
  
Use a Mixin for Admin inlines; less code duplication.
## Code Before:
import warnings

from django.contrib import admin

from .forms import TranslationFormSet
from . import settings


class TranslationStackedInline(admin.StackedInline):
    def __init__(self, *args, **kwargs):
        super(TranslationStackedInline, self).__init__(*args, **kwargs)

        if settings.AUTO_HIDE_LANGUAGE:
            self.exclude = ('language_code', )
            self.can_delete = False

    extra = 1
    formset = TranslationFormSet
    max_num = len(settings.LANGUAGES)


class TranslationTabularInline(admin.TabularInline):
    def __init__(self, *args, **kwargs):
        super(TranslationTabularInline, self).__init__(*args, **kwargs)

        if settings.AUTO_HIDE_LANGUAGE:
            self.exclude = ('language_code', )
            self.can_delete = False

    extra = 1
    formset = TranslationFormSet
    max_num = len(settings.LANGUAGES)


class TranslationInline(TranslationStackedInline):
    def __init__(self, *args, **kwargs):
        warnings.warn(DeprecationWarning(
            "TranslationInline is deprecated; "
            "use TranslationStackedInline or TranslationTabularInline instead."
        ))

        return super(TranslationInline, self).__init__(*args, **kwargs)

## Instruction:
Use a Mixin for Admin inlines; less code duplication.
## Code After:
import warnings

from django.contrib import admin

from .forms import TranslationFormSet
from . import settings


class TranslationInlineMixin(object):
    def __init__(self, *args, **kwargs):
        super(TranslationInlineMixin, self).__init__(*args, **kwargs)

        if settings.AUTO_HIDE_LANGUAGE:
            self.exclude = ('language_code', )
            self.can_delete = False

    extra = 1
    formset = TranslationFormSet
    max_num = len(settings.LANGUAGES)


class TranslationStackedInline(TranslationInlineMixin, admin.StackedInline):
    pass


class TranslationTabularInline(TranslationInlineMixin, admin.TabularInline):
    pass


class TranslationInline(TranslationStackedInline):
    def __init__(self, *args, **kwargs):
        warnings.warn(DeprecationWarning(
            "TranslationInline is deprecated; "
            "use TranslationStackedInline or TranslationTabularInline instead."
        ))

        return super(TranslationInline, self).__init__(*args, **kwargs)
