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

Source Code for Package ajango.database.inputs

  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  """ Modul zarzadzania polami wejsciowymi. """ 
 20   
 21  from __future__                  import print_function 
 22  from django.core.management.base import CommandError 
 23  from ajango.core.factory         import FactoryBase 
 24  from ajango.database.validate    import ValidationManager 
 25  from ajango.core.generable       import Generable 
 26  from ajango.core.hybrid          import Hybrid, get_str_object_factory 
 27   
28 -class InputFactoryObject(FactoryBase):
29 """ Klasa fabryki pol wejsciowych. """
30 - def init(self):
31 """ Metoda inicujujaca. """ 32 self.set_items('Input', { 33 'default' : 'ajango.database.inputs.default', 34 'number' : 'ajango.database.inputs.number' 35 }) 36 get_str_object_factory(self)
37
38 -def input_factory(params):
39 """ Fabryka pol wejsciowych. """ 40 return InputFactoryObject(params).get_from_params()
41 42 #pylint: disable=R0902
43 -class InputBase(Hybrid, Generable):
44 """ Abstrakcyjna klasa pola wejsiowego. """
45 - def __init__(self, params):
46 self.type = "unKnown" 47 self.theme = 'default' 48 self.default = "" 49 self.messages = [] 50 self.has_prepare = False 51 self.data = {} 52 self.xml_name = "INPUT" 53 self.pre_init() 54 self.label = "" 55 self.default = "" 56 self.tag = "" 57 self.validation_manager = ValidationManager() 58 Hybrid.__init__(self, params) 59 self.value = self.default 60 self.post_init()
61 - def read_from_xml(self, xmldoc):
62 """ Inicjalizacja z danych XML. """ 63 self.label = self.getAttribute('label') 64 self.default = self.getAttribute('default') 65 self.tag = self.getAttribute('tag')
66 - def read_from_dict(self, params):
67 """ Inicjalizacja ze zmiennej slownikowej. """ 68 try: 69 self.validation_manager = params['vm'] 70 if self.validation_manager == None: 71 self.validation_manager = ValidationManager() 72 except KeyError: 73 self.validation_manager = ValidationManager() 74 try: 75 self.label = params['label'] 76 self.default = params['default'] 77 self.tag = params['tag'] 78 except KeyError: 79 raise CommandError("Input element is invalid")
80 - def pre_init(self):
81 """ Czynnosci przed inicjalizacja. """ 82 self.add_permited(["VALIDATE"])
83 - def post_init(self):
84 """ Czynnosci po inicjalizacji. """ 85 pass
86 - def execute(self, view, view_name='view'):
87 """ Wykonanie zadan obiektu. """ 88 self.validation_manager.execute(view) 89 if len(self.validation_manager) > 0: 90 view.add_line("%s.add_input({'type': %r, " 91 "'label': %r, " 92 "'tag': %r, " 93 "'default': %r, " 94 "'vm': vm })" % 95 (view_name, self.type, self.label, 96 self.tag, self.default)) 97 else: 98 view.add_line("%s.add_input({'type': %r, " 99 "'label': %r, " 100 "'tag': %r, " 101 "'default': %r })" % 102 (view_name, self.type, self.label, 103 self.tag, self.default))
104 - def set_value(self, value):
105 """ Ustawienie wartosci. """ 106 self.value = value
107 - def check(self, name, xmldoc_elem):
108 """ Oczytanie nodow wewnetrznych. """ 109 if name == 'VALIDATE': 110 self.validation_manager.add_validate(xmldoc_elem)
111 - def prepare_data(self):
112 """ Przygotowanie danych dla szablonu. """ 113 if self.has_prepare: 114 return 115 # Id dla fromularza, do uzycia przy kilku widokach na jednej stronie 116 self.has_prepare = True 117 self.data['label'] = self.label 118 self.data['default'] = self.default 119 self.data['value'] = self.value 120 self.data['tag'] = self.tag 121 self.data['url'] = '%s/ajango_inputs/%s.html' % (self.theme, self.type) 122 self.data['messages'] = self.messages
123 - def get_data(self):
124 """ Pobieranie danych dla szablonu. """ 125 self.prepare_data() 126 return dict(self.data)
127 - def get_label(self):
128 """ Pobranie opisu pola wejsiowego. """ 129 self.prepare_data() 130 return self.label
131 - def get_messages(self):
132 """ Pobranie wiadomosci bledu pola wejsciowego. """ 133 return self.messages
134 - def is_valid(self, data):
135 """ Sprawdzenie pole wejsciowe jest poprawnie wypelnione. """ 136 value = False 137 try: 138 text = data[self.tag] 139 value = self.validation_manager.is_valid(text) 140 self.messages += self.validation_manager.get_messages() 141 except KeyError: 142 self.messages.append("Cannot read data named %r from POST" % 143 self.tag) 144 return value
145
146 -class InputManager(object):
147 """ Klasa zarzadzajaca obiektami pol wejsiowych. """
148 - def __init__(self):
149 self.inputs = [] 150 self.messages = []
151 - def add_input(self, input_ob):
152 """ Dodaj pole wejsiowe. """ 153 self.inputs.append(input_ob)
154 - def get_inputs(self):
155 """ Zwraca tablice elementow kontenera. """ 156 return self.inputs
157 - def is_valid(self, data):
158 """ Sprawdza czy pola wejsiowe sa poprawnie wypelnione. """ 159 result = True 160 for elem in self.inputs: 161 if not elem.is_valid(data): 162 self.messages += elem.get_messages() 163 result = False 164 return result
165 - def set_data(self, data):
166 """ Ustawienie elementow danymi z parametru. """ 167 for elem in self.inputs: 168 try: 169 value = data[elem.tag] 170 elem.value = value 171 except KeyError: 172 print("There are no tag %r in data object" % elem.tag) 173 elem.value = elem.default
174 - def set_data_table(self, table_el):
175 """ Ustwienie elementow na podstawie tabeli. """ 176 for elem in self.inputs: 177 value = table_el.serializable_value(elem.tag) 178 elem.value = value
179 - def get_messages(self):
180 """ Pobiera wiadomosci bledu pol wejsiowych. """ 181 return self.messages
182