def get_raw_content(self): charlotte_root = settings.get_charlotte_root() f = open(charlotte_root / "articles/content/{slug}".format(slug=self.slug)) content = f.read() f.close() return content
def post_article(title, author, article_format, content): slug = unique_slug_from_title(title) charlotte_root = settings.get_charlotte_root() connection = sqlite3.connect(charlotte_root / "articles" / "database.db") query = \ """ INSERT INTO articles (id, title, author, format, date, slug) VALUES (null, :title, :author, :article_format, datetime('now'), :slug) """ parameters = \ { "title": title, "author": author, "slug": slug, "article_format": article_format } connection.execute(query, parameters) connection.commit() cursor = connection.execute("SELECT last_insert_rowid()") rows = cursor.fetchall() new_id = rows[0][0] f = open(charlotte_root / "articles/content/{slug}".format(slug=slug), "wb") f.write(content) f.close() connection.close()
def update_article(article_id, title=None, author=None, article_format=None, content=None): article = get_article_by_id(article_id) charlotte_root = settings.get_charlotte_root() if title is None or title == article.title: slug = article.slug else: slug = updated_unique_slug(article_id, title) old_file = article.get_file_path() os.rename(old_file, charlotte_root / "articles/content/{slug}".format(slug=slug)) if content is not None: f = open(charlotte_root / "articles/content/{slug}".format(slug=slug), "wb") f.write(content) f.close() try: connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") query = """ UPDATE articles SET title = COALESCE(:title, title), author = COALESCE(:author, author), format = COALESCE(:format, format), slug = COALESCE(:slug, slug) WHERE id = :article_id """ parameters = { "article_id": article_id, "title": title, "author": author, "format": article_format, "slug": slug, "content": content } connection.execute(query, parameters) connection.commit() finally: connection.close()
def how_many(): connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") cursor = connection.execute("SELECT COUNT(*) FROM articles") row = cursor.fetchone() connection.close() number = row[0] return number
def get_article_by_slug(slug): connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") query = "SELECT id, title, author, format, date FROM articles WHERE slug = :slug" parameters = {"slug": slug} cursor = connection.execute(query, parameters) rows = cursor.fetchall() connection.close() if len(rows) > 0: article_id, title, author, article_format, date = rows[0] return Article(article_id, title, author, article_format, date, slug) else: return None
def get_article_by_id(article_id): connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") query = "SELECT id, title, author, format, date, slug FROM articles WHERE id = :id" parameters = {"id": article_id} cursor = connection.execute(query, parameters) row = cursor.fetchone() connection.close() try: article_id, title, author, article_format, date, slug = row return Article(article_id, title, author, article_format, date, slug) except: return None
def updated_unique_slug(article_id, title): slug = slug_from_title(title) connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") cursor = connection.execute("SELECT id, slug FROM articles") rows = cursor.fetchall() connection.close() slugs = [row[1] for row in rows if row[0] != article_id] n = 2 unique_slug = slug while unique_slug in slugs: unique_slug = slug + "-" + str(n) n += 1 return unique_slug
def delete_article(article_id): try: article = get_article_by_id(article_id) article_file = article.get_file_path() connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") query = "DELETE FROM articles WHERE id = :id" parameters = {"id": article_id} connection.execute(query, parameters) connection.commit() os.remove(article_file) except Exception as exception: app.logger.error( "Could not delete article {article_id}: {exception}".format( article_id=article_id, exception=exception)) finally: connection.close()
def get_all(): connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") query = """ SELECT id, title, author, format, date, slug FROM articles ORDER BY date DESC """ cursor = connection.execute(query) rows = cursor.fetchall() connection.close() articles = [] for row in rows: article_id, title, author, article_format, date, slug = row article = Article(article_id, title, author, article_format, date, slug) articles.append(article) return articles
def get_latest_articles(skip, number): connection = sqlite3.connect(settings.get_charlotte_root() / "articles" / "database.db") query = """ SELECT id, title, author, format, date, slug FROM articles ORDER BY date DESC LIMIT :skip, :number """ parameters = {"skip": skip, "number": number} cursor = connection.execute(query, parameters) rows = cursor.fetchall() connection.close() articles = [] for row in rows: article_id, title, author, article_format, date, slug = row article = Article(article_id, title, author, article_format, date, slug) articles.append(article) return articles
def get_file_path(self): charlotte_root = settings.get_charlotte_root() return charlotte_root / "articles/content/{slug}".format( slug=self.slug)