# Path: mqueue/tests/base.py
# class MqueueBaseTest(TestCase):
#     user: User
# 
#     def setUp(self):
#         self.factory = RequestFactory()
#         self.maxDiff = None
#         self.user = User.objects.create_user(  # type: ignore
#             "myuser", "myemail@test.com", "super_password"
#         )
# 
#     def reset(self):
#         for event in MEvent.objects.all():
#             event.delete()
# 
#     def create_mevent(
#         self,
#         name: str = "M event",
#         url: str = "/",
#         obj_pk: int = 1,
#         notes: str = "Notes",
#     ):
#         self._content_type = ContentType.objects.get_for_model(User)
#         return MEvent.objects.create(
#             name=name,
#             url=url,
#             obj_pk=obj_pk,
#             notes=notes,
#             model=User,
#             content_type=self._content_type,
#         )
# 
# 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/admin.py
# def link_to_object(obj: MEvent) -> SafeText:
#     return format_html(  # type: ignore
#         f'<a href="{obj.url}" target="_blank">{obj.url}</a>'
#     )
# 
# def link_to_object_admin(obj: MEvent) -> SafeText:
#     return format_html(  # type: ignore
#         f'<a href="{obj.admin_url}" target="_blank">{obj.admin_url}</a>'
#     )
# 
# class MEventAdmin(admin.ModelAdmin):
#     date_hierarchy = "date_posted"
#     readonly_fields = ["date_posted", "request"]
#     list_display: List[Any] = [
#         event,
#         "name",
#         "date_posted",
#         "bucket",
#         "user",
#         link_to_object,
#         link_to_object_admin,
#         "scope",
#     ]
#     list_filter = (
#         "event_class",
#         ("content_type", admin.RelatedOnlyFieldListFilter),
#         ("user", admin.RelatedOnlyFieldListFilter),
#         "scope",
#     )
#     search_fields = ["name", "user__username", "event_class", "bucket"]
#     link_to_object.allow_tags = True
#     link_to_object.short_description = _("See on site")
#     link_to_object_admin.allow_tags = True
#     link_to_object_admin.short_description = _("See in admin")
#     format_event_class.allow_tags = True
#     format_event_class.short_description = _("Class")
#     filters_on_top = True
#     list_select_related = (
#         "user",
#         "content_type",
#     )
# 
#     class Media:
#         css = {
#             "all": (
#                 "mqueue/mqueue.css",
#                 "https://cdn.jsdelivr.net/npm/font-awesome@4.7.0"
#                 + "/css/font-awesome.min.css",
#             )
#         }
# 
#     def get_readonly_fields(
#         self, request: HttpRequest, obj: Union[MEvent, None] = None
#     ):
#         super(MEventAdmin, self).get_readonly_fields(request, obj)
#         return ("notes", "request")


from django.contrib import admin
from .base import MqueueBaseTest
from mqueue.models import MEvent
from mqueue.admin import link_to_object, link_to_object_admin, MEventAdmin


class MqueueTestAdmin(MqueueBaseTest):
    def test_admin(self):
        instance, _ = MEvent.objects.get_or_create(
            name="Event name", url="http://url", admin_url="http://admin_url"
        )
        res = link_to_object(instance)
        link = '<a href="http://url" target="_blank">http://url</a>'
        self.assertEqual(link, res)
        res = link_to_object_admin(instance)
        link = '<a href="http://admin_url" target="_blank">http://admin_url</a>'
        self.assertEqual(link, res)
        request = self.factory.get("/")

        class TestAdminSite(admin.AdminSite):
            pass


# Path: mqueue/tests/base.py
# class MqueueBaseTest(TestCase):
#     user: User
# 
#     def setUp(self):
#         self.factory = RequestFactory()
#         self.maxDiff = None
#         self.user = User.objects.create_user(  # type: ignore
#             "myuser", "myemail@test.com", "super_password"
#         )
# 
#     def reset(self):
#         for event in MEvent.objects.all():
#             event.delete()
# 
#     def create_mevent(
#         self,
#         name: str = "M event",
#         url: str = "/",
#         obj_pk: int = 1,
#         notes: str = "Notes",
#     ):
#         self._content_type = ContentType.objects.get_for_model(User)
#         return MEvent.objects.create(
#             name=name,
#             url=url,
#             obj_pk=obj_pk,
#             notes=notes,
#             model=User,
#             content_type=self._content_type,
#         )
# 
# 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/admin.py
# def link_to_object(obj: MEvent) -> SafeText:
#     return format_html(  # type: ignore
#         f'<a href="{obj.url}" target="_blank">{obj.url}</a>'
#     )
# 
# def link_to_object_admin(obj: MEvent) -> SafeText:
#     return format_html(  # type: ignore
#         f'<a href="{obj.admin_url}" target="_blank">{obj.admin_url}</a>'
#     )
# 
# class MEventAdmin(admin.ModelAdmin):
#     date_hierarchy = "date_posted"
#     readonly_fields = ["date_posted", "request"]
#     list_display: List[Any] = [
#         event,
#         "name",
#         "date_posted",
#         "bucket",
#         "user",
#         link_to_object,
#         link_to_object_admin,
#         "scope",
#     ]
#     list_filter = (
#         "event_class",
#         ("content_type", admin.RelatedOnlyFieldListFilter),
#         ("user", admin.RelatedOnlyFieldListFilter),
#         "scope",
#     )
#     search_fields = ["name", "user__username", "event_class", "bucket"]
#     link_to_object.allow_tags = True
#     link_to_object.short_description = _("See on site")
#     link_to_object_admin.allow_tags = True
#     link_to_object_admin.short_description = _("See in admin")
#     format_event_class.allow_tags = True
#     format_event_class.short_description = _("Class")
#     filters_on_top = True
#     list_select_related = (
#         "user",
#         "content_type",
#     )
# 
#     class Media:
#         css = {
#             "all": (
#                 "mqueue/mqueue.css",
#                 "https://cdn.jsdelivr.net/npm/font-awesome@4.7.0"
#                 + "/css/font-awesome.min.css",
#             )
#         }
# 
#     def get_readonly_fields(
#         self, request: HttpRequest, obj: Union[MEvent, None] = None
#     ):
#         super(MEventAdmin, self).get_readonly_fields(request, obj)
#         return ("notes", "request")


# Path: mqueue/tests/test_admin.py
from django.contrib import admin
from .base import MqueueBaseTest
from mqueue.models import MEvent
from mqueue.admin import link_to_object, link_to_object_admin, MEventAdmin



class MqueueTestAdmin(MqueueBaseTest):
    def test_admin(self):
        instance, _ = MEvent.objects.get_or_create(
            name="Event name", url="http://url", admin_url="http://admin_url"
        )
        res = link_to_object(instance)
        link = '<a href="http://url" target="_blank">http://url</a>'
        self.assertEqual(link, res)
        res = link_to_object_admin(instance)
        link = '<a href="http://admin_url" target="_blank">http://admin_url</a>'
        self.assertEqual(link, res)
        request = self.factory.get("/")

        class TestAdminSite(admin.AdminSite):
            pass


        adm = MEventAdmin(MEvent, TestAdminSite)