from django.utils.translation import gettext as _
from django.db.models import BooleanField
from django.db.models.deletion import ProtectedError

class RequiredBooleanField(BooleanField):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

    self.blank = False

  # we need to skip BooleanField implementation of deconstruct, since it expects blank to be True
  def deconstruct(self):
    return super(BooleanField, self).deconstruct()

def PROTECT(collector, field, sub_objs, using):
  deleted_model     = field.remote_field.model
  referencing_model = sub_objs[0].__class__
  message           = _("Cannot delete some instances of model '%(deleted_model)s' because "
                        "they are referenced through a protected foreign key:"
                        "'%(referencing_model)s.%(referencing_field)s'")
  substitutions     = {
    'deleted_model':     deleted_model._meta.verbose_name.title(),
    'referencing_model': referencing_model._meta.verbose_name.title(),
    'referencing_field': field.verbose_name
  }

  raise ProtectedError(message % substitutions, sub_objs)
