admin: remove redundant code
This commit is contained in:
parent
c42ed6bc99
commit
d5f6714ed7
|
@ -12,17 +12,10 @@ from django.views.generic import DeleteView, ListView, UpdateView
|
||||||
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
||||||
|
|
||||||
from passbook.core.models import PropertyMapping
|
from passbook.core.models import PropertyMapping
|
||||||
from passbook.lib.utils.reflection import path_to_class
|
from passbook.lib.utils.reflection import all_subclasses, path_to_class
|
||||||
from passbook.lib.views import CreateAssignPermView
|
from passbook.lib.views import CreateAssignPermView
|
||||||
|
|
||||||
|
|
||||||
def all_subclasses(cls):
|
|
||||||
"""Recursively return all subclassess of cls"""
|
|
||||||
return set(cls.__subclasses__()).union(
|
|
||||||
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class PropertyMappingListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
class PropertyMappingListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
||||||
"""Show list of all property_mappings"""
|
"""Show list of all property_mappings"""
|
||||||
|
|
||||||
|
|
|
@ -12,17 +12,10 @@ from django.views.generic import DeleteView, ListView, UpdateView
|
||||||
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
||||||
|
|
||||||
from passbook.core.models import Source
|
from passbook.core.models import Source
|
||||||
from passbook.lib.utils.reflection import path_to_class
|
from passbook.lib.utils.reflection import all_subclasses, path_to_class
|
||||||
from passbook.lib.views import CreateAssignPermView
|
from passbook.lib.views import CreateAssignPermView
|
||||||
|
|
||||||
|
|
||||||
def all_subclasses(cls):
|
|
||||||
"""Recursively return all subclassess of cls"""
|
|
||||||
return set(cls.__subclasses__()).union(
|
|
||||||
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class SourceListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
class SourceListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
||||||
"""Show list of all sources"""
|
"""Show list of all sources"""
|
||||||
|
|
||||||
|
|
|
@ -12,17 +12,10 @@ from django.views.generic import DeleteView, ListView, UpdateView
|
||||||
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
||||||
|
|
||||||
from passbook.flows.models import Stage
|
from passbook.flows.models import Stage
|
||||||
from passbook.lib.utils.reflection import path_to_class
|
from passbook.lib.utils.reflection import all_subclasses, path_to_class
|
||||||
from passbook.lib.views import CreateAssignPermView
|
from passbook.lib.views import CreateAssignPermView
|
||||||
|
|
||||||
|
|
||||||
def all_subclasses(cls):
|
|
||||||
"""Recursively return all subclassess of cls"""
|
|
||||||
return set(cls.__subclasses__()).union(
|
|
||||||
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class StageListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
class StageListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
||||||
"""Show list of all stages"""
|
"""Show list of all stages"""
|
||||||
|
|
||||||
|
@ -34,8 +27,7 @@ class StageListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs["types"] = {
|
kwargs["types"] = {
|
||||||
x.__name__: x._meta.verbose_name
|
x.__name__: x._meta.verbose_name for x in all_subclasses(Stage)
|
||||||
for x in sorted(all_subclasses(Stage), key=lambda x: x.__name__)
|
|
||||||
}
|
}
|
||||||
return super().get_context_data(**kwargs)
|
return super().get_context_data(**kwargs)
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,19 @@
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
|
|
||||||
|
def all_subclasses(cls, sort=True):
|
||||||
|
"""Recursively return all subclassess of cls"""
|
||||||
|
classes = set(cls.__subclasses__()).union(
|
||||||
|
[s for c in cls.__subclasses__() for s in all_subclasses(c, sort=sort)]
|
||||||
|
)
|
||||||
|
if sort:
|
||||||
|
return sorted(classes, key=lambda x: x.__name__)
|
||||||
|
return classes
|
||||||
|
|
||||||
|
|
||||||
def class_to_path(cls):
|
def class_to_path(cls):
|
||||||
"""Turn Class (Class or instance) into module path"""
|
"""Turn Class (Class or instance) into module path"""
|
||||||
return "%s.%s" % (cls.__module__, cls.__name__)
|
return f"{cls.__module__}.{cls.__name__}"
|
||||||
|
|
||||||
|
|
||||||
def path_to_class(path):
|
def path_to_class(path):
|
||||||
|
|
Reference in New Issue