# coding=utf-8 import re from django.db import models from web.models.constants import CANCELLED_APPOINTMENT_COLOR_CONFIGURATION_TYPE, \ NO_SHOW_APPOINTMENT_COLOR_CONFIGURATION_TYPE, KIT_EMAIL_HOUR_CONFIGURATION_TYPE, \ KIT_EMAIL_DAY_OF_WEEK_CONFIGURATION_TYPE class ConfigurationItem(models.Model): class Meta: app_label = 'web' type = models.CharField(max_length=50, verbose_name='Type', editable=False ) name = models.CharField(max_length=255, verbose_name='Name', editable=False ) value = models.CharField(max_length=50, verbose_name='Value', ) def __str__(self): return "%s %s" % (self.name, self.value) def __unicode__(self): return "%s %s" % (self.name, self.value) @staticmethod def is_valid(item): message = ConfigurationItem.validation_error(item) return message == "" @staticmethod def validation_error(item): pattern = None if item.type == CANCELLED_APPOINTMENT_COLOR_CONFIGURATION_TYPE \ or item.type == NO_SHOW_APPOINTMENT_COLOR_CONFIGURATION_TYPE: pattern = "^#[0-9a-fA-F]+$" if item.type == KIT_EMAIL_HOUR_CONFIGURATION_TYPE: pattern = "^[0-9]{2}:[0-9]{2}$" if item.type == KIT_EMAIL_DAY_OF_WEEK_CONFIGURATION_TYPE: pattern = "^(MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY)$" if pattern is not None: if not re.compile(pattern).match(item.value): return "Invalid value of param: " + item.name + ". It should match regex pattern: " + pattern return ""