Package ajango :: Package database :: Package validate
[hide private]
[frames] | no frames]

Source Code for Package ajango.database.validate

  1  ########################################################################### 
  2  #                                                                         # 
  3  #  Copyright (C) 2016  Rafal Kobel <rafyco1@gmail.com>                    # 
  4  #                                                                         # 
  5  #  This program is free software: you can redistribute it and/or modify   # 
  6  #  it under the terms of the GNU General Public License as published by   # 
  7  #  the Free Software Foundation, either version 3 of the License, or      # 
  8  #  (at your option) any later version.                                    # 
  9  #                                                                         # 
 10  #  This program is distributed in the hope that it will be useful,        # 
 11  #  but WITHOUT ANY WARRANTY; without even the implied warranty of         # 
 12  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           # 
 13  #  GNU General Public License for more details.                           # 
 14  #                                                                         # 
 15  #  You should have received a copy of the GNU General Public License      # 
 16  #  along with this program.  If not, see <http://www.gnu.org/licenses/>.  # 
 17  #                                                                         # 
 18  ########################################################################### 
 19  """ 
 20  Modul obiektow sprawdzajacych. 
 21   
 22  Obiekty sprawdzajace zastosowane sa w formularzach wprowadzania danych. Maja 
 23  one za zadanie zweryfikowanie czy napisy wprowadzone przez uzytkownika przyjmuja 
 24  zadana forme. 
 25   
 26  Dostepne parametry 
 27  ================== 
 28   
 29      - I{type} - Typ walidatora 
 30      - I{param} - Parametr zalezny od typu 
 31   
 32  Dostepne Walidacje 
 33  ================== 
 34   
 35  Walidatory moga sprawdzic nastepujace warunki: 
 36   
 37      - L{pole jest puste <ajango.database.validate.isempty>} 
 38      - L{pole spelnia wyrazenie regularne <ajango.database.validate.regex>} 
 39      - L{pole jest mniejsze niz parametr <ajango.database.validate.lt>} 
 40      - L{pole jest wieksze niz parametr <ajango.database.validate.gt>} 
 41  """ 
 42   
 43  from django.core.management.base import CommandError 
 44  from ajango.core.factory         import FactoryBase 
 45  from ajango.core.generable       import Generable 
 46  from ajango.core.hybrid          import Hybrid, get_str_object_factory 
 47  import abc 
48 49 -class ValidateFactoryObject(FactoryBase):
50 """ Fabryka obiektow sprawdzajacych. """
51 - def __init__(self, param=None):
52 self.param = None 53 FactoryBase.__init__(self, param)
54 - def init(self):
55 """ Metoda inicjalizujaca. """ 56 self.set_items('Validate', { 57 'isempty' : 'ajango.database.validate.isempty', 58 'regex' : 'ajango.database.validate.regex', 59 'lt' : 'ajango.database.validate.lt', 60 'gt' : 'ajango.database.validate.gt' 61 }) 62 get_str_object_factory(self, 'isempty')
63
64 -def validate_factory(param):
65 """ Fabryka obiektow sprawdzajacych. """ 66 return ValidateFactoryObject(param).get_from_params()
67
68 -class ValidateBase(Hybrid, Generable):
69 """ Klasa abstrakcyjna obiektu sprawdzajacego. """ 70 __metaclass__ = abc.ABCMeta
71 - def __init__(self, param):
72 self.has_prepare = False 73 self.type = "unKnown" 74 self.data = {} 75 self.xml_name = "VALIDATE" 76 self.pre_init() 77 self.messages = [] 78 self.param = "" 79 Hybrid.__init__(self, param) 80 self.post_init()
81 - def pre_init(self):
82 """ Czynnosci przed inicjalizacja. """ 83 pass
84 - def post_init(self):
85 """ Czynnosci po inicjalizacji. """ 86 pass
87 - def get_param(self):
88 """ Pobranie parametru validatora. """ 89 return self.param
90 - def read_from_xml(self, xmldoc):
91 """ Inicjalizacja z danych XML. """ 92 self.param = self.getAttribute('param')
93 - def read_from_dict(self, params):
94 """ Inicjalizacja ze zmiennej slownikowej. """ 95 try: 96 self.param = params['param'] 97 except KeyError: 98 raise CommandError("Input element is invalid")
99 @abc.abstractmethod
100 - def is_valid(self, text):
101 """ Sprawdza czy pola wejsiowe sa poprawnie wypelnione. """ 102 raise CommandError("File not implemented yet.")
103 - def execute(self, view, view_name="view"):
104 """ Wykonanie zadan obiektu. """ 105 view.add_line("vm.add_validate({'type': %r, 'param': %r })" % 106 (self.type, self.param))
107 - def get_messages(self):
108 """ Pobiera wiadomosci bledu pol wejsiowych. """ 109 return self.messages
110 - def check(self, name, xmldoc_elem):
111 pass
112
113 -class ValidationManager(Generable):
114 """ Klasa zarzadzajaca obiektami sprawdzajacymi. """
115 - def __init__(self):
116 self.validations = [] 117 self.messages = []
118 - def __len__(self):
119 return len(self.validations)
120 - def add_validate(self, param):
121 """ Dodanie obiektu sprawdzajacego. """ 122 self.validations.append(validate_factory(param))
123 - def is_valid(self, text):
124 """ Sprawdza czy pola wejsciowe sa poprawnie wypelnione. """ 125 for elem in self.validations: 126 if not elem.is_valid(text): 127 self.messages += elem.get_messages() 128 return False 129 return True
130 - def get_messages(self):
131 """ Pobranie wiadomosci bledu pola wejsciowego. """ 132 return self.messages
133 - def execute(self, view, view_name="view"):
134 """ Wykonanie zadan obiektu. """ 135 if len(self.validations) > 0: 136 view.add_import("ValidationManager", "ajango.database.validate") 137 view.add_line("vm = ValidationManager()") 138 for elem in self.validations: 139 elem.execute(view)
140