1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import logging
29 logger = logging.getLogger('camelot.view.validator')
30
31 from PyQt4 import QtGui
32 from PyQt4 import QtCore
33
34 from fifo import fifo
35
37 """A validator class validates an entity before flushing it to the database
38 and provides the user with feedback if the entity is not ready to flush
39 """
40
42 self.admin = admin
43 self.model = model
44 self.message_cache = fifo(10)
45
47 """@return: list of messages explaining invalid data
48 empty list if object is valid
49 """
50 messages = []
51 for column in self.model.getColumns():
52 value = getattr(entity_instance, column[0])
53 if column[1]['nullable']!=True:
54 is_null = False
55 if value==None:
56 is_null = True
57 elif (column[1]['widget'] == 'code') and \
58 (sum(len(c) for c in value) == 0):
59 is_null = True
60 elif (column[1]['widget'] == 'str') and (len(value) == 0):
61 is_null = True
62 elif (column[1]['widget'] == 'many2one') and (not value.id):
63 is_null = True
64 if is_null:
65 messages.append(u'%s is a required field' % (column[1]['name']))
66 return messages
67
69 """Verify if a row in a model is 'valid' meaning it could be flushed to
70 the database
71 """
72 messages = []
73 logger.debug('is valid for row %s' % row)
74
75 try:
76 entity_instance = self.model._get_object(row)
77 if entity_instance:
78 messages = self.objectValidity(entity_instance)
79 self.message_cache.add_data(row, entity_instance.id, messages)
80 except:
81
82 pass
83 return len(messages) == 0
84
86 try:
87 return self.message_cache.get_data_at_row(row)
88 except KeyError:
89 raise Exception('Programming error : isValid should be called ' \
90 'before calling validityMessage')
91
93 """Inform the user about the validity of the data at row, by showing
94 a message box, this function can only be called if isValid has been
95 called and is finished within the model thread
96 """
97 messages = self.validityMessages(row)
98 QtGui.QMessageBox.information(parent, u'Validation', u'\n'.join(messages))
99