Exemplo n.º 1
0
def add(db, anime):
    """Add an anime (or update existing).

    anime is an AnimeTree instance.

    """
    aid = anime.aid
    values = {
        'aid': aid,
        'title': anime.title,
        'type': anime.type,
        'episodecount': anime.episodecount,
    }
    if anime.startdate is not None:
        values['startdate'] = datets.to_ts(anime.startdate)
    if anime.enddate is not None:
        values['enddate'] = datets.to_ts(anime.enddate)
    with db:
        upsert(db, 'anime', ['aid'], values)
        our_anime = lookup(db, anime.aid, episode_fields=ALL)
        our_episodes = our_anime.episodes
        for episode in anime.episodes:
            add_episode(db, aid, episode)
            our_episodes = [
                ep for ep in our_episodes
                if not (ep.type == episode.type and ep.number == episode.number)
            ]
        # Remove extra episodes that we have.
        for episode in our_episodes:
            delete_episode(db, aid, episode)
Exemplo n.º 2
0
 def test_upsert_update(self):
     utils.upsert(self.db, 'mytable', ['id'], {
         'id': 1,
         'lastname': 'Teiwaz',
     })
     cur = self.db.cursor()
     cur.execute('SELECT * FROM mytable')
     self.assertEqual((1, 'Mir', 'Teiwaz'), cur.fetchone())
Exemplo n.º 3
0
 def test_upsert_multiple_key_update(self):
     utils.upsert(self.db, 'mytable', ['firstname', 'lastname'], {
         'firstname': 'Mir',
         'lastname': 'Jakuri',
         'age': 350,
     })
     cur = self.db.cursor()
     cur.execute('SELECT * FROM mytable')
     self.assertEqual(('Mir', 'Jakuri', 350), cur.fetchone())
Exemplo n.º 4
0
def add_episode(db, aid, episode):
    """Add an episode."""
    values = {
        'aid': aid,
        'type': episode.type,
        'number': episode.number,
        'title': episode.title,
        'length': episode.length,
    }
    upsert(db, 'episode', ['aid', 'type', 'number'], values)
Exemplo n.º 5
0
 def test_upsert_insert(self):
     utils.upsert(self.db, 'mytable', ['id'], {
         'id': 2,
         'firstname': 'Ion',
         'lastname': 'Preciel',
     })
     cur = self.db.cursor()
     cur.execute('SELECT * FROM mytable')
     self.assertEqual((1, 'Mir', 'Jakuri'), cur.fetchone())
     self.assertEqual((2, 'Ion', 'Preciel'), cur.fetchone())
Exemplo n.º 6
0
def set_status(
        db,
        aid: int,
        complete: Any,
        watched_episodes: int,
) -> None:
    """Set anime status."""
    upsert(db, 'cache_anime', ['aid'], {
        'aid': aid,
        'complete': 1 if complete else 0,
        'watched_episodes': watched_episodes,
    })