Skip to content
Snippets Groups Projects
Commit 3723bd1f authored by radek's avatar radek
Browse files

minor refactoring

parent 64809818
No related branches found
No related tags found
No related merge requests found
Pipeline #15452 passed
...@@ -78,26 +78,4 @@ class AuctionItem (Resource): ...@@ -78,26 +78,4 @@ class AuctionItem (Resource):
#json.dump #json.dump
#added_item = jsonify(item.serialize()) #added_item = jsonify(item.serialize())
added_item = json.dumps(item.serialize(), default=str) added_item = json.dumps(item.serialize(), default=str)
return {'data' : added_item}, 201 return {'data' : added_item}, 201
\ No newline at end of file
def backend_get(item_id = None):
""" Analogue of GET method for backend use only.
Args:
id (str, optional): id for specif item, if it is not provided it will return all items. Defaults to None.
Returns:
list or dict: representation of item objects type depends on if item_id is provided or not.
"""
item = items.Item()
if not item_id:
logger.info("retriving all items! (backend only)")
query_result = item.get_all()
item_from_db = [item_from_db for item_from_db in query_result]
return item_from_db
logger.info(f"Trying to retrive a item with id: {id} (backend only)" )
try:
return item.get_item(item_id)
except ItemNotFound as err:
return err
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Basic views for Application Basic views for Application
=========================== ===========================
""" """
import logging
from flask import Blueprint, render_template, send_from_directory, redirect from flask import Blueprint, render_template, send_from_directory, redirect
...@@ -11,11 +12,12 @@ from flask_jwt_extended import ( ...@@ -11,11 +12,12 @@ from flask_jwt_extended import (
#login info handling decorators #login info handling decorators
from tjts5901.backend.logincookies import logged_in, admin_logged_in from tjts5901.backend.logincookies import logged_in, admin_logged_in
from tjts5901.backend.models.items import Item, ItemNotFound
#Get item data from db #Get item data from db
import tjts5901.backend.item_api as items import tjts5901.backend.item_api as items
logger = logging.getLogger(__name__)
# Main blueprint. # Main blueprint.
bp = Blueprint('views', __name__) bp = Blueprint('views', __name__)
...@@ -47,7 +49,7 @@ def bidding(item_id) -> str: ...@@ -47,7 +49,7 @@ def bidding(item_id) -> str:
""" """
#Get requested auction data #Get requested auction data
a = items.AuctionItem.backend_get(item_id) a = backend_get(item_id)
print("Item found", a) print("Item found", a)
if a == None: if a == None:
#redirect if item not found #redirect if item not found
...@@ -74,3 +76,25 @@ def terms(): ...@@ -74,3 +76,25 @@ def terms():
Maybe more precise terms would be useful Maybe more precise terms would be useful
""" """
return "Don't misuse this service or do anything illegal" return "Don't misuse this service or do anything illegal"
def backend_get(item_id = None):
""" Analogue of GET method for backend use only.
Args:
id (str, optional): id for specif item, if it is not provided it will return all items. Defaults to None.
Returns:
list or dict: representation of item objects type depends on if item_id is provided or not.
"""
item = Item()
if not item_id:
logger.info("retriving all items! (backend only)")
query_result = item.get_all()
item_from_db = [item_from_db for item_from_db in query_result]
return item_from_db
logger.info(f"Trying to retrive a item with id: {id} (backend only)" )
try:
return item.get_item(item_id)
except ItemNotFound as err:
return err
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment