def user(userid): try: if request.method == 'GET': cg_user = dbm.fetch_user_info(userid=userid) if cg_user: return jsonify(cg_user) return not_found() elif request.method == 'PUT': if request.json: # Verify request json contains needed parameters if not ('uname' or 'ufirstname' or 'ulastname' or 'uemail' or 'uphone' or 'udob' in request.json): return missing_parameters_error() # Verify that parameters are valid errors = validate_update_account_data(request.json) if errors: return jsonify({'Errors': errors}), 400 # Update user account: if dbm.update_user_account(userid, request.json): response = jsonify(request.json) response.status_code = 201 return response return not_found() else: return bad_request() except Exception as e: print e return internal_server_error()
def delete_from_user_wish_list(userid, productid): try: if request.method == 'DELETE': in_wish_list = dbm.wish_list_contains( productid=productid, userid=userid)['product_in_wishlist'] if in_wish_list: dbm.remove_from_wish_list(productid=productid, userid=userid) wish_list = dbm.fetch_user_wish_list(userid=userid) return jsonify(wish_list) else: not_found() elif request.method == 'POST': in_wish_list = dbm.wish_list_contains( productid=productid, userid=userid)['product_in_wishlist'] if not in_wish_list: dbm.add_product_to_user_wishlist(productid=productid, userid=userid) wish_list = dbm.fetch_user_wish_list(userid=userid) return jsonify(wish_list) else: return jsonify({ 'error': 'Product {0} is already in user {1} wish list.'.format( productid, userid) }), 400 except Exception as e: print e return internal_server_error()
def get_comment_file(comment_id): comment = Comment.query.get(comment_id) if comment is None: return not_found('Comment does not exist') filelocate = comment.filelocate if filelocate is None: return not_found('File does not exist') return send_from_directory(UPLOAD_FOLDER, filelocate)
def get_user_picture(user_id): user = User.query.filter_by(username=user_id).first() if user is None: return not_found('User does not exist') pictureName = user.pictureName pictureLocate = user.pictureLocate if picture is None: return not_found('Picture does not exist') return send_from_directory(UPLOAD_FOLDER, pictureLocate)
def home_latest_prod(): try: result = dbm.fetch_latest_products() if result: return jsonify(result) else: not_found() except Exception as e: print e return internal_server_error()
def get_users_in_group(group_id): group = Group.query.get(group_id) if group is None: return not_found('Group does not exist') if g.current_user.id != group.create_user: return forbidden('User does not create this group') users = group.users.all() if users is None: return not_found('User in group is not exist') return jsonify({'users': [user.to_json() for user in users]})
def get_news_file(id): news = News.query.get(id) if news is None: return not_found('News does not exist') if news.group is not None and g.current_user not in news.house.users\ and g.current_user.id != news.house.create_user: return forbidden('User does not in this group') filelocate = news.filelocate if filelocate is None: return not_found('File does not exist') return send_from_directory(UPLOAD_FOLDER, filelocate)
def get_user_news(user_id): user = User.query.filter_by(username=user_id).first() if user is None: return not_found('User does not exist') page = request.args.get('page', 1, type=int) per_page = request.args.get('per_page', 20, type=int) pagination = user.news.order_by(News.created_at.desc()).paginate( page, per_page, error_out=False) pag_news = pagination.items if pagination.total < 1: # 아무것도 없을 경우 return not_found('User does not have news') return jsonify({'news': [news.to_json() for news in pag_news\ if news.group is None or g.current_user in news.house.users]})
def get_news_in_group(group_id): group = Group.query.get(group_id) if group is None: return not_found('Group does not exist') if g.current_user not in group.users: return forbidden('User does not in this group') page = request.args.get('page', 1, type=int) per_page = request.args.get('per_page', 20, type=int) pagination = group.news.order_by(News.created_at.desc()).paginate( page, per_page, error_out=False) pag_news = pagination.items if pagination.total < 1: return not_found('News is not exist') return jsonify({'news': [news.to_json() for news in pag_news]})
def get_notice_news(): notice_news = News.query.filter_by(notice=True).order_by( News.created_at.desc()).all() if notice_news is None: return not_found('News does not exist') return jsonify({'notice' : [news.to_json() for news in notice_news\ if news.group is None or g.current_user in news.house.users]}), 200
def user_payment(userid): try: if request.method == 'GET': payment_method = dbm.fetch_user_payment_methods(userid) if payment_method: return jsonify(payment_method) return not_found() elif request.method == 'POST': if request.json: payment_keys = post_payment_keys payment_keys.append('ppreferred') for key in payment_keys: if key not in request.json: return jsonify( {"Errors": "Missing {0} in request.".format(key)}), 400 errors = validate_payment(request.json) if errors: return jsonify({'Errors': errors}), 400 billing_addressid = dbm.fetch_user_preferences( userid)['billing_address']['aid'] if billing_addressid: pid = dbm.create_user_payment_method( userid, request.json, billing_addressid) return jsonify({'payment_methodid': pid}), 201 else: return jsonify({ 'error': 'Preferred Billing Address Not Found For User {0}'. format(userid) }), 400 return bad_request() except Exception as e: print e.message return internal_server_error()
def user_address(userid): try: if request.method == 'GET': address = dbm.fetch_user_address(userid) if address: return jsonify(address) return not_found() elif request.method == 'POST': if request.json: address_keys = post_address_keys address_keys.append('apreferred') address_keys.append('atype') for key in address_keys: if key not in request.json: return missing_parameters_error() errors = validate_address(request.json) if errors: return jsonify({'Errors': errors}), 400 if request.json[ 'atype'] == 'billing' and 'pid' not in request.json: return jsonify({'Errors': "Missing Payment Method."}), 400 new_address_id = dbm.create_user_address(userid, request.json) return jsonify({'aid': new_address_id}), 201 else: return missing_parameters_error() except Exception as e: print e.message return internal_server_error()
def put_group(group_id): old_group = Group.query.get(group_id) if old_group is None: # 그룹이 존재 하지 않을 경우 return not_found('group does not exist') if request.json is None: # 요청이 올바르지 않은 경우 return bad_request('JSON request is invaild') if g.current_user.id != old_group.create_user: return forbidden('User cannot modify group') name = request.json.get('name') description = request.json.get('description') users = request.json.get('users') if users is None or users == []: old_group.users = [g.current_user] user_list = list(users) old_group.name = name old_group.description = description if type(user_list ) == list and user_list is not None and len(user_list) >= 1: old_group.users = [ User.query.filter_by(username=user_name).first() for user_name in user_list\ if User.query.filter_by(username=user_name).count() != 0 ] db.session.commit() return jsonify(old_group.to_json())
def get_group(group_id): group = Group.query.get(group_id) if group is None: return not_found('group does not exist') if g.current_user.id != group.create_user: return forbidden('User does not create this group') return jsonify(group.to_json())
def user_preferences(userid): try: if request.method == 'GET': preferences = dbm.fetch_user_preferences(userid) if preferences: return jsonify(preferences) return not_found() elif request.method == 'PUT': if ('shipping_addressid' or 'billing_addressid' or 'cid') not in request.json: return missing_parameters_error() errors = validate_user_preferences(request.json, userid) if errors: return jsonify({'errors': errors}), 400 if 'shipping_addressid' in request.json: dbm.update_user_preferred_shipping( request.json['shipping_addressid'], userid) if 'billing_addressid' in request.json: dbm.update_user_preferred_billing( request.json['billing_addressid'], userid) if 'cid' in request.json: dbm.update_user_preferred_payment(request.json['cid'], userid) preferences = dbm.fetch_user_preferences(userid) if preferences: return jsonify(preferences) return bad_request() except Exception as e: print e.message return internal_server_error()
def search_allmight(): startpoint = request.args.get('startpoint') endpoint = request.args.get('endpoint') context = request.args.get('context') context = context.lower() if startpoint is None: startpoint = News.query.order_by(News.id.asc()).first().created_at if endpoint is None: endpoint = News.query.order_by(News.id.desc()).first().created_at page = request.args.get('page', 1, type=int) per_page = request.args.get('per_page', 60, type=int) search_result = News.query\ .filter(endpoint > News.created_at)\ .filter(News.created_at > startpoint).order_by(News.id.desc())\ .filter(News.parsed_context.like('%'+context+'%'))\ .paginate(page, per_page, error_out=False) pag_result = pagination.items if context is None: # 특정 구문이 주어지지 않은 경우 return jsonify({'news':[news.to_json() for news in pag_result\ if news.house is None or g.current_user in news.house.users\ or g.current_user.id == news.house.create_user]}) if pag_result is None: return not_found('News does not exist') return jsonify({'news':[news.to_json() for news in pag_result\ if news.house is None or g.current_user in news.house.users\ or g.current_user.id == news.house.create_user]})
def get_comments_comment(comment_id): comment = Comment.query.get(comment_id) if comment is None: return not_found('Comment does not exist') return jsonify({ 'reply_comments': [reply_comment.to_json() for reply_comment in comment.comments] })
def delete_notice_news(news_id): news = News.query.filter_by(id=news_id).first() if news is None: return not_found('News does not exist') if g.current_user.id != news.author_id: return forbidden('Cannot chang other user\'s news') news.notice = False return jsonify(news.to_json())
def test_resource_not_found_on_put(self): resource_id = self.create_resource() self.delete_resource(resource_id) error = e.not_found(resource=self.resource) response = client.put(self.generate_url(resource_id), param='param') response.assert_match(404, error)
def get_ratings(): try: rating = dbm.fetch_esrb_ratings() if rating: return jsonify(rating) return not_found() except Exception as e: print e return internal_server_error()
def get_product(productid): try: product = dbm.fetch_product(productid) if product: return jsonify(product) return not_found() except Exception as e: print e return internal_server_error()
def get_platform(platformid): try: for p in platform_list: if p['platformid'] == platformid: return jsonify(p) return not_found() except Exception as e: print e return internal_server_error()
def shipment_fees(): try: result = dbm.fetch_shipment_fees() if result: return jsonify(result) return not_found() except Exception as e: print e return internal_server_error()
def get_all_status(): try: status = dbm.fetch_all_status() if status: return jsonify(status) return not_found() except Exception as e: print e return internal_server_error()
def get_orders(): try: orders = dbm.fetch_all_orders() if orders: return jsonify(orders) return not_found() except Exception as e: print e return internal_server_error()
def get_users(): try: users = dbm.fetch_users() if users: return jsonify(users) return not_found() except Exception as e: print e return internal_server_error()
def user_shipping_address(userid): try: address = dbm.fetch_user_shipping_address(userid) if address: return jsonify(address) return not_found() except Exception as e: print e return internal_server_error()
def get_genre(): try: genre = dbm.fetch_all_genre() if genre: return jsonify(genre) return not_found() except Exception as e: print e return internal_server_error()
def get_categories(): try: categories = dbm.fetch_all_categories() if categories: return jsonify(categories) return not_found() except Exception as e: print e return internal_server_error()
def get_platforms(): try: platforms = dbm.fetch_all_platforms() if platforms: return jsonify(platforms) return not_found() except Exception as e: print e return internal_server_error()
def get_products(): try: products = dbm.fetch_all_products() if products: return jsonify(products) return not_found() except Exception as e: print e return internal_server_error()
def test_resource_does_not_exist_on_get(self): resource_id = self.create_resource() self.delete_resource(resource_id) url = self.generate_url(resource_id) error = e.not_found(resource=self.resource) response = client.get(url) response.assert_match(404, error)
def test_dissociation_when_not_associated(self): with self.generated_resources() as (left_id, right_id): error = e.not_found(resource=self.right_resource) response = self.dissociate_resources(left_id, self.FAKE_ID) response.assert_match(404, error)
def test_get_association_when_left_does_not_exist(self): with self.generated_resources() as (left_id, right_id): error = e.not_found(resource=self.left_resource) response = self.get_association(self.FAKE_ID, right_id) response.assert_match(404, error)
def not_found_error(e): return not_found('item not found')