mirror of https://github.com/logos-co/open-law.git
book star backend
This commit is contained in:
parent
53bc7810b2
commit
ddfcddeecd
|
@ -25,6 +25,7 @@ def create_app(environment="development"):
|
|||
section_blueprint,
|
||||
vote_blueprint,
|
||||
approve_blueprint,
|
||||
star_blueprint,
|
||||
)
|
||||
from app.models import (
|
||||
User,
|
||||
|
@ -55,6 +56,7 @@ def create_app(environment="development"):
|
|||
app.register_blueprint(section_blueprint)
|
||||
app.register_blueprint(vote_blueprint)
|
||||
app.register_blueprint(approve_blueprint)
|
||||
app.register_blueprint(star_blueprint)
|
||||
|
||||
# Set up flask login.
|
||||
@login_manager.user_loader
|
||||
|
|
|
@ -7,3 +7,4 @@ from .home import bp as home_blueprint
|
|||
from .section import bp as section_blueprint
|
||||
from .vote import bp as vote_blueprint
|
||||
from .approve import bp as approve_blueprint
|
||||
from .star import bp as star_blueprint
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
)
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from app import models as m, db
|
||||
from app.logger import log
|
||||
|
||||
bp = Blueprint("star", __name__, url_prefix="/star")
|
||||
|
||||
|
||||
@bp.route(
|
||||
"/<int:book_id>",
|
||||
methods=["POST"],
|
||||
)
|
||||
@login_required
|
||||
def star_book(book_id: int):
|
||||
book: m.Book = db.session.get(m.Book, book_id)
|
||||
if not book:
|
||||
log(log.WARNING, "Book with id [%s] not found", book_id)
|
||||
return jsonify({"message": "Book not found"}), 404
|
||||
|
||||
book_star: m.BookStar = m.BookStar.query.filter_by(
|
||||
user_id=current_user.id, book_id=book_id
|
||||
).first()
|
||||
current_user_star = True
|
||||
if book_star:
|
||||
current_user_star = False
|
||||
db.session.delete(book_star)
|
||||
db.session.commit()
|
||||
else:
|
||||
book_star = m.BookStar(user_id=current_user.id, book_id=book_id)
|
||||
log(
|
||||
log.INFO,
|
||||
"User [%s]. Add book [%s] star",
|
||||
current_user,
|
||||
book,
|
||||
)
|
||||
book_star.save()
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"stars_count": len(book.stars),
|
||||
"current_user_star": current_user_star,
|
||||
}
|
||||
)
|
|
@ -0,0 +1,58 @@
|
|||
from flask import current_app as Response
|
||||
from flask.testing import FlaskClient
|
||||
|
||||
from app import models as m
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
def test_star(client: FlaskClient):
|
||||
_, user = login(client)
|
||||
|
||||
response: Response = client.post(
|
||||
"/star/999",
|
||||
data=dict(
|
||||
positive=True,
|
||||
),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response
|
||||
assert response.status_code == 404
|
||||
assert response.json["message"] == "Book not found"
|
||||
|
||||
book = m.Book(
|
||||
label="Test Interpretation 1 Label",
|
||||
user_id=user.id,
|
||||
).save()
|
||||
|
||||
assert len(book.stars) == 0
|
||||
|
||||
response: Response = client.post(
|
||||
f"/star/{book.id}",
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response
|
||||
assert response.status_code == 200
|
||||
json = response.json
|
||||
assert json
|
||||
assert "stars_count" in json
|
||||
assert json["stars_count"] == 1
|
||||
assert "current_user_star" in json
|
||||
assert json["current_user_star"]
|
||||
assert len(book.stars) == 1
|
||||
|
||||
response: Response = client.post(
|
||||
f"/star/{book.id}",
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response
|
||||
assert response.status_code == 200
|
||||
json = response.json
|
||||
assert json
|
||||
assert "stars_count" in json
|
||||
assert json["stars_count"] == 0
|
||||
assert "current_user_star" in json
|
||||
assert not json["current_user_star"]
|
||||
assert len(book.stars) == 0
|
Loading…
Reference in New Issue