def upload(): try: uploaded_files = request.files.getlist("files[]") filenames = img_upload(uploaded_files) if len(filenames) > 0: urls = ['/images/'+x for x in filenames] return prep_response({"files_url": urls}) except: pass return prep_response('Upload error', 500)
def get_items_comments(user_name, oid): model = DOMAIN['comments'] page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) ret = model.get_index({"item_id": oid}, sort=('created', 1), per_page=per_page, page=page) return prep_response(ret)
def upload_icon(): try: user = authorized_user() uploaded_files = request.files.getlist("files[]") if len(uploaded_files) > 1: uploaded_files = [uploaded_files[-1]] filenames = img_upload(uploaded_files) if len(filenames) > 0: # removed old image if user['user_icon'] != 'default.png': os.remove(os.path.join(app.config['UPLOAD_FOLDER'], user['user_icon'])) DOMAIN['users'].patch(user['_id'], {'user_icon': filenames[-1]}) return prep_response({}) except Exception as e: logger.debug(str(e)) return prep_response('Upload error', 500)
def get_stock_user(oid): model = DOMAIN['users'] query = { 'stocks': oid } page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) ret = model.get_index(query, per_page=per_page, page=page) return prep_response(ret)
def __rest_collection(collection, query=dict(), sort=("created", -1), per_page=0, page=0): if collection in DOMAIN: model = DOMAIN[collection] try: if request.method == 'GET': ret = model.get_index(query, sort, per_page, page) logger.debug(ret) return prep_response(ret) elif request.method == 'POST': data = parse_request(request.form) ret = model.post(data) return prep_response(ret, 201) except ValueError as e: logger.debug(e) return prep_response(str(e), 400) except Exception as e: logger.debug(e) return prep_response(str(e), 500) abort(404)
def __rest_document(collection, oid): if collection in DOMAIN: model = DOMAIN[collection] try: if request.method == "GET": ret = model.get_by_id(oid) return prep_response(ret) elif request.method == "POST": data = parse_request(request.form) ret = model.patch(oid, data) return prep_response(ret) elif request.method == "DELETE": model.delete(oid) return prep_response(dict(), 204) except ValueError as e: return prep_response(str(e), 400) except Exception as e: logger.debug(e) return prep_response(str(e), 500) abort(404)
def auth_login(): """ :post_param {string} identify: required :post_param {string} password: required :return {number} code: status code :return {string} token: jwt :return {object} user: user info :desc: user login and get token, user data. """ try: data = parse_request(request.form) user = login(data['identify'], data['password']) if user: return prep_response(user) return prep_response("invalid identify or password", 401) except Exception as e: logger.debug(e) return prep_response("invalid params", 400)
def get_stocked_items(user_name): user = authorized_user() model = DOMAIN['items'] item_ids = [ObjectId(oid) for oid in user['stocks']] query = { 'status': 'published', '_id': {'$in': item_ids} } page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) ret = model.get_index(query, per_page=per_page, page=page) return prep_response(ret)
def get_user_icon(user_name): """ get user's icon :param user_name: :return: """ user = DOMAIN['users'].get_by_identify(user_name) if user: img_path = os.path.join(app.config['UPLOAD_FOLDER'], user['user_icon']) return send_file(img_path) else: return prep_response('Not Found', 404)
def authenticated_users_items(): user = authorized_user() model = DOMAIN['items'] status = request.args.get('status') page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) query = { 'user_id': user['_id'], 'status': status } items = model.get_index(query, page=page, per_page=per_page) logger.debug(items["page"]) return prep_response(items)
def get_feeds(user_name): user = authorized_user() model = DOMAIN['items'] query = { 'status': 'published', '$or': [ {'user_id': {'$in': user['following_users']}}, {'tags': {'$in': user['following_tags']}}, ], } page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) ret = model.get_index(query, per_page=per_page, page=page) return prep_response(ret)
def create_account(): try: data = parse_request(request.form) if set(['user_email', 'user_name', 'password']) > data.keys() \ or data['user_name'] == '' or data['password'] == '': return prep_response('invalid param', 400) if len(app.config["ALLOW_DOMAIN"]) > 1 and data["user_email"].split("@")[1] not in app.config["ALLOW_MAIL"]: return prep_response('invalid email', 400) model = DOMAIN['users'] user = model.get_index({"$or": [{"user_name": data["user_name"]}, {'user_email': data['user_email']}]}) if user["count"] > 0: return prep_response("existed user name or email address.", 400) user = { "user_name": data['user_name'], "user_email": data["user_email"], 'user_icon': "default.png", "password": password_encryption(data['password']), "profile": { "first_name": '', "last_name": '', "organization": '', "description": '' }, 'stocks': [], 'following_tags': [], 'following_users': [], "status": "inactive" } if app.config["MAIL_AUTH"]: oid = model.post(user) if oid: # send token by mail data["_id"] = oid send_token(data) ret = {"oid": oid, "action": 'mail_auth'} else: return prep_response('db error', 500) else: user["status"] = "active" oid = model.post(user) login(data["user_email"], data['password']) ret = {"oid": oid, "action": 'login'} return prep_response(ret) except Exception as e: logger.debug(e) return prep_response('invalid param', 400)
def toggle_follow_tag(oid, operation): user = authorized_user() model = DOMAIN['users'] ret = model.toggle_array(user['_id'], 'following_tags', oid, operation) return prep_response(ret)
def toggle_stock(oid, operation): user = authorized_user() model = DOMAIN['users'] ret = model.toggle_array(user['_id'], "stocks", oid, operation) return prep_response(ret)
def decorated_function(*args, **kwargs): if check_login(): return f(*args, **kwargs) return prep_response({'message': 'This API requires authorization'}, 404)