# Path: dash/orgs/views.py
# class OrgObjPermsMixin(OrgPermsMixin):
#     def get_object_org(self):
#         return self.get_object().org
# 
#     def has_org_perm(self, codename):
#         has_org_perm = super(OrgObjPermsMixin, self).has_org_perm(codename)
# 
#         if has_org_perm:
#             user = self.get_user()
# 
#             if user.is_anonymous:
#                 return True
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         has_perm = super(OrgObjPermsMixin, self).has_permission(request, *args, **kwargs)
# 
#         if has_perm:
#             user = self.get_user()
# 
#             # user has global permission
#             if user.has_perm(self.permission):
#                 return True
# 
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
# class OrgPermsMixin(object):
#     """
#     Get the organisation and the user within the inheriting view so that it be
#     come easy to decide whether this user has a certain permission for that
#     particular organization to perform the view's actions
#     """
# 
#     def get_user(self):
#         return self.request.user
# 
#     def derive_org(self):
#         return self.request.org
# 
#     def pre_process(self, request, *args, **kwargs):
#         user = self.get_user()
#         org = self.derive_org()
# 
#         if user.is_superuser:
#             return None
# 
#         if not org:
#             return HttpResponseRedirect(reverse("orgs.org_choose"))
# 
#         return None
# 
#     def has_org_perm(self, permission):
#         (app_label, codename) = permission.split(".")
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().is_anonymous:
#             return False
# 
#         if self.org:
#             org_group = self.get_user().get_org_group()
#             if org_group:
#                 if org_group.permissions.filter(content_type__app_label=app_label, codename=codename).exists():
#                     return True
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         """
#         Figures out if the current user has permissions for this view.
#         """
#         self.kwargs = kwargs
#         self.args = args
#         self.request = request
#         self.org = self.derive_org()
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().has_perm(self.permission):
#             return True
# 
#         return self.has_org_perm(self.permission)
# 
# Path: dash/tags/models.py
# class Tag(SmartModel):
# 
#     name = models.CharField(max_length=64, help_text=_("The name of this tag"))
#     org = models.ForeignKey(Org, on_delete=models.PROTECT)
# 
#     def __str__(self) -> str:
#         return self.name
# 
#     class Meta:
#         unique_together = ("name", "org")


from smartmin.views import SmartCreateView, SmartCRUDL, SmartDeleteView, SmartListView, SmartUpdateView
from django.db.models.functions import Lower
from django.utils.translation import gettext_lazy as _
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from dash.tags.models import Tag


class TagCRUDL(SmartCRUDL):
    model = Tag
    actions = ("create", "list", "update", "delete")

    class Create(OrgPermsMixin, SmartCreateView):
        fields = ("name",)

        def pre_save(self, obj):
            obj = super(TagCRUDL.Create, self).pre_save(obj)

            org = self.derive_org()
            obj.org = org

            return obj

    class Update(OrgObjPermsMixin, SmartUpdateView):
        fields = ("name",)
        delete_url = "id@tags.tag_delete"

    class List(OrgPermsMixin, SmartListView):
        fields = ("name", "modified_on", "created_on")

        def get_queryset(self, **kwargs):
            queryset = super(TagCRUDL.List, self).get_queryset(**kwargs)
            queryset = queryset.filter(org=self.derive_org()).order_by(Lower("name"))

            return queryset


# Path: dash/orgs/views.py
# class OrgObjPermsMixin(OrgPermsMixin):
#     def get_object_org(self):
#         return self.get_object().org
# 
#     def has_org_perm(self, codename):
#         has_org_perm = super(OrgObjPermsMixin, self).has_org_perm(codename)
# 
#         if has_org_perm:
#             user = self.get_user()
# 
#             if user.is_anonymous:
#                 return True
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         has_perm = super(OrgObjPermsMixin, self).has_permission(request, *args, **kwargs)
# 
#         if has_perm:
#             user = self.get_user()
# 
#             # user has global permission
#             if user.has_perm(self.permission):
#                 return True
# 
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
# class OrgPermsMixin(object):
#     """
#     Get the organisation and the user within the inheriting view so that it be
#     come easy to decide whether this user has a certain permission for that
#     particular organization to perform the view's actions
#     """
# 
#     def get_user(self):
#         return self.request.user
# 
#     def derive_org(self):
#         return self.request.org
# 
#     def pre_process(self, request, *args, **kwargs):
#         user = self.get_user()
#         org = self.derive_org()
# 
#         if user.is_superuser:
#             return None
# 
#         if not org:
#             return HttpResponseRedirect(reverse("orgs.org_choose"))
# 
#         return None
# 
#     def has_org_perm(self, permission):
#         (app_label, codename) = permission.split(".")
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().is_anonymous:
#             return False
# 
#         if self.org:
#             org_group = self.get_user().get_org_group()
#             if org_group:
#                 if org_group.permissions.filter(content_type__app_label=app_label, codename=codename).exists():
#                     return True
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         """
#         Figures out if the current user has permissions for this view.
#         """
#         self.kwargs = kwargs
#         self.args = args
#         self.request = request
#         self.org = self.derive_org()
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().has_perm(self.permission):
#             return True
# 
#         return self.has_org_perm(self.permission)
# 
# Path: dash/tags/models.py
# class Tag(SmartModel):
# 
#     name = models.CharField(max_length=64, help_text=_("The name of this tag"))
#     org = models.ForeignKey(Org, on_delete=models.PROTECT)
# 
#     def __str__(self) -> str:
#         return self.name
# 
#     class Meta:
#         unique_together = ("name", "org")


# Path: dash/tags/views.py
from smartmin.views import SmartCreateView, SmartCRUDL, SmartDeleteView, SmartListView, SmartUpdateView
from django.db.models.functions import Lower
from django.utils.translation import gettext_lazy as _
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from dash.tags.models import Tag



class TagCRUDL(SmartCRUDL):
    model = Tag
    actions = ("create", "list", "update", "delete")

    class Create(OrgPermsMixin, SmartCreateView):
        fields = ("name",)

        def pre_save(self, obj):
            obj = super(TagCRUDL.Create, self).pre_save(obj)

            org = self.derive_org()
            obj.org = org

            return obj

    class Update(OrgObjPermsMixin, SmartUpdateView):
        fields = ("name",)
        delete_url = "id@tags.tag_delete"

    class List(OrgPermsMixin, SmartListView):
        fields = ("name", "modified_on", "created_on")

        def get_queryset(self, **kwargs):
            queryset = super(TagCRUDL.List, self).get_queryset(**kwargs)
            queryset = queryset.filter(org=self.derive_org()).order_by(Lower("name"))

            return queryset


    class Delete(OrgObjPermsMixin, SmartDeleteView):