Package camelot :: Package camelot :: Package view :: Module validator
[hide private]
[frames] | no frames]

Source Code for Module camelot.camelot.view.validator

  1  #  ============================================================================ 
  2  # 
  3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved. 
  4  #  www.conceptive.be / project-camelot@conceptive.be 
  5  # 
  6  #  This file is part of the Camelot Library. 
  7  # 
  8  #  This file may be used under the terms of the GNU General Public 
  9  #  License version 2.0 as published by the Free Software Foundation 
 10  #  and appearing in the file LICENSE.GPL included in the packaging of 
 11  #  this file.  Please review the following information to ensure GNU 
 12  #  General Public Licensing requirements will be met: 
 13  #  http://www.trolltech.com/products/qt/opensource.html 
 14  # 
 15  #  If you are unsure which license is appropriate for your use, please 
 16  #  review the following information: 
 17  #  http://www.trolltech.com/products/qt/licensing.html or contact 
 18  #  project-camelot@conceptive.be. 
 19  # 
 20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 
 21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
 22  # 
 23  #  For use of this library in commercial applications, please contact 
 24  #  project-camelot@conceptive.be 
 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   
36 -class Validator(QtCore.QObject):
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
41 - def __init__(self, admin, model):
42 self.admin = admin 43 self.model = model 44 self.message_cache = fifo(10)
45
46 - def objectValidity(self, entity_instance):
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
68 - def isValid(self, row):
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 # rude, should be fixed 82 pass 83 return len(messages) == 0
84
85 - def validityMessages(self, row):
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
92 - def validityMessage(self, row, parent):
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