# Path: mqueue/models.py
# class MEvent(models.Model):
#     # required fields
#     content_type = models.ForeignKey(
#         ContentType,
#         null=True,
#         blank=True,
#         verbose_name=_("Content type"),
#         on_delete=SET_NULL,
#     )
#     obj_pk = models.IntegerField(
#         blank=True, null=True, verbose_name=_("Object primary key")
#     )
#     name = models.CharField(max_length=120, verbose_name=_("Name"))
#     # content fields
#     url = models.CharField(max_length=255, blank=True, verbose_name=_("Url"))
#     admin_url = models.CharField(
#         max_length=255, blank=True, verbose_name=_("Admin url")
#     )
#     notes = models.TextField(blank=True)
#     # meta
#     date_posted = models.DateTimeField(auto_now_add=True, verbose_name=_("Date posted"))
#     event_class = models.CharField(max_length=120, blank=True, verbose_name=_("Class"))
#     user = models.ForeignKey(
#         USER_MODEL,
#         null=True,
#         blank=True,
#         related_name="+",
#         on_delete=models.SET_NULL,
#         verbose_name=_("User"),
#     )
#     groups = models.ManyToManyField(
#         Group,
#         blank=True,
#         verbose_name=_("Groups"),
#     )
#     request = models.TextField(blank=True, verbose_name=_("Request"))
#     bucket = models.CharField(max_length=60, blank=True, verbose_name=_("Bucket"))
#     data = models.TextField(blank=True, verbose_name=_("Data"))
#     # manager
#     scope = models.CharField(
#         max_length=18,
#         choices=SCOPE,
#         default=SCOPE[0][0],
#         verbose_name=_("Scope"),
#     )
#     objects = MEventManager()
# 
#     class Meta:  # type: ignore
#         app_label = "mqueue"
#         verbose_name = _("Event")
#         verbose_name_plural = _("Events")
#         ordering = ["-date_posted"]
#         # permissions = (("view_mevent", "Can see Events"),)
# 
#     def __str__(self) -> str:
#         return f"{self.name} - {self.date_posted}"
# 
# Path: mqueue/utils.py
# def get_user(instance: Model) -> Union[User, None]:
#     user: Union[User, None] = None
#     try:
#         user = instance.user  # type: ignore
#     except Exception:
#         try:
#             user = instance.editor  # type: ignore
#         except Exception:
#             pass
#     return user
# 
# def get_url(instance: Model) -> str:
#     url = ""
#     get_event_object_url = getattr(instance.__class__, "get_event_object_url", None)
#     if callable(get_event_object_url):
#         url = instance.get_event_object_url()  # type: ignore
#         return url  # type: ignore
#     get_absolute_url = getattr(instance.__class__, "get_absolute_url", None)
#     if callable(get_absolute_url):
#         url = instance.get_absolute_url()  # type: ignore
#         return url  # type: ignore
#     return ""
# 
# def get_admin_url(instance: Model) -> str:
#     admin_url = reverse(
#         "admin:%s_%s_change"
#         % (instance._meta.app_label, instance._meta.model_name),  # type: ignore
#         args=[instance.id],  # type: ignore
#     )
#     return admin_url
# 
# def get_object_name(instance: Model, user: User) -> str:
#     obj_name: str = ""
#     try:
#         obj_name = instance.__str__()  # type: ignore
#     except AttributeError:
#         try:
#             obj_name = instance.name  # type: ignore
#         except Exception:
#             try:
#                 obj_name = instance.title  # type: ignore
#             except Exception:
#                 try:
#                     obj_name = instance.slug  # type: ignore
#                 except Exception:
#                     obj_name = str(instance.pk)
#     if obj_name:
#         if len(obj_name) >= 45:
#             obj_name = obj_name[:45] + "..."
#     if hasattr(instance, "date_posted"):
#         obj_name = instance.__class__.__name__ + " - "
#         obj_name += str(instance.date_posted)  # type: ignore
#     elif hasattr(instance, "created"):
#         obj_name = instance.__class__.__name__ + " - "
#         obj_name += (str(instance.created),)  # type: ignore
#     if user:
#         obj_name += f" ({user.username})"
#     return obj_name


from .models import MEvent
from .utils import get_user, get_url, get_admin_url, get_object_name
            evt: MEvent = instance.event(evt, "create")
            evt.save()
    return


def mmessage_delete(sender, instance, **kwargs):  # type: ignore
    # try to get the user
    user = get_user(instance)
    # try to get the object name
    obj_name = get_object_name(instance, user)  # type: ignore
    event_class: str = instance.__class__.__name__ + " deleted"
    has_event_method = getattr(instance, "event", None)
    # create event
    evt = MEvent.objects.create(
        model=instance.__class__,
        name=obj_name,
        obj_pk=instance.pk,
        user=user,
        event_class=event_class,
        commit=not has_event_method,
    )
    if has_event_method:
        evt: MEvent = instance.event(evt, "delete")
        evt.save()
    return


def mmessage_update(sender, instance, created, **kwargs):  # type: ignore
    if created is False:
        # try to get the user

# Path: mqueue/models.py
# class MEvent(models.Model):
#     # required fields
#     content_type = models.ForeignKey(
#         ContentType,
#         null=True,
#         blank=True,
#         verbose_name=_("Content type"),
#         on_delete=SET_NULL,
#     )
#     obj_pk = models.IntegerField(
#         blank=True, null=True, verbose_name=_("Object primary key")
#     )
#     name = models.CharField(max_length=120, verbose_name=_("Name"))
#     # content fields
#     url = models.CharField(max_length=255, blank=True, verbose_name=_("Url"))
#     admin_url = models.CharField(
#         max_length=255, blank=True, verbose_name=_("Admin url")
#     )
#     notes = models.TextField(blank=True)
#     # meta
#     date_posted = models.DateTimeField(auto_now_add=True, verbose_name=_("Date posted"))
#     event_class = models.CharField(max_length=120, blank=True, verbose_name=_("Class"))
#     user = models.ForeignKey(
#         USER_MODEL,
#         null=True,
#         blank=True,
#         related_name="+",
#         on_delete=models.SET_NULL,
#         verbose_name=_("User"),
#     )
#     groups = models.ManyToManyField(
#         Group,
#         blank=True,
#         verbose_name=_("Groups"),
#     )
#     request = models.TextField(blank=True, verbose_name=_("Request"))
#     bucket = models.CharField(max_length=60, blank=True, verbose_name=_("Bucket"))
#     data = models.TextField(blank=True, verbose_name=_("Data"))
#     # manager
#     scope = models.CharField(
#         max_length=18,
#         choices=SCOPE,
#         default=SCOPE[0][0],
#         verbose_name=_("Scope"),
#     )
#     objects = MEventManager()
# 
#     class Meta:  # type: ignore
#         app_label = "mqueue"
#         verbose_name = _("Event")
#         verbose_name_plural = _("Events")
#         ordering = ["-date_posted"]
#         # permissions = (("view_mevent", "Can see Events"),)
# 
#     def __str__(self) -> str:
#         return f"{self.name} - {self.date_posted}"
# 
# Path: mqueue/utils.py
# def get_user(instance: Model) -> Union[User, None]:
#     user: Union[User, None] = None
#     try:
#         user = instance.user  # type: ignore
#     except Exception:
#         try:
#             user = instance.editor  # type: ignore
#         except Exception:
#             pass
#     return user
# 
# def get_url(instance: Model) -> str:
#     url = ""
#     get_event_object_url = getattr(instance.__class__, "get_event_object_url", None)
#     if callable(get_event_object_url):
#         url = instance.get_event_object_url()  # type: ignore
#         return url  # type: ignore
#     get_absolute_url = getattr(instance.__class__, "get_absolute_url", None)
#     if callable(get_absolute_url):
#         url = instance.get_absolute_url()  # type: ignore
#         return url  # type: ignore
#     return ""
# 
# def get_admin_url(instance: Model) -> str:
#     admin_url = reverse(
#         "admin:%s_%s_change"
#         % (instance._meta.app_label, instance._meta.model_name),  # type: ignore
#         args=[instance.id],  # type: ignore
#     )
#     return admin_url
# 
# def get_object_name(instance: Model, user: User) -> str:
#     obj_name: str = ""
#     try:
#         obj_name = instance.__str__()  # type: ignore
#     except AttributeError:
#         try:
#             obj_name = instance.name  # type: ignore
#         except Exception:
#             try:
#                 obj_name = instance.title  # type: ignore
#             except Exception:
#                 try:
#                     obj_name = instance.slug  # type: ignore
#                 except Exception:
#                     obj_name = str(instance.pk)
#     if obj_name:
#         if len(obj_name) >= 45:
#             obj_name = obj_name[:45] + "..."
#     if hasattr(instance, "date_posted"):
#         obj_name = instance.__class__.__name__ + " - "
#         obj_name += str(instance.date_posted)  # type: ignore
#     elif hasattr(instance, "created"):
#         obj_name = instance.__class__.__name__ + " - "
#         obj_name += (str(instance.created),)  # type: ignore
#     if user:
#         obj_name += f" ({user.username})"
#     return obj_name


# Path: mqueue/signals.py
from .models import MEvent
from .utils import get_user, get_url, get_admin_url, get_object_name

            evt: MEvent = instance.event(evt, "create")
            evt.save()
    return


def mmessage_delete(sender, instance, **kwargs):  # type: ignore
    # try to get the user
    user = get_user(instance)
    # try to get the object name
    obj_name = get_object_name(instance, user)  # type: ignore
    event_class: str = instance.__class__.__name__ + " deleted"
    has_event_method = getattr(instance, "event", None)
    # create event
    evt = MEvent.objects.create(
        model=instance.__class__,
        name=obj_name,
        obj_pk=instance.pk,
        user=user,
        event_class=event_class,
        commit=not has_event_method,
    )
    if has_event_method:
        evt: MEvent = instance.event(evt, "delete")
        evt.save()
    return


def mmessage_update(sender, instance, created, **kwargs):  # type: ignore
    if created is False:
        # try to get the user

        user = get_user(instance)