Ejemplo n.º 1
0
 def validate_remove_day(self, day):
     if not self.is_int(day):
         raise ValidationError("Day must be a natural number")
     day = int(day)
     if day < 1:
         raise ValidationError("Day cannot be less than 1")
     if day > 31:
         raise ValidationError("Day cannot be more than 31")
Ejemplo n.º 2
0
    def validate_add(self, cost, category):
        if not self.is_int(cost):
            raise ValidationError("Sum must be a natural number")
        cost = int(cost)
        if cost < 1:
            raise ValidationError("Sum cannot be 0 or negative")

        if not self.is_category_valid(category):
            raise ValidationError("Category is not valid")
Ejemplo n.º 3
0
    def validate_list_category_condition(self, category, operator, value):
        if not self.is_category_valid(category):
            raise ValidationError("Category is not valid")

        if not self.is_operator(operator):
            raise ValidationError(
                "Operator is not valid, use only '<', '=' or '>'")

        if not self.is_int(value):
            raise ValidationError("The value must be a natural number")
Ejemplo n.º 4
0
 def check_requirements(self):
     if self.required and self.value is None:
         msg = 'Value for field \'%s\' is required, received \'%s\' instead ' % (
             self.name, self.value)
         raise ValidationError(msg)
     elif not self.nullable and not isinstance(
             self.value,
         (int, float)) and self.value is not None and len(self.value) == 0:
         msg = 'Value for field \'%s\' can not be empty, received \'%s\' instead ' % (
             self.name, self.value)
         raise ValidationError(msg)
Ejemplo n.º 5
0
 def check_type(self):
     accepted = (str, int, float)
     if self.name in ('first_name', 'last_name'):
         regex = re.compile('[@_!#$%^&*()<>?/|}{~:;.,+=№\]\[0123456789]')
         if regex.search(str(self.value)):
             msg = 'Value for field \'%s\' should contain only letters' % self.name
             raise ValidationError(msg)
         accepted = (str, )
     if not isinstance(self.value, accepted) and self.value is not None:
         msg = 'Value for field \'%s\' has wrong type' % self.name
         raise ValidationError(msg)
Ejemplo n.º 6
0
 def validate(self):
     if self.is_empty:
         raise ValidationError('Value for field \'%s\'can not be empty.' %
                               self.name)
     elif not isinstance(self.value, list):
         raise ValidationError('Value for field \'%s\' must be an array' %
                               self.name)
     for i in self.value:
         if not isinstance(i, int):
             raise ValidationError(
                 'Array for field \'%s\'can must contain only integers' %
                 self.name)
Ejemplo n.º 7
0
 def validate(self):
     l, c = 11, '7'
     msg = '''Value for field \'%s\' must be an %s symbols long starting with %s, received \'%s\' instead''' \
           % (self.name, l, c, self.value)
     if not self.is_empty and not self.is_none:
         if len(str(self.value)) != l or str(
                 self.value)[0] != c and '.' not in str(self.value):
             raise ValidationError(msg)
         try:
             int(self.value)
         except Exception:
             raise ValidationError(msg)
Ejemplo n.º 8
0
 def check_type(self):
     accepted = (str, int)
     if self.name in ('first_name', 'last_name'):
         accepted = (str, )
     if not isinstance(self.value, accepted) and self.value is not None:
         msg = 'Value for field \'%s\' has wrong type' % self.name
         raise ValidationError(msg)
Ejemplo n.º 9
0
 def validate(self):
     l, c = 11, '7'
     if not self.is_empty:
         if len(str(self.value)) != l or str(self.value)[0] != c:
             msg = '''Value for field \'%s\' must be an %s symbols long starting with %s, received \'%s\' instead''' \
                   % (self.name, l, c, self.value)
             raise ValidationError(msg)
Ejemplo n.º 10
0
 def validate(self):
     if not self.is_empty:
         try:
             datetime.datetime.strptime(self.value, '%d.%m.%Y')
         except:
             msg = 'Value for field \'%s\' must be in format dd.mm.yyyy, received \'%s\' instead' % (
                 self.name, self.value)
             raise ValidationError(msg)
Ejemplo n.º 11
0
 def validate(self):
     age_limit = 70
     if not self.is_empty:
         super(BirthDayField,
               self).validate()  # Check if date in expected format
         birth_date, today_date = self.value, datetime.datetime.today(
         ).strftime('%d.%m.%Y')
         bday, bmonth, byear = map(int, birth_date.split('.'))
         day, month, year = map(int, today_date.split('.'))
         msg = 'Your age must be equal or below %s' % age_limit
         if year - byear > age_limit:
             raise ValidationError(msg)
         elif year - byear == 70:
             if bmonth > month:
                 raise ValidationError(msg)
             elif bmonth == month:
                 if bday > day:
                     raise ValidationError(msg)
Ejemplo n.º 12
0
    def undo_last_action(self):
        '''
        Undoes the last action that modified the list of expenses

        Input: list - params from input
        Output: -
        '''
        if self.undo_actions.nothing_to_undo():
            raise ValidationError("No action available to undo")
        self.undo_actions.undo()
Ejemplo n.º 13
0
    def max_day(self, params):
        '''
        Returns the maximum expense in the specified day

        Input: list - params from input
        Output: string - the max expense or error
        '''
        day_string = params[0]

        self.validator.validate_max_day(day_string)
        day = int(day_string)

        max_day = self.get_max_by(lambda e: e.day == day)
        if max_day < 1:
            raise ValidationError("No expenses found in the " +
                                  self.format_day(day) + " day")
        return "\nThe maximum of the " + self.format_day(
            day) + " day is " + str(max_day)
Ejemplo n.º 14
0
    def build_list(self, expenses, condition=lambda e: True):
        '''
        Builds a string representing the list of expenses 

        Input: 
            list - expenses list to be converted into string
            lambda (optional) - receives an expense object and must return a bool value
        Output: string - the converted list
        '''
        list_expenses = ""
        prints = 0

        for e in expenses:
            if condition(e):
                prints += 1
                list_expenses += "\n" + str(prints) + ". " + str(e)

        if prints < 1:
            raise ValidationError("No expenses found")

        return list_expenses
Ejemplo n.º 15
0
    def validate_remove_range(self, day_from, day_to):
        if not self.is_int(day_from):
            raise ValidationError("The starting day must be a natural number")
        day_from = int(day_from)
        if day_from < 1:
            raise ValidationError("The starting day cannot be less than 1")
        if day_from > 31:
            raise ValidationError("The starting day cannot be more than 31")

        if not self.is_int(day_to):
            raise ValidationError("The eding day must be a natural number")
        day_to = int(day_to)
        if day_to < 1:
            raise ValidationError("The eding day cannot be less than 1")
        if day_to > 31:
            raise ValidationError("The eding day cannot be more than 31")

        if day_from > day_to:
            raise ValidationError(
                "The starting day cannot be after the ending day")
Ejemplo n.º 16
0
    def validate_insert(self, day, cost, category):
        if not self.is_int(day):
            raise ValidationError("Day must be a natural number")
        day = int(day)
        if day < 1:
            raise ValidationError("Day cannot be less than 1")
        if day > 31:
            raise ValidationError("Day cannot be more than 31")

        if not self.is_int(cost):
            raise ValidationError("Sum must be a natural number")
        cost = int(cost)
        if cost < 1:
            raise ValidationError("Sum cannot be 0 or negative")

        if not self.is_category_valid(category):
            raise ValidationError("Category is not valid")
Ejemplo n.º 17
0
    def validate_add(self, day, cost, category):
        if not self.is_int(day):
            raise ValidationError("Day must be a natural number")
        day = int(day)
        if day < 1:
            raise ValidationError("Day cannot be less than 1")
        if day > 30:
            raise ValidationError("Day cannot be more than 30")

        if not self.is_int(cost):
            raise ValidationError("Sum must be a natural number")
        cost = int(cost)
        if cost < 1:
            raise ValidationError("Sum cannot be 0 or negative")

        if len(category) < 1:
            raise ValidationError("Category must have at least 1 character")
Ejemplo n.º 18
0
 def validate_sum_category(self, category):
     if not self.is_category_valid(category):
         raise ValidationError("Category is not valid")
Ejemplo n.º 19
0
 def validate(self):
     accepted = (0, 1, 2, '', None)
     if self.value not in accepted:
         msg = '''Value for field \'%s\' must be one of (%s), received %s instead''' \
               % (self.name, ', '.join([str(i) for i in accepted]), self.value)
         raise ValidationError(msg)
Ejemplo n.º 20
0
 def validate(self):
     if not self.is_empty and '@' not in str(self.value):
         msg = 'Value for field \'%s\' must be an email string, received \'%s\' instead' % (
             self.name, self.value)
         raise ValidationError(msg)
Ejemplo n.º 21
0
 def check_type(self):
     accepted = (dict, list, tuple)
     if not isinstance(self.value, accepted) and self.value is not None:
         msg = 'Value for field \'%s\' can must be dict or array-like, received \'%s\' instead ' % (
             self.name, self.value)
         raise ValidationError(msg)
Ejemplo n.º 22
0
 def validate_filter_above(self, value):
     if not self.is_int(value):
         raise ValidationError("The value must be a natural number")