Browse Source

working on like button functionality and many to many table relationship

master
grumbulon 7 months ago
parent
commit
12372fc203
  1. 4
      MANIFEST.in
  2. 10
      lush.egg-info/PKG-INFO
  3. 22
      lush.egg-info/SOURCES.txt
  4. 1
      lush.egg-info/dependency_links.txt
  5. 1
      lush.egg-info/not-zip-safe
  6. 1
      lush.egg-info/requires.txt
  7. 1
      lush.egg-info/top_level.txt
  8. 45
      lush/blog.py
  9. 14
      lush/schema.sql
  10. 2
      lush/static/style.css
  11. 4
      lush/templates/base.html
  12. 14
      lush/templates/blog/index.html
  13. 12
      setup.py
  14. 38
      tests/conftest.py
  15. 8
      tests/data.sql

4
MANIFEST.in

@ -0,0 +1,4 @@
include lush/schema.sql
graft lush/static
graft lush/templates
global-exclude *.pyc

10
lush.egg-info/PKG-INFO

@ -0,0 +1,10 @@
Metadata-Version: 1.0
Name: lush
Version: 0.1.0
Summary: UNKNOWN
Home-page: UNKNOWN
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Description: UNKNOWN
Platform: UNKNOWN

22
lush.egg-info/SOURCES.txt

@ -0,0 +1,22 @@
LICENSE
MANIFEST.in
README.md
setup.py
lush/__init__.py
lush/auth.py
lush/blog.py
lush/db.py
lush/schema.sql
lush.egg-info/PKG-INFO
lush.egg-info/SOURCES.txt
lush.egg-info/dependency_links.txt
lush.egg-info/not-zip-safe
lush.egg-info/requires.txt
lush.egg-info/top_level.txt
lush/static/style.css
lush/templates/base.html
lush/templates/auth/login.html
lush/templates/auth/register.html
lush/templates/blog/create.html
lush/templates/blog/index.html
lush/templates/blog/update.html

1
lush.egg-info/dependency_links.txt

@ -0,0 +1 @@

1
lush.egg-info/not-zip-safe

@ -0,0 +1 @@

1
lush.egg-info/requires.txt

@ -0,0 +1 @@
flask

1
lush.egg-info/top_level.txt

@ -0,0 +1 @@
lush

45
lush/blog.py

@ -11,6 +11,17 @@ bp = Blueprint('blog', __name__)
@bp.route('/')
def index():
db = get_db()
posts = db.execute(
'SELECT p.id, title, body, created, author_id, username, likes'
' FROM post p JOIN user u ON p.author_id = u.id'
' ORDER BY created DESC'
).fetchall()
return render_template('blog/index.html', posts=posts)
@bp.route('/')
@login_required
def userposts():
db = get_db()
posts = db.execute(
'SELECT p.id, title, body, created, author_id, username'
@ -25,6 +36,7 @@ def create():
if request.method == 'POST':
title = request.form['title']
body = request.form['body']
likes = 0
error = None
if not title:
@ -35,9 +47,9 @@ def create():
else:
db = get_db()
db.execute(
'INSERT INTO post (title, body, author_id)'
' VALUES (?, ?, ?)',
(title, body, g.user['id'])
'INSERT INTO post (title, body, author_id, likes)'
' VALUES (?, ?, ?, ?)',
(title, body, g.user['id'], likes)
)
db.commit()
return redirect(url_for('blog.index'))
@ -46,7 +58,7 @@ def create():
def get_post(id, check_author=True):
post = get_db().execute(
'SELECT p.id, title, body, created, author_id, username'
'SELECT p.id, title, body, created, author_id, username, likes'
' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?',
(id,)
@ -94,4 +106,27 @@ def delete(id):
db = get_db()
db.execute('DELETE FROM post WHERE id = ?', (id,))
db.commit()
return redirect(url_for('blog.index'))
return redirect(url_for('blog.index'))
@bp.route('/like/<int:post_id>/<action>')
@login_required
def like_post(id):
db = get_db()
if request.form['like_button'] == 'Like this post':
post_current_likes = get_db().execute(
'SELECT p.id, likes'
' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?',
(id,)
).fetchone()
post_current_likes += 1
db.execute(
'UPDATE post SET likes = ?'
' WHERE id = ?',
(post_current_likes, id)
)
db.commit()
db.execute()

14
lush/schema.sql

@ -1,17 +1,27 @@
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS post;
CREATE TABLE user (
CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
liked
);
CREATE TABLE post (
CREATE TABLE IF NOT EXISTS post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
author_id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
body TEXT NOT NULL,
likes INTEGER DEFAULT 0,
FOREIGN KEY (author_id) REFERENCES user (id)
);
CREATE TABLE IF NOT EXISTS post_likers (
id INTEGER PRIMARY KEY,
post_id INTEGER,
user_id INTEGER,
FOREIGN KEY(post_id) REFERENCES post(id),
FOREIGN KEY(user_id) REFERENCES user(id)
);

2
lush/static/style.css

@ -15,6 +15,8 @@ nav ul li a, nav ul li span, header .action { display: block; padding: 0.5rem; }
.post > header { display: flex; align-items: flex-end; font-size: 1.30em; }
.post > header > div:first-of-type { flex: auto; }
.post > header h1 { font-size: 2em; margin-bottom: 0; }
.heading > h1 {font-family: serif; color: #b1d4eb; margin: 1rem 0;}
.likes > p {font-family: serif; margin: 1rem ;}
.post .about { color: slategray; font-style: italic; }
.post .body { white-space: pre-line; }
.content:last-child { margin-bottom: 0; }

4
lush/templates/base.html

@ -1,8 +1,8 @@
<!doctype html>
<title>{% block title %}{% endblock %} - lush</title>
<title>{% block title %}{% endblock %} lush</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<nav>
<h1>lush</h1>
<h1><a class="action" href="/">lush</a></h1>
<ul>
{% if g.user %}
<li><span>{{ g.user['username'] }}</span>

14
lush/templates/blog/index.html

@ -1,10 +1,16 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Posts{% endblock %}</h1>
<h1>{% block header_pubposts %}Public posts{% endblock %}</h1>
{% if g.user %}
<a class="action" href="{{ url_for('blog.create') }}">New</a>
<a class="heading" href="{{ url_for('blog.userposts') }}"> <h1>{% block header_userposts %}Your posts{% endblock %}</h1></a>
{% endif %}
{% if g.user %}
<a class="action" href="{{ url_for('blog.create') }}">New</a>
{% endif %}
{% endblock %}
{% block content %}
@ -20,6 +26,10 @@
{% endif %}
</header>
<p class="body">{{ post['body'] }}</p>
<p class="body">{{ post['likes'] | string }} likes</p>
<form action="{{ url_for('blog.delete', id=post['id']) }}" method="post">
<input type="submit" value="Like this post" onclick="return confirm('Are you sure?');">
</form>
</article>
{% if not loop.last %}
<hr>

12
setup.py

@ -0,0 +1,12 @@
from setuptools import find_packages, setup
setup(
name='lush',
version='0.1.0',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=[
'flask',
],
)

38
tests/conftest.py

@ -0,0 +1,38 @@
import os
import tempfile
import pytest
from lush import create_app
from lush.db import get_db, init_db
with open(os.path.join(os.path.dirname(__file__), 'data.sql'), 'rb') as f:
_data_sql = f.read().decode('utf8')
@pytest.fixture
def app():
db_fd, db_path = tempfile.mkstemp()
app = create_app({
'TESTING': True,
'DATABASE': db_path,
})
with app.app_context():
init_db()
get_db().executescript(_data_sql)
yield app
os.close(db_fd)
os.unlink(db_path)
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def runner(app):
return app.test_cli_runner()

8
tests/data.sql

@ -0,0 +1,8 @@
INSERT INTO user (username, password)
VALUES
('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'),
('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79');
INSERT INTO post (title, body, author_id, created)
VALUES
('test title', 'test' || x'0a' || 'body', 1, '2018-01-01 00:00:00');
Loading…
Cancel
Save