class TestStore(BaseTest): item_schema = ItemSchema() store_schema = StoreSchema() def setUp(self): BaseTest.setUp(self) with self.app_context(): store = self.store_schema.load({"name": "amazon"}) store.upsert_store() new_item = self.item_schema.load({ "name": "test_name", "price": "23.45", "store_id": 1 }) new_item.upsert_item() def test_crud(self): with self.app_context(): store = StoreModel.find_store_by_id(1) self.assertIsNotNone(store) self.assertEqual(store, StoreModel.find_store_by_name("amazon")) self.assertEqual(1, store.id) self.assertEqual("amazon", store.name) self.assertListEqual(["test_name"], [item.name for item in store.items.all()]) self.assertDictEqual( { "id": 1, "items": [{ "id": 1, "store_id": 1, "name": "test_name", "price": 23.45 }], "name": "amazon", }, self.store_schema.dump(store), ) store.delete() self.assertIsNone(StoreModel.find_store_by_id(1)) self.assertIsNone(StoreModel.find_store_by_name("amazon"))
class TestStore(TestCase): store_schema = StoreSchema() def test_load_store(self): new_store = self.store_schema.load({"name": "amazon"}) self.assertIsInstance(new_store, StoreModel) self.assertEqual(new_store.name, "amazon") def test_dump_store(self): new_store = self.store_schema.load({"name": "amazon"}) self.assertDictEqual( self.store_schema.dump(new_store), { "id": None, "items": [], "name": "amazon" }, )
from flask_restful import Resource from flask_jwt_extended import jwt_required from schemas.store import StoreSchema from models.store import StoreModel store_schema = StoreSchema() store_list_schema = StoreSchema(many=True) class Store(Resource): @classmethod @jwt_required def get(cls, name: str): store = StoreModel.find_by_name(name) if store: return store_schema.dump(store) return {"message": "Store not found."}, 404 @classmethod @jwt_required def post(cls, name: str): if StoreModel.find_by_name(name): return ( { "message": 'A store with name "{}" already exists.'.format(name) }, 400, ) store = StoreModel(name=name)
from flask_restful import Resource from flask import request from flask_jwt_extended import fresh_jwt_required, jwt_required, get_jwt_claims from marshmallow import ValidationError from models.store import StoreModel from schemas.store import StoreSchema store_schema = StoreSchema() stores_schema = StoreSchema(many=True) STORE_NOT_FOUND = "The store with name {} not found." STORE_INSERT_SUCCESSFUL_INFO = "The store with name {} inserted successfully." STORE_ALREADY_EXIST = "The store name {} is already exist." STORE_UPDATE_SUCCESSFUL = "The store updation successful." STORE_DELETE_SUCCESSFUL = "The store name {} is deleted successfully." ADMIN_PRIVILEGE_REQUIRE_ERROR = "You are not admin. Only authorised person can take this action" class Store(Resource): @classmethod def get(cls, name: str): store = StoreModel.find_by_name(name) if not store: return {"Message": STORE_NOT_FOUND.format(name)} return store_schema.dump(store), 201 @classmethod @jwt_required def post(cls, name: str):
from flask import request from flask_restful import Resource from flask_jwt_extended import ( create_access_token, create_refresh_token, jwt_refresh_token_required, jwt_required, get_jwt_identity, ) from werkzeug.security import safe_str_cmp from libs.util import admin_method from models.store import StoreModel from schemas.store import StoreSchema store_schema = StoreSchema() STORE_NOT_FOUND = "Store not found" ERROR_EMAIL_EXISTS = "Store email is already exists." ERROR_ID_EXISTS = "Store id is already exists." IDENTIFICATION_ERROR = "Store credentials are incorrect" ERROR_SAVING_STORE = "Could not perform store save" SUCCESS_SAVING_STORE = "Store has been successfully saved" SUCCESS_LOGOUT = "Logged out successfully" ERROR_DELETING_STORE = "Could not perform store deletion" SUCCESS_DELETING_STORE = "Store deleted completely" class StoreRegister(Resource): method_decorators = {'post': [admin_method]}
from flask_restful import Resource from models.store import StoreModel from flask_jwt_extended import jwt_required, fresh_jwt_required from schemas.store import StoreSchema storeSchema = StoreSchema() storeListSchema = StoreSchema(many=True) class Store(Resource): @classmethod def get(cls, name): store = StoreModel.find_by_name(name) if store: return storeSchema.dump(store), 200 return {'message': 'Store Not Found'}, 404 @classmethod @jwt_required def post(cls, name): if StoreModel.find_by_name(name): return { 'message': "A store with name '{}' already exists!".format(name) }, 400 store = StoreModel(name=name) try: store.save_to_db() except: return {'message': 'An error occured creating the store.'}, 500
from flask_restful import Resource from marshmallow import EXCLUDE from models.store import StoreModel from schemas.store import StoreSchema NAME_ALREADY_EXISTS = "A store with name '{}' already exists." ERROR_INSERTING = "An error occurred while inserting the store." STORE_NOT_FOUND = "Store not found." STORE_DELETED = "Store deleted." store_schema = StoreSchema(unknown=EXCLUDE) store_list_schema = StoreSchema(many=True) class Store(Resource): @classmethod def get(cls, name: str): store = StoreModel.find_by_name(name) if store: return store_schema.dump(store), 200 return {"message": STORE_NOT_FOUND}, 404 @classmethod def post(cls, name: str): if StoreModel.find_by_name(name): return {"message": NAME_ALREADY_EXISTS.format(name)}, 400 store = StoreModel(name=name) try: store.save_to_db()