def db_checks(klass, cod, fld): """ Db checks for report code field. Method returns empty string if code is validated or relevant error if otherwise. """ errors = [] try: if cod.upper() == "PRE": value = fld.working_value[0].encode() if fld.display() == "id": curz = db.cursor() # Check if Id is duplicated curz.execute('SELECT CASE WHEN EXISTS (SELECT * FROM pregreports WHERE idfield = %s) \ THEN CAST(1 AS BIT) ELSE CAST(0 as BIT) END;', [value]) if int(curz.fetchone()[0]): errors.append("duplicate_id") elif fld.display() == "lmpdate": if not (klass.is_past_date(value) and klass.is_date_before(value, 9*30)): errors.append("invalid_LMPDate") elif fld.display() == "ancdate": if not klass.is_date_before(value, 0): errors.append("past_ancdate") elif fld.display() == "gravidity": if not 0 < int(value) < 30: errors.append("gravidity_limit") elif fld.display() == "parity": if not 1 < int(value) < 30: errors.append("parity_limit") elif fld.display() == "weight": if not 35 < float((re.findall('\d+.\d|\d+',value))[0]) < 150: errors.append("weight_limit") elif fld.display() == "height": if not 50 < float((re.findall('\d+.\d|\d+',value))[0]) < 250: errors.append("height_limit") else: pass except Exception, errors: pass
def rows(self): curz = db.cursor() qry = 'SELECT %s FROM %s' % (', '.join([x[0] for x in self.cols]), self.tablename) curz.execute(qry) rs = curz.fetchall() curz.close() db.commit() return rs
def create_in_db(self, repc): try: tbl, cols = stuff = self.creation_sql(repc) if self.created: return stuff curz = db.cursor() curz.execute('SELECT TRUE FROM information_schema.tables WHERE table_name = %s', (tbl,)) if not curz.fetchone(): curz.execute('CREATE TABLE %s (indexcol SERIAL NOT NULL);' % (tbl,)) curz.close() return self.create_in_db(repc) for col in cols: curz.execute('SELECT TRUE FROM information_schema.columns WHERE table_name = %s AND column_name = %s', (tbl, col[0])) if not curz.fetchone(): curz.execute('ALTER TABLE %s ADD COLUMN %s %s;' % (tbl, col[0], col[1])) curz.close() db.commit() self.created = True return stuff except Exception, e: raise Exception, ('Table creation: ' + str(e))