Skip to content

Commit e4a1b2c

Browse files
committed
New grid layout, fixes, images and other stuff
1 parent 9d9b390 commit e4a1b2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1298
-293
lines changed

db_create.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ CREATE TABLE "media" (
3434
"title" TEXT,
3535
"media_type_id" INTEGER,
3636
"isbn" INTEGER,
37-
"age_limit" INTEGER,
37+
"age_limit" INTEGER,
3838
"author_id" INTEGER,
39+
"image" TEXT,
3940
FOREIGN KEY("media_type_id") REFERENCES "media_types"("id"),
4041
FOREIGN KEY("author_id") REFERENCES "authors"("id"),
4142
PRIMARY KEY("id" AUTOINCREMENT)
@@ -66,4 +67,4 @@ VALUES("Student", 28),
6667
("Default", 14);
6768

6869
CREATE INDEX ix_author_name ON authors (name COLLATE NOCASE);
69-
CREATE INDEX ix_media_title ON media (title COLLATE NOCASE);
70+
CREATE INDEX ix_media_title ON media (title COLLATE NOCASE);

library_db/app.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import logging
23
from flask import Flask
34
from flask_cors import CORS
@@ -17,6 +18,7 @@ def create_app():
1718
app = Flask(__name__)
1819
CORS(app, resources={r"/api/*": {"origins": "*"}})
1920
app.config["CORS_HEADER"] = "Content-Type"
21+
app.config["UPLOAD_FOLDER"] = os.path.join(app.static_folder, "images/media/")
2022

2123
app.secret_key = "c4c4f1e8c78c2a52ee46"
2224

library_db/library.db

0 Bytes
Binary file not shown.

library_db/routes/api/other/other.py

+56-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from flask import Blueprint, session, request
1+
from pathlib import Path
22
from file_read_backwards import FileReadBackwards
3+
from flask import Blueprint, session, request, current_app, send_file
34

45
from library_db.utils.db_utils import (
56
get_media,
@@ -10,7 +11,7 @@
1011
user_query_mini,
1112
get_borrower,
1213
)
13-
from library_db.utils.utils import is_admin
14+
from library_db.utils.utils import is_admin, goodreads_search, scrape_goodreads_cover, imdb_search
1415
from library_db.logger import (
1516
get_info_logfile,
1617
get_error_logfile,
@@ -21,7 +22,11 @@
2122

2223
@other_bluep.route("/media/<int:media_id>", methods=["GET"])
2324
def media(media_id):
24-
media_info = get_media(media_id).__dict__
25+
media_info = get_media(media_id)
26+
if not media_info:
27+
return {"error": "No media with this ID found"}
28+
29+
media_info = media_info.__dict__
2530
if is_media_borrowed(media_id):
2631
media_info.update({"is_borrowed": True})
2732
media_info.update(
@@ -33,10 +38,22 @@ def media(media_id):
3338

3439
return media_info
3540

41+
@other_bluep.route("/media/image/<int:media_id>", methods=["GET"])
42+
def media_image(media_id):
43+
media_info = get_media(media_id)
44+
if not media_info:
45+
return {"error": "No media with this ID found"}
46+
47+
if media_info.image:
48+
path = Path(current_app.config["UPLOAD_FOLDER"]).joinpath(media_info.image)
49+
if path.exists():
50+
return send_file(path, mimetype="image/jpeg")
51+
52+
return {"error": "Image does not exsist for this media."}
3653

3754
@other_bluep.route("/mini_search/author", methods=["POST"])
3855
def query_authors():
39-
if not request.is_json and "query" in request.get_json():
56+
if not (request.is_json and "query" in request.get_json()):
4057
return {"error": "Unprocessable data"}, 400
4158

4259
query = request.get_json()["query"]
@@ -47,7 +64,7 @@ def query_authors():
4764

4865
@other_bluep.route("/mini_search/media", methods=["POST"])
4966
def query_media():
50-
if not request.is_json and "query" in request.get_json():
67+
if not (request.is_json and "query" in request.get_json()):
5168
return {"error": "Unprocessable data"}, 400
5269

5370
query = request.get_json()["query"]
@@ -99,3 +116,37 @@ def get_log():
99116
response.update({"log_lines": lines})
100117

101118
return response
119+
120+
121+
@other_bluep.route("/scraper/search_covers", methods=["POST"])
122+
def search_covers():
123+
if not (request.is_json and "query" in request.get_json() and "media_type" in request.get_json()):
124+
return {"error": "Unprocessable data"}, 400
125+
126+
if request.get_json().get("media_type") == "Book":
127+
try:
128+
urls = goodreads_search(request.get_json().get("query"))
129+
except:
130+
return {"error": "Search failed"}
131+
132+
with_images = False
133+
elif request.get_json().get("media_type") == "Blu-Ray" or request.get_json().get("media_type") == "DVD":
134+
urls = imdb_search(request.get_json().get("query"))
135+
with_images = True
136+
elif request.get_json().get("media_type") == "CD":
137+
return {"error": "CD-Cover Scraper not Implementet"}
138+
else:
139+
return {"error": "Invalid media type"}
140+
141+
return {"with_images": with_images, "urls": urls}
142+
143+
@other_bluep.route("/scraper/book/scrape_cover", methods=["POST"])
144+
def scrape_coverurl():
145+
if not (request.is_json and "url" in request.get_json()):
146+
return {"error": "Unprocessable data"}, 400
147+
148+
url = scrape_goodreads_cover(request.get_json().get("url"))
149+
if not url:
150+
return {"error": "Could not scrape image url"}
151+
152+
return {"image_url": url}

library_db/routes/media/media.py

+13-17
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,25 @@ def show_medialist():
4545
query_params.update({name: value})
4646

4747
table_data_prams = deepcopy(query_params)
48-
try:
49-
table_data_prams.pop("status")
50-
except KeyError:
51-
pass
5248

5349
author_query_match = re.match(AUTHOR_REGEX, query_params.get("query", ""))
5450
if author_query_match:
5551
table_data_prams.update({"author_query": author_query_match.group(1)})
5652
table_data_prams["query"] = re.sub(AUTHOR_REGEX, "", table_data_prams["query"])
5753
table_data_prams["query"] = table_data_prams["query"].strip()
5854

59-
query_count = get_media_query_count(
60-
table_data_prams.get("query", ""),
61-
table_data_prams.get("author_query", ""),
62-
table_data_prams.get("media_type", ""),
63-
)
6455

6556
table_data, sort_field, sort_dir = get_media_list(
6657
PAGE_SIZE, PAGE_SIZE * page, **table_data_prams
6758
)
68-
69-
borrow_status_filter = query_params.get("status", "all")
70-
if borrow_status_filter == "availabel":
71-
table_data = [i for i in table_data if not is_media_borrowed(i.id)]
72-
elif borrow_status_filter == "borrowed":
73-
table_data = [i for i in table_data if is_media_borrowed(i.id)]
59+
60+
61+
query_count = get_media_query_count(
62+
table_data_prams.get("query", ""),
63+
table_data_prams.get("author_query", ""),
64+
table_data_prams.get("status", "all"),
65+
table_data_prams.get("media_type", None)
66+
)
7467

7568
template_vars = get_template_vars(session)
7669
template_vars["table_data"] = table_data
@@ -84,8 +77,11 @@ def show_medialist():
8477
template_vars["page_size"] = PAGE_SIZE
8578
template_vars["url"] = request.url
8679
template_vars["update_query_params"] = update_query_params
87-
template_vars["borrow_status_filter"] = borrow_status_filter
88-
return render_template("media/medialist.html", **template_vars)
80+
template_vars["borrow_status_filter"] = query_params.get("status", "all")
81+
if session.get("grid", True):
82+
return render_template("media/medialist_grid.html", **template_vars)
83+
else:
84+
return render_template("media/medialist_table.html", **template_vars)
8985

9086

9187
@media_bluep.route("/media/return", methods=["POST"])

0 commit comments

Comments
 (0)