Created
April 4, 2018 12:19
-
-
Save krikunts/9b09477960846d407f7b170b6ea22788 to your computer and use it in GitHub Desktop.
fill_check
This file contains hidden or 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
| def fill_checker(cls, **kwargs): | |
| return type(cls.__name__, (cls,), kwargs) | |
| class AbstractSectionFillChecker: | |
| FILL_STATUS_EMPTY = "EMPTY" | |
| FILL_STATUS_FILLING_STARTED = "FILLING_STARTED" | |
| FILL_STATUS_FILLED = "FILLED" | |
| ST_INSTANCE = "WORK_PROGRAM" | |
| ST_TEMPLATE = "TEMPLATE" | |
| def __init__(self, section): | |
| self.section = section | |
| if section and section.work_program is not None: | |
| self.section_type = self.ST_INSTANCE | |
| elif section and section.work_program_template is not None: | |
| self.section_type = self.ST_TEMPLATE | |
| else: | |
| self.section_type = None | |
| @property | |
| def content(self): | |
| return self.section.content | |
| def _check(self, content): | |
| raise NotImplemented() | |
| def check(self): | |
| return self._check(self.content) | |
| class SimpleSectionFillChecker(AbstractSectionFillChecker): | |
| min_length = 1 | |
| min_list_length = 1 | |
| def get_result(self, values): | |
| values = tuple(values) | |
| if all(value == self.FILL_STATUS_FILLED for value in values): | |
| return self.FILL_STATUS_FILLED | |
| elif any(value in (self.FILL_STATUS_FILLED, self.FILL_STATUS_FILLING_STARTED) for value in values): | |
| return self.FILL_STATUS_FILLING_STARTED | |
| else: | |
| return self.FILL_STATUS_EMPTY | |
| def _check(self, content): | |
| if not self.section_type: | |
| return None | |
| if not content and type(content) != bool: | |
| return self.FILL_STATUS_EMPTY | |
| if isinstance(content, dict): | |
| return self.get_result(self._check(c) for c in content.values()) | |
| elif isinstance(content, list): | |
| result = self.get_result(self._check(c) for c in content) | |
| if len(content) < self.min_list_length and \ | |
| result in (self.FILL_STATUS_FILLED, self.FILL_STATUS_FILLING_STARTED): | |
| return self.FILL_STATUS_FILLING_STARTED | |
| return result | |
| elif isinstance(content, str) and len(content) < self.min_length: | |
| return self.FILL_STATUS_FILLING_STARTED | |
| else: | |
| return self.FILL_STATUS_FILLED | |
| class InstanceOnlySectionFillChecker(SimpleSectionFillChecker): | |
| def _check(self, content): | |
| if self.section_type != self.ST_INSTANCE: | |
| return None | |
| return super()._check(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment