def test_db_api_flush(db): db.create_all() movie = db['Movie'](title="Mad Max") db.add(movie) assert object_state(movie).pending is True db.flush() assert object_state(movie).persistent is True db.commit()
def resolve(self) -> 'FileModel': """Resolve the file by its SHA1 hash. todo: reference to util.hash_file If the computed hash is new, the file is committed to the database. Otherwise, the original entry is re-used. Returns ------- FileModel The current instance if the file is new, or a new ``FileModel`` instance representing the original database entry. """ if not self.resolved: self._join() hash = self._get_hash() with self.session(add=False) as s: match = s.query(self.__class__).filter_by(hash=hash).first() if match is None: s.add(self) self.hash = hash self.path = self._path file = self else: file = match file.connect(self) if object_state(self).persistent: s.delete(self) file._resolved = True file.used = datetime.datetime.now() return file else: return self
def test_add_block(blocks_with_txs, sqlitedb_session): results = add_blocks(blocks_with_txs, sqlitedb_session, insert=True) for obj, result in results: assert result state = object_state(obj) assert state.persistent is True assert Block.count(sqlitedb_session) == len(blocks_with_txs)
def to_dict(self, with_shortcode=False): ans = {x.key:x.value for x in object_state(self).attrs} ans.update({ 'project': self.project.to_dict(), 'datetime_claimed': calendar.timegm(self.datetime_claimed.utctimetuple()) if self.datetime_claimed else None, }) if with_shortcode: ans['lower_shortcode'] = int_to_str(self.lower_sequence_num, self.project.alphabet) ans['upper_shortcode'] = int_to_str(self.upper_sequence_num, self.project.alphabet) return ans
def to_dict(self, with_shortcode=False): ans = {x.key: x.value for x in object_state(self).attrs} ans.update({ 'project': self.project.to_dict(), 'datetime_claimed': calendar.timegm(self.datetime_claimed.utctimetuple()) if self.datetime_claimed else None, }) if with_shortcode: ans['lower_shortcode'] = int_to_str(self.lower_sequence_num, self.project.alphabet) ans['upper_shortcode'] = int_to_str(self.upper_sequence_num, self.project.alphabet) return ans
def safe_insert(obj, session, **kwargs): """ Add single object to session and commit session. Args: obj (Union[TxBase,Block]): session (sqlalchemy.orm.session.Session): **kwargs (dict): Returns: bool: """ with session_scope(session, **kwargs) as s: s.add(obj) result = getattr(object_state(obj), 'persistent', False) return result
def subcommand_add(*unused_args, **unused_kwargs): starsystem = get_system_or_none(system_name) if not starsystem: return landmark = Landmark(name=starsystem.name, name_lower=starsystem.name_lower, xz=starsystem.xz, y=starsystem.y) landmark = db.merge(landmark) persistent = object_state(landmark).persistent db.commit() if persistent: bot.reply( "System '{}' was already a landmark. Updated to current coordinates." .format(starsystem.name)) else: bot.reply("Added system '{}' as a landmark.".format( starsystem.name))
def to_dict(self): ans = {x.key: x.value for x in object_state(self).attrs} ans.update({ 'project': self.item.project_id if self.item else None, }) return ans
def to_dict(self, with_shortcode=False): ans = {x.key: x.value for x in object_state(self).attrs} if with_shortcode: ans['lower_shortcode'] = self.lower_shortcode() return ans
def state(self): return object_state(self)
def to_dict(self): ans = {x.key:x.value for x in object_state(self).attrs} ans.update({ 'project': self.item.project_id if self.item else None, }) return ans
def to_dict(self, with_shortcode=False): ans = {x.key:x.value for x in object_state(self).attrs} if with_shortcode: ans['lower_shortcode'] = self.lower_shortcode() return ans
def safe_insert(obj, session, log_fail=False, **kwargs): with session_scope(session, **kwargs) as s: s.add(obj) result = getattr(object_state(obj), 'persistent', False) if not result and log_fail: generate_fail_log_from_obj(logger, obj)