Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from models.author import Author
from models.post import Post


p1 = Post("1", "Hello")
Post.drop_table()
Post.create_table()
p1 = Post("Big whale", "Hello", "Whales", "Grant")

ipdb.set_trace()
2 changes: 1 addition & 1 deletion lib/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sqlite3

CONN = sqlite3.connect('company.db')
CONN = sqlite3.connect('blogger.db')
CURSOR = CONN.cursor()
25 changes: 23 additions & 2 deletions lib/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Post():
all = {}
categories = ['sports', 'news', 'pop', 'nature']

def __init__(self, title, content, category, author_id=None, id = None):
def __init__(self, title, content, category, author_id, id = None):
self.id = id
self.title = title
self.content = content
Expand All @@ -24,4 +24,25 @@ def title(self, new_title):
if isinstance(new_title, str) and 0 < len(new_title) <= 20:
self._title = new_title


@classmethod
def create_table(cls):
""" Create a new table to persist the attributes of Post instances """
sql = """
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY,
title TEXT,
content TEXT,
category TEXT,
author_id INTEGER,
FOREIGN KEY (author_id) REFERENCES authors(id))
"""
CURSOR.execute(sql)
CONN.commit()

@classmethod
def drop_table(cls):
""" Drop the table to persist the attributes of Post instances """
sql = "DROP TABLE IF EXISTS posts"

CURSOR.execute(sql)
CONN.commit()