def import_existing(database_file): """ Import posts from an older Simple database """ try: conn = sqlite3.connect(database_file) except sqlite3.Error: print("Cannot open file {0}! Are you sure it is a Sqlite database?". format(database_file)) return try: from simple.app import orm, Post from simple.util import slugify except Exception: print("Error: Cannot import simple. Have you created a config file?") return if not prompt_bool( "This will erase any existing posts inside 'posts.db'. Are you sure?" ): return with orm.db_session(): for post in orm.select(p for p in Post): post.delete() orm.flush() cursor = conn.cursor() cursor.execute("SELECT * FROM posts;") for data in cursor.fetchall(): page = old_page(*data) post = Post(id=page.id, title=page.title, slug=page.slug, created=dateutil.parser.parse(page.created_at), posted=dateutil.parser.parse(page.created_at), modified=dateutil.parser.parse(page.updated_at), content=page.text or " ", is_special_page=False, draft=page.draft == 1) pass orm.commit()
def fake_posts(count=10): """ Create an empty database and fill it with fake posts """ try: from faker import Faker fake = Faker() except ImportError: print( "Error: module fake-factory is not installed. Cannot create fake data" ) return try: from simple.app import orm, Post from simple.util import slugify except Exception: print("Error: Cannot import simple. Have you created a config file?") return start_time = time.time() with orm.db_session(): if orm.select(p for p in Post).count() == 0: for i in range(count): title = fake.lorem.sentence(100)[:100] content = fake.lorem.paragraphs(20) created = fake.date_time_this_month() posted = fake.date_time_this_month() modified = fake.date_time_this_month() p = Post(title=title, slug=slugify(title), created=created, posted=posted, modified=modified, is_special_page=False, content=content, draft=random.choice((True, False))) orm.commit() print("Created {0} posts in {1}".format(count, time.time() - start_time))
def fake_posts(count=10): """ Create an empty database and fill it with fake posts """ try: from faker import Faker fake = Faker() except ImportError: print("Error: module fake-factory is not installed. Cannot create fake data") return try: from simple.app import orm, Post from simple.util import slugify except Exception: print("Error: Cannot import simple. Have you created a config file?") return start_time = time.time() with orm.db_session(): if orm.select(p for p in Post).count() == 0: for i in range(count): title = fake.lorem.sentence(100)[:100] content = fake.lorem.paragraphs(20) created = fake.date_time_this_month() posted = fake.date_time_this_month() modified = fake.date_time_this_month() p = Post( title=title, slug=slugify(title), created=created, posted=posted, modified=modified, is_special_page=False, content=content, draft=random.choice((True, False)) ) orm.commit() print("Created {0} posts in {1}".format(count, time.time()-start_time))
def import_existing(database_file): """ Import posts from an older Simple database """ try: conn = sqlite3.connect(database_file) except sqlite3.Error: print("Cannot open file {0}! Are you sure it is a Sqlite database?".format(database_file)) return try: from simple.app import orm, Post from simple.util import slugify except Exception: print("Error: Cannot import simple. Have you created a config file?") return if not prompt_bool("This will erase any existing posts inside 'posts.db'. Are you sure?"): return with orm.db_session(): for post in orm.select(p for p in Post): post.delete() orm.flush() cursor = conn.cursor() cursor.execute("SELECT * FROM posts;") for data in cursor.fetchall(): page = old_page(*data) post = Post(id=page.id, title=page.title, slug=page.slug, created=dateutil.parser.parse(page.created_at), posted=dateutil.parser.parse(page.created_at), modified=dateutil.parser.parse(page.updated_at), content=page.text or " ", is_special_page=False, draft=page.draft == 1) pass orm.commit()