def create(self, *_, **kwargs): """Create object""" if not kwargs: raise IntegrityError("no parameters to create") for field_name, field in self.model_cls._fields.items(): if (getattr(self.model_cls, field_name) is None or getattr(self.model_cls, field_name) == "None") and field.required: raise IntegrityError('NOT NULL constraint failed: {} in {} column' .format(getattr(self.model_cls, field_name), field_name)) edited_kw = {} for field_name, field in kwargs.items(): value = getattr(self.model_cls, field_name).validate(kwargs.get(field_name)) edited_kw[field_name] = value insert_query = sql.SQL("INSERT INTO {0} ({1}) VALUES ({2}) RETURNING *;").format( sql.Identifier(str(self.model_cls._table_name).lower()), sql.SQL(', ').join([sql.Identifier(i) for i in edited_kw.keys()]), sql.SQL(', ').join([sql.Literal(i) for i in edited_kw.values()])) print(insert_query.as_string(connection)) cursor.execute(insert_query.as_string(connection)) res = dict(zip([i.name for i in cursor.description], cursor.fetchone())) connection.commit() return self.model_cls(**res)
def set_quota(self, quota): if self._partition.subpartitions is not None: # this is a superpartition raise IntegrityError('Cannot set quota on a superpartition.') if self._partition.parent is not None and quota < 0: # quota < 0 -> infinite. This partition cannot be a subpartition raise IntegrityError('Infinite quota set for a subpartition') self._quota = quota
def check_fields(self): """Return exception if required field is none""" for field_name, field in self._fields.items(): if (getattr(self, field_name) is None or getattr(self, field_name) == "None") and field.required: raise IntegrityError( 'NOT NULL constraint failed: {} in {} column'.format(getattr(self, field_name), field_name))
def embed_into(self, inventory, check=False): try: site = inventory.sites[self._site_name()] except KeyError: raise ObjectError('Unknown site %s', self._site_name()) try: partition = inventory.partitions[self._partition_name()] except KeyError: raise ObjectError('Unknown partition %s', self._partition_name()) updated = False try: site_partition = site.partitions[partition] except KeyError: IntegrityError('SitePartition %s/%s must exist but does not.', site.name, partition.name) else: if check and (site_partition is self or site_partition == self): # identical object -> return False if check is requested pass else: site_partition.copy(self) updated = True if check: return site_partition, updated else: return site_partition
def insert(self, model: str, record: Dict): table_name = self.get_table_name_from_model(model) if table_name not in self.data.keys(): self.data[table_name] = list() id_ = record.get( 'id', len(self.data[table_name]) + 1) or len(self.data[table_name]) + 1 existing_row = None if id_ <= len(self.data[table_name]): existing_row = next( (row for row in self.data[table_name] if row['id'] == id_), None) if existing_row: raise IntegrityError( message=f'Row with id ({id_}) exists in table {table_name}') record['id'] = id_ self.data[table_name].append(record) return record
def add_block_replica(self, replica): # this function should be called automatically to avoid integrity errors try: dataset_replica = self._dataset_replicas[replica.block.dataset] except KeyError: raise ObjectError('Dataset %s is not at %s' % (replica.block.dataset.name, self._name)) if replica not in dataset_replica.block_replicas: raise IntegrityError('%s is not a block replica of %s' % (str(replica), str(dataset_replica))) for partition, site_partition in self.partitions.iteritems(): if not partition.contains(replica): continue try: block_replica_list = site_partition.replicas[dataset_replica] except KeyError: if len(dataset_replica.block_replicas) == 1: # this is the sole block replica site_partition.replicas[dataset_replica] = None else: site_partition.replicas[dataset_replica] = set([replica]) else: if block_replica_list is None: # assume this function was called for all new block replicas # then we are just adding another replica to this partition pass else: # again assuming this function is called for all new block replicas, # block_replica_list not being None implies that adding this new # replica will not make the dataset replica in this partition complete block_replica_list.add(replica)
def _load_files(self): if self.id == 0: return set() files = Block.inventory_store.get_files(self) if len(files) != self._num_files: raise IntegrityError( 'Number of files mismatch in %s: predicted %d, loaded %d' % (str(self), self._num_files, len(files))) size = sum(f.size for f in files) if size != self._size: raise IntegrityError( 'Size mismatch in %s: predicted %d, loaded %d' % (str(self), self._size, size)) return files
def validate(self, value): if value is None and not self.required: return None elif isinstance(value, datetime.datetime): return value elif isinstance(value, (list, tuple)): return datetime.datetime(*value) elif isinstance(value, dict): return datetime.datetime(**value) else: raise IntegrityError("wrong data insert to {} ({})".format(self.f_type, value))