This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.py
73 lines (57 loc) · 2.59 KB
/
items.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import copy
import json
import io
import uuid
from urllib.parse import urlparse
from urllib.request import urlopen
from bson import json_util
from PIL import Image
from flask import Blueprint, request, jsonify
from view_utils import get_headers, get_default_options_response
from config import db, site_url
items_blueprint = Blueprint('items', __name__)
@items_blueprint.route("/<item_id>/", methods=['GET'])
def item_detail(item_id):
item = db.items.find_one({"urlid": f"{site_url}/items/{item_id}/"})
if item is None:
return "item with this urlid not found", 404
return jsonify(json.loads(json_util.dumps(item))), 200, get_headers({'Content-Type': 'application/json'})
@items_blueprint.route("/", methods=['GET', 'POST', 'DELETE', 'OPTIONS'])
def items():
if request.method == 'OPTIONS':
return get_default_options_response(request)
if request.method == 'POST':
json_data = copy.deepcopy(request.get_json())
if "_id" in json_data:
json_data.pop("_id")
if "type" not in json_data or len(json_data["type"]) == 0:
json_data["type"] = "item"
if "urlid" not in json_data or len(json_data["urlid"]) == 0:
json_data["urlid"] = f"{site_url}/items/{str(uuid.uuid4())}/"
# TODO: process spritesheet
"""
if "foaf:depiction" in json_data and len(json_data["foaf:depiction"]) > 0:
# don't try to read images from own site
if not urlparse(json_data["foaf:depiction"]).netloc == urlparse(site_url).netloc:
fd = urlopen(json_data["foaf:depiction"])
image_file = io.BytesIO(fd.read())
im = Image.open(image_file)
im.thumbnail((512, 512), Image.Resampling.LANCZOS)
filename = str(uuid.uuid4()) + ".png"
im.save(f'./images/{filename}')
json_data["foaf:depiction"] = site_url + "/images/" + filename
"""
db.items.find_one_and_replace(
{"urlid": json_data["urlid"]},
json_data,
upsert=True
)
return jsonify(json_data), 201, get_headers({'Content-Type': 'application/json'})
if request.method == 'DELETE':
json_data = request.get_json()
if "urlid" not in json_data:
return "urlid key is required for DELETE", 400
db.items.find_one_and_delete({"urlid": json_data["urlid"]})
return "", 204, get_headers()
items = list(db.items.find({"type": "item"}))
return jsonify(json.loads(json_util.dumps(items))), 200, get_headers({'Content-Type': 'application/json'})