def get_all_posts(self): REQUEST = """SELECT rowid, name, title, content, user, updated, published FROM posts""" results = [] for row in self.cursor.execute(REQUEST): post = Post.from_result(self, row) results.append(post) return results
def get_published_posts(self, year=None, month=None, page=None): REQUEST_DATE = """SELECT rowid, name, title, content, user, updated, published FROM posts WHERE (published > ? AND published < ?) ORDER BY published DESC""" REQUEST = """SELECT rowid, name, title, content, user, updated, published FROM posts WHERE (published > 0) ORDER BY published DESC""" def datetime_to_epoc(dt): return int(dt.strftime("%s")) results = [] if year or month: if year and month: dt_start = datetime(year, month, 1) epoc_start = datetime_to_epoc(dt_start) month += 1 if month > 12: year = year + 1 month = 1 dt_end = datetime(year, month, 1) epoc_end = int(dt_end.strftime("%s")) else: dt_start = datetime(year, 1, 1) epoc_start = int(dt_start.strftime("%s")) year += 1 dt_end = datetime(year, 1, 1) epoc_end = int(dt_end.strftime("%s")) for row in self.cursor.execute(REQUEST_DATE, [epoc_start, epoc_end]): post = Post.from_result(self, row) results.append(post) else: # get all for row in self.cursor.execute(REQUEST).fetchall(): post = Post.from_result(self, row) results.append(post) return results
def get_published_post(self, year, month, post_name): REQUEST = """SELECT rowid, name, title, content, user, updated, published FROM posts WHERE (published > ? AND published < ? AND name = ?)""" dt_start = datetime(year, month, 1) epoc_start = int(dt_start.strftime("%s")) month += 1 if month > 12: year = year + 1 month = 1 dt_end = datetime(year, month, 1) epoc_end = int(dt_end.strftime("%s")) result = self.cursor.execute(REQUEST, [epoc_start, epoc_end, post_name]).fetchone() if result: return Post.from_result(self, result)
def get_post_by_id(self, id_val): REQUEST = """SELECT rowid, name, title, content, user, updated, published FROM posts WHERE (rowid=?)""" result = self.cursor.execute(REQUEST, [id_val]).fetchone() if result: return Post.from_result(self, result)