Last active
August 29, 2017 08:06
-
-
Save slomo/44dc8999fc62ccb1a88017cab8248da5 to your computer and use it in GitHub Desktop.
What my ideal phases.py should look like
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from adhcoracy4.phases import register_module, ModuleType, PhaseType | |
from .models import MyModel | |
from .views import MyDashBoardView, MyCollectView | |
Collect(PhaseType): | |
description = _('Human readable phase name') | |
view = MyCollectView | |
features = { | |
'crud': (MyModel, 'myapp_name.MyOtherModel') # model as class or django style model string | |
} | |
# namespace for platform specifc properties | |
custom = { | |
'more_stuff', 'only_present_on_my_platform' | |
} | |
Rate(PhaseType): | |
description = _('Human readable phase name') | |
view = '.views.AwesomeRateView', # path relative to app directory | |
features = { | |
'rate': (MyModel), | |
} | |
IdeaCollection(ModuleType): | |
description = _('Human readable module type') | |
phase_types = ( | |
Rate, | |
Collect, | |
) | |
# namespace for platform specifc properties | |
custom = { | |
'specific_stuff': 'only_my_platform_has', | |
} | |
register_module(IdeaCollection) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
from django.apps import apps | |
_registered_modules = {} | |
_registered_phases = {} | |
class ModuleType(): | |
# could be better as property but requires meta class | |
@classmethod | |
def get_identifier(cls): | |
return apps.get_containing_app_config(cls.__module__).label | |
class PhaseType(): | |
_module_id = None | |
@classmethod | |
def name(cls): | |
return cls.__name__.lower() | |
@classmethod | |
def get_identifier(cls): | |
return '{module_id}:{name}'.format( | |
module_id=cls._module_id, | |
name=cls.name() | |
) | |
@classmethod | |
def get_module_type(cls): | |
return _registered_modules[cls._module_id] | |
def __str__(self): | |
return '{s.__class__.__name__} ({s.app}:{s.phase})'.format(s=self) | |
def has_feature(self, feature, model): | |
return model in self.features.get(feature, []) | |
def _register_phases(phase_type, module_id): | |
phase_type._module_id = module_id | |
_registerd_phases[phase_type.get_identifier()] = phase_type | |
def register_module(module_type): | |
_registered_modules[module_type.get_identifier()] = module_type | |
for phase in module_type.phase_types: | |
_register_phase(phase_type, module_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment