def validate_term_model(): """ Валидатор терминов для не Entity модели """ views_layouts_root = get_or_create_view_layouts_root() try: # page layout root pages_layouts_root = TermModel.objects.get( slug=PAGE_LAYOUT_ROOT_TERM_SLUG, parent=views_layouts_root) except TermModel.DoesNotExist: pages_layouts_root = TermModel( slug=PAGE_LAYOUT_ROOT_TERM_SLUG, parent=views_layouts_root, name=_('Page layout'), semantic_rule=TermModel.XOR_RULE, system_flags=_default_layout_system_flags_restriction) pages_layouts_root.save() for page_layout in PageLayout.objects.all(): try: # page layout TermModel.objects.get(slug=page_layout.key, parent=pages_layouts_root) except TermModel.DoesNotExist: layout = TermModel( slug=page_layout.key, parent=pages_layouts_root, name=page_layout.title, semantic_rule=TermModel.OR_RULE, system_flags=_default_layout_system_flags_restriction) layout.save()
def validate_terms(instance): """ Валидатор терминов представлений для конкретной инстанции """ system_flags = _default_layout_system_flags_restriction views_layouts = get_views_layouts() pages_layouts_root = views_layouts.get(PAGE_LAYOUT_ROOT_TERM_SLUG, None) if pages_layouts_root is not None: if instance.id is not None: try: origin = PageLayout.objects.get(pk=instance.id) except PageLayout.DoesNotExist: pass else: layout = views_layouts.get(origin.key, None) if layout is not None: layout.slug, layout.name = instance.key, instance.title layout.save() else: layout = TermModel(slug=instance.key, parent=pages_layouts_root, name=instance.title, semantic_rule=TermModel.OR_RULE, system_flags=system_flags) layout.save()
def validate_term_model(cls): """ RUS: Валидация модели терминов. Добавляет термины класса объекта в дерево согласно структуре наследования. """ view_root = get_or_create_view_layouts_root() try: # publication root TermModel.objects.get(slug=cls.LAYOUT_TERM_SLUG, parent=view_root) except TermModel.DoesNotExist: publication_root = TermModel( slug=cls.LAYOUT_TERM_SLUG, parent=view_root, name=_('Publication'), semantic_rule=TermModel.XOR_RULE, system_flags=_publication_root_terms_system_flags_restriction) publication_root.save() super(PublicationBase, cls).validate_term_model()
def validate_term_model(cls): """ RUS: Добавляет термин 'Категория пользователя' и 'Неизвестный пользователь' в модель терминов TermModel при их отсутствии. """ # валидируем только один раз key = 'vldt:сstmr_ctgr' need_validation = EntityModel._validate_term_model_cache.get(key, True) if need_validation: EntityModel._validate_term_model_cache[key] = False with transaction.atomic(): try: # customer-category customer_category = TermModel.objects.get( slug=cls.CUSTOMER_CATEGORY_ROOT_TERM_SLUG, parent=None) except TermModel.DoesNotExist: customer_category = TermModel( slug=cls.CUSTOMER_CATEGORY_ROOT_TERM_SLUG, parent=None, name=force_text(_('Customer category')), semantic_rule=TermModel.XOR_RULE, system_flags=( TermModel.system_flags.delete_restriction | TermModel.system_flags.change_parent_restriction | TermModel.system_flags.change_slug_restriction)) customer_category.save() with transaction.atomic(): try: # unknown-customer customer_category.get_descendants(include_self=False).get( slug=cls.UNKNOWN_CUSTOMER_TERM_SLUG) except TermModel.DoesNotExist: unknown_customer = TermModel( slug=cls.UNKNOWN_CUSTOMER_TERM_SLUG, parent_id=customer_category.id, name=force_text(_('Unknown customer')), semantic_rule=TermModel.OR_RULE, system_flags=( TermModel.system_flags.delete_restriction | TermModel.system_flags.change_parent_restriction | TermModel.system_flags.change_slug_restriction | TermModel.system_flags.has_child_restriction)) unknown_customer.save() super(CustomerCategoryMixin, cls).validate_term_model()
def validate_term_model(cls): """ RUS: Добавляет термин Регион и Другие регионы в модель терминов TermModel при их отсутствии. """ # валидируем только один раз key = 'vldt:place' need_validation = EntityModel._validate_term_model_cache.get(key, True) if need_validation: EntityModel._validate_term_model_cache[key] = False with transaction.atomic(): try: # region region = TermModel.objects.get( slug=cls.REGION_ROOT_TERM_SLUG, parent=None) except TermModel.DoesNotExist: region = TermModel( slug=cls.REGION_ROOT_TERM_SLUG, parent=None, name=force_text(_('Region')), semantic_rule=TermModel.XOR_RULE, system_flags=( TermModel.system_flags.delete_restriction | TermModel.system_flags.change_parent_restriction | TermModel.system_flags.change_slug_restriction)) region.save() with transaction.atomic(): try: # terra-incognita region.get_descendants(include_self=False).get( slug=cls.TERRA_INCOGNITA_TERM_SLUG) except TermModel.DoesNotExist: terra_incognita = TermModel( slug=cls.TERRA_INCOGNITA_TERM_SLUG, parent_id=region.id, name=force_text(_('Terra Incognita')), semantic_rule=TermModel.OR_RULE, system_flags=( TermModel.system_flags.delete_restriction | TermModel.system_flags.change_parent_restriction | TermModel.system_flags.change_slug_restriction | TermModel.system_flags.has_child_restriction)) terra_incognita.save() super(PlaceMixin, cls).validate_term_model()
def get_or_create_term_wrapper( original_term, default_name=_("Type"), rule=TermModel.AND_RULE, wrapped_rule=TermModel.XOR_RULE, specification=TermModel.SPECIFICATION_MODES[0][0], system_flags=_default_system_flags_restriction): """ RUS: Получает или создает термин обертку. """ term_parent = original_term.parent # Compose new entity model class term slug new_term_slug = ENTITY_CLASS_WRAPPER_TERM_SLUG_PATTERN.format( original_term.slug) if (term_parent is None) or (term_parent.slug != new_term_slug): with transaction.atomic(): try: # get or create model class root term root_term = TermModel.objects.get(slug=new_term_slug, parent=term_parent) except TermModel.DoesNotExist: root_term = TermModel(slug=new_term_slug, parent=term_parent, name=original_term.name, semantic_rule=rule, specification_mode=specification, system_flags=system_flags) root_term.save() # set original entity model class term to new parent if original_term.parent != root_term: original_term.parent = root_term original_term.name = default_name original_term.semantic_rule = wrapped_rule original_term.save() else: root_term = term_parent return root_term
def validate_term_model(cls): """ RUS: Добавляет месяц в модель терминов TermModel. Проверяет, есть ли месяц в модели TermModel, и при его отсутствии создает диапазон месяцев (1-12) и разбивку по месяцам в этих диапазонах. """ # Устанавливаем таймаут для валидации key = 'vldt:mnth_add' need_validation = EntityModel._validate_term_model_cache.get(key, True) if need_validation: EntityModel._validate_term_model_cache[key] = False system_flags = _default_system_flags_restriction with transaction.atomic(): try: # added month added_month = TermModel.objects.get( slug=cls.ADDED_MONTH_ROOT_TERM_SLUG, parent=None) except TermModel.DoesNotExist: added_month = TermModel( slug=cls.ADDED_MONTH_ROOT_TERM_SLUG, parent=None, name=_('Added month'), semantic_rule=TermModel.OR_RULE, system_flags=system_flags) added_month.save() for i in range(1, 13): month_key = cls.ADDED_MONTH_KEY.format(i) with transaction.atomic(): try: TermModel.objects.get(slug=month_key, parent=added_month) except TermModel.DoesNotExist: month = TermModel(slug=month_key, parent_id=added_month.id, name=_(calendar.month_name[i]), semantic_rule=TermModel.OR_RULE, system_flags=system_flags) month.save() super(AddedMonthTermsValidationMixin, cls).validate_term_model()
def get_or_create_view_layouts_root(): """ Создаем или возвращаем корневой термин представлений """ view_root = getattr(PageLayout, '_view_layouts_root_cache', None) if not view_root: try: # view layout root view_root = TermModel.objects.get(slug=VIEW_LAYOUT_ROOT_TERM_SLUG, parent=None) except TermModel.DoesNotExist: view_root = TermModel( slug=VIEW_LAYOUT_ROOT_TERM_SLUG, parent=None, name=_('View'), semantic_rule=TermModel.XOR_RULE, system_flags=_default_layout_system_flags_restriction) view_root.save() PageLayout._view_layouts_root_cache = view_root return view_root
def validate_term_model(cls): """ RUS: Добавляет год в модель терминов TermModel. """ # Устанавливаем таймаут для валидации key = 'vldt:year_add' need_validation = EntityModel._validate_term_model_cache.get(key, True) if need_validation: EntityModel._validate_term_model_cache[key] = False system_flags = _default_system_flags_restriction with transaction.atomic(): try: # added year TermModel.objects.get(slug=cls.ADDED_YEAR_ROOT_TERM_SLUG) except TermModel.DoesNotExist: added_year = TermModel(slug=cls.ADDED_YEAR_ROOT_TERM_SLUG, parent=None, name=_('Added year'), semantic_rule=TermModel.XOR_RULE, system_flags=system_flags) added_year.save() super(AddedYearTermsValidationMixin, cls).validate_term_model()
def get_or_create_model_class_wrapper_term(cls): """ RUS: Получает или создает модель класса пользователя model_root_term. """ system_flags = _default_system_flags_restriction cls_name_lower = cls.__name__.lower() # Get original entity model class term original_model_class_term = cls.get_entities_types( from_cache=False)[cls_name_lower] original_model_class_term_parent = original_model_class_term.parent # Compose new entity model class term slug new_model_class_term_slug = ENTITY_CLASS_WRAPPER_TERM_SLUG_PATTERN.format( cls_name_lower) if original_model_class_term_parent.slug != new_model_class_term_slug: with transaction.atomic(): try: # get or create model class root term model_root_term = TermModel.objects.get( slug=new_model_class_term_slug, parent=original_model_class_term_parent) except TermModel.DoesNotExist: model_root_term = TermModel( slug=new_model_class_term_slug, parent_id=original_model_class_term_parent.id, name=force_text(cls._meta.verbose_name), semantic_rule=TermModel.AND_RULE, system_flags=system_flags) model_root_term.save() # set original entity model class term to new parent original_model_class_term.parent = model_root_term original_model_class_term.name = _("Type") original_model_class_term.save() else: model_root_term = original_model_class_term_parent return model_root_term
def validate_term_model(cls): """ RUS: Добавляет Состояние родителя и объекта в модель TermModel при их отсутствии и сохраняет их. """ super(FSMMixin, cls).validate_term_model() if not cls._meta.abstract and cls._meta.get_field( 'status').model == cls: system_flags = _default_system_flags_restriction model_root_term = get_or_create_model_class_wrapper_term(cls) with transaction.atomic(): try: states_parent_term = TermModel.objects.get( slug=cls.STATE_ROOT_TERM_SLUG, parent=model_root_term) except TermModel.DoesNotExist: states_parent_term = TermModel( slug=cls.STATE_ROOT_TERM_SLUG, parent_id=model_root_term.id, name=force_text( cls._meta.get_field('status').verbose_name), semantic_rule=TermModel.XOR_RULE, system_flags=system_flags) states_parent_term.save() transition_states = cls.TRANSITION_TARGETS for state_key, state_name in transition_states.items(): with transaction.atomic(): try: states_parent_term.get_descendants( include_self=False).get(slug=state_key) except TermModel.DoesNotExist: state = TermModel(slug=state_key, parent_id=states_parent_term.id, name=force_text(state_name), semantic_rule=TermModel.OR_RULE, system_flags=system_flags) state.save()
def validate_term_model(cls): """ RUS: Добавляет день в модель терминов. Проверяет, есть ли день в модели TermModel, и при его отсутствии создает диапазон дат и разбивку по дням в этих диапазонах. """ # валидируем только один раз key = 'vldt:day_add' need_validation = EntityModel._validate_term_model_cache.get(key, True) if need_validation: EntityModel._validate_term_model_cache[key] = False system_flags = _default_system_flags_restriction with transaction.atomic(): try: # added day added_day = TermModel.objects.get( slug=cls.ADDED_DAY_ROOT_TERM_SLUG, parent=None) except TermModel.DoesNotExist: added_day = TermModel(slug=cls.ADDED_DAY_ROOT_TERM_SLUG, parent=None, name=_('Added day'), semantic_rule=TermModel.OR_RULE, system_flags=system_flags) added_day.save() day_ranges = ((1, 11), (11, 21), (21, 32)) for r in day_ranges: # added day range day_range_key = cls.ADDED_DAY_RANGE_KEY.format(r[0], r[1] - 1) with transaction.atomic(): try: added_day_range = TermModel.objects.get( slug=day_range_key, parent=added_day) except TermModel.DoesNotExist: added_day_range = TermModel( slug=day_range_key, parent_id=added_day.id, name="{} - {}".format(r[0], r[1] - 1), semantic_rule=TermModel.OR_RULE, system_flags=system_flags) added_day_range.save() for i in range(r[0], r[1]): # added day day_key = cls.ADDED_DAY_KEY.format(i) with transaction.atomic(): try: TermModel.objects.get(slug=day_key) except TermModel.DoesNotExist: day = TermModel(slug=day_key, parent_id=added_day_range.id, name="{:02d}".format(i), semantic_rule=TermModel.OR_RULE, system_flags=system_flags) day.save() super(AddedDayTermsValidationMixin, cls).validate_term_model()