def get_products(): """ Return a list of products for the shop --- parameters: - in: header name: authorization required: true schema: type: string responses: 200: description: return a list of productos for the user's shop schema: type: array items: type: object parameters: id: type: string example: 12dwqe3f42-f423f-432-4f23-432-324f234f2 title: type: string example: any name for a product description: type: string example: any description for a product category: type: string example: any category for a product price: type: integer example: 45000 image: type: string example: any name for a product 404: description: shop not found """ user = request.user shop = storage.filter_by(Shop, 'user', user) if not shop: return jsonify(message='Shop is not found'), 404 categories = shop[0].categories prods = [] for cat in categories: for prod in cat.products: p_dict = prod.to_dict() try: with open(p_dict['image'], 'r') as img_file: p_dict['image'] = img_file.read() except Exception as e: print('Image preservation Error') print('image from a Product is no longer available') print('Try Uploading a new one') p_dict['image'] = 'no image' p_dict['category_str'] = cat.title prods.append(p_dict) return Response(json.dumps(prods), mimetype='application/json')
def formalize_user(): """ This method formalizes a temporal user into a permanent one --- parameters: - in: header name: authorization required: true schema: type: string - in: query name: username required: true schema: type: string - in: query name: passwd required: true schema: type: string responses: 200: description: Succesfully merged the user with the temporal one schema: type: object properties: message: type: string example: Success 309: description: an error occurred while merging the users schema: type: object properties: message: type: string example: Error creating user """ print(request.get_json()) username = request.get_json()['username'] passwd = username = request.get_json()['passwd'] # Check if the user exists by comparing the username # this contains the registered email existing_user = storage.filter_by(User, 'username', username) if not existing_user: user = storage.get(User, request.user) user.username = username user.passwd = passwd user.save() return jsonify(message='Success') return jsonify(message='Error creating user'), 309
def decorated(*args, **kwargs): token = request.headers.get('authorization') if not token: return jsonify({'message': 'Token is missing'}) try: data = jwt.decode(token, current_app.config['SECRET_KEY']) print('Data:', data) user = storage.filter_by(User, 'id', data['user_id'])[0] # Add a feature to handle expired tokens and CachedUsers print('User:'******'message': 'Token is invalid', 'type': 'error'}), 401 return f(*args, **kwargs)
def all_categories(): """ Return all categories for the registered user --- parameters: - in: header name: authorization required: true schema: type: string responses: 200: description: the category was saved succefully schema: type: array items: type: object parameters: id: type: string example: 1241234r34532-4523-4532-46245-456345634563 title: type: string example: any category stored description: type: string example: any description for this category """ try: shop = storage.filter_by(Shop, 'user', request.user)[0] categories = shop.categories categories = [category.to_dict() for category in categories] return Response(json.dumps(categories), mimetype='application/json') except Exception as e: print(e) return Response(json.dumps([]), mimetype='application/json')
def create_category(): """ Creates a new category linked to a shop create a shop if the user doesn't have one --- parameters: - in: header name: authorization required: true schema: type: string - in: query name: title required: true schema: type: string - in: query name: description required: true schema: type: string responses: 200: description: the category was saved succefully schema: type: object parameters: id: type: string example: 1239102941-1231290412-dfs12rf-123re-1234r223 400: description: incomplete request schema: type: object parameters: message: type: string example: incomplete request """ state = request.get_json() user = request.user # tests the input values print(state) if not state['title'] or not state['description']: return jsonify(message="incomplete request"), 400 # verify the store for the user shops = storage.filter_by(Shop, 'user', user) shop = None for shp in shops: if shp.user == user: shop = shp # if shop doesn't exists create a new instance if not shop: shop = Shop() shop.user = user shop.save() # Create the Category instance category = Category() category.title = state['title'] category.description = state['description'] category.shop = shop.id category.save() return jsonify(id=category.id)