def put(self, branch_name, name): is_admin = Item.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 branch = BranchModel.find_by_name(branch_name) if not branch: return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400 data = Item.parser.parse_args() error_validation = validators.item_validator(**data) if error_validation['error validation']: return error_validation item = ItemModel.find_by_name_in_branch(branch.id, name) log = LogModel("update item '{}'".format(name), g.user.username, Item.admin) if item is None: item = ItemModel(name, **data) else: item.price = data['price'] item.year = data['year'] item.item_type = data['item_type'] item.vendor = data['vendor'] item.model = data['model'] item.branch_id = data['branch_id'] item.save_to_db() log.save_to_db() return item.json()
def put(self, name): is_admin = Branch.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 data = Branch.parser.parse_args() error_validation = validators.branch_validator(**data) if error_validation['error validation']: return error_validation branch = BranchModel.find_by_name(name) log = LogModel("update branch '{}'".format(name), g.user.username, Branch.admin) if branch is None: branch = BranchModel(name, **data) else: branch.country = data['country'] branch.city = data['city'] branch.postal_code = data['postal_code'] branch.street = data['street'] branch.email = data['email'] branch.phone = data['phone'] branch.save_to_db() log.save_to_db() return branch.json()
def put(self, name): is_admin = Position.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 data = Position.parser.parse_args() error_validation = validators.position_validator(**data) if error_validation['error validation']: return error_validation user = g.user if not user.verify_password(data['password']): return { 'message': 'You can not update a position because you have typed a wrong password!' }, 400 position = PositionModel.find_by_name(name) log = LogModel("update position '{}'".format(name), g.user.username, Position.admin) if position is None: position = PositionModel(name) # else: # position.name = name position.save_to_db() log.save_to_db() return position.json()
def post(self, name): # begin is_admin = Branch.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 # end if BranchModel.find_by_name(name): return {'message': "A branch with name '{}' already exists.".format(name)}, 400 data = Branch.parser.parse_args() error_validation = validators.branch_validator(**data) if error_validation['error validation']: return error_validation branch = BranchModel(name, **data) log = LogModel("add branch '{}'".format(name), g.user.username, Branch.admin) try: branch.save_to_db() log.save_to_db() except: return {'message': 'An error occurred inserting the branch.'}, 500 # Internal Server Error return branch.json(), 201
def post(self): data = Log.parser.parse_args() log = LogModel(**data) try: log.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return {"message": "Log successfully add to the db."}, 201
def post(self): data = arguments_log.parse_args() log = LogModel(**data) try: log.save_log() except: return { 'message': 'An internal error ocurred trying to save log' }, 500 return log.json(), 200
def delete(self, name): is_admin = Branch.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 branch = BranchModel.find_by_name(name) if branch: log = LogModel("remove branch '{}'".format(name), g.user.username, Branch.admin) branch.delete_from_db() log.save_to_db() return {'message': 'Branch deleted.'}
def save_data_to_db(): """function to interact with database""" if not UserModel.find_by_username(session['username']): new_user = UserModel(session['username'], generate_password_hash(session['password']), session['phone_number'], None, None, None, None, None) new_user.save_to_db() log = LogModel(session['username'], session['ip'], session['latitude'], session['longitude'], session['time'], None) log.save_to_db()
def delete(self): is_user = False try: if g.user: is_user = True except: pass data = CustomerDelete.parser.parse_args() error_validation = validators.delete_validator(**data) if error_validation['error validation']: return error_validation if is_user: user = g.user position = PositionModel.find_by_id(user.position_id) if position.name != 'admin' or not user.verify_password( data['password']): return { 'message': "You are not privileged to delete customer's account!" }, 400 customer = CustomerModel.find_by_username(data['username']) if customer: log = LogModel("remove customer '{}'".format(data['username']), g.user.username, auth.admin) customer.delete_from_db() log.save_to_db() return {'message': "Customer's account deleted."} return { 'message': "Customer '{}' account does not exist.".format( data['username']) } else: customer = g.customer if customer.username != data['username']: return { 'message': 'You can not delete your account because you have typed wrong username!' }, 400 if not customer.verify_password(data['password']): return { 'message': 'You can not delete your account because you have typed wrong password!' }, 400 log = LogModel("remove customer '{}'".format(data['username']), g.customer.username, auth.customer) customer.delete_from_db() log.save_to_db() return {'message': 'Your account is deleted.'}
def update_platform(cls, _id, p_name, p_logo, p_type, user_id, **kargs): """ 更新应用 """ try: # 除当前修改的名称以外,是否还存在要修改的名称 data = platform_col.find({"p_name": p_name, "_id": {"$ne": _id}}) if data.count() != 0: return -1001 else: data = platform_col.update({"_id": _id}, { "$set": { "p_name": p_name, "p_logo": p_logo, "p_type": p_type, "update_time": Util.timeFormat() } }) if data["ok"] == 1: log_str = "应用名称:{};应用ID:{};应用logo:{};应用类型:{}".format( p_name, _id, p_logo, p_type) log_result = LogModel.add_log("修改应用", log_str, user_id, "update") return 1 else: return 0 except Exception as e: current_app.logger.error(e) return 0
def insert_platform(cls, p_name, p_logo, p_type, user_id, **kargs): """ 添加应用 """ try: data = platform_col.find({"p_name": p_name}) if data.count() != 0: return -1001 else: p_id = str(uuid.uuid1()) platform_col.insert({ "_id": p_id, "p_name": p_name, "p_logo": p_logo, "p_type": p_type, "create_time": Util.timeFormat(), "deleted": 0, "creator_id": user_id }) log_str = "应用名称:{};应用ID:{};应用logo:{};应用类型:{}".format( p_name, p_id, p_logo, p_type) log_result = LogModel.add_log("添加应用", log_str, user_id, "insert") return 1 except Exception as e: current_app.logger.error(e) return 0
def update_test_info(cls, p_id, t_id, t_name, t_str, t_desc, user_id): """ 修改实验信息 """ try: data = test_col.find({ "p_id": p_id, "t_name": t_name, }) test_col.update({ "p_id": p_id, "_id": t_id, "t_id": { "$ne": t_id } }, { "$set": { "t_name": t_name, "t_str": t_str, "t_desc": t_desc, "update_time": Util.timeFormat() } }) log_str = "实验名称:{};实验ID:{};实验描述:{};加盐字符:{}".format( t_name, t_id, t_desc, t_str) log_result = LogModel.add_log("修改实验", log_str, user_id, "update") return 1 except Exception as e: current_app.logger.error(e) return 0
def add_test(cls, p_id, t_name, t_str, t_desc, user_id): """ 创建实验 """ try: data = test_col.find({"p_id": p_id, "t_name": t_name}) data_len = len(list(data)) if data_len != 0: return -2001 else: t_id = str(uuid.uuid1()) test_col.insert({ "_id": t_id, "p_id": p_id, "t_name": t_name, "t_str": t_str, "t_desc": t_desc, "t_status": 1, "create_time": Util.timeFormat(), "creator_id": user_id }) log_str = "实验名称:{};实验ID:{};实验描述:{};加盐字符:{}".format( t_name, t_id, t_desc, t_str) log_result = LogModel.add_log("创建实验", log_str, user_id, "insert") return 1 except Exception as e: current_app.logger.error(e) return 0
def post(self): parser = reqparse.RequestParser( ) # create parameters parser from request # define the input parameters need and its type parser.add_argument('email', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('password', required=True, type=str, help="This field cannot be left blanck") data = parser.parse_args() account = AccountModel.find_by_email(data["email"]) if account: if account.verify_password(data["password"]): token = account.generate_auth_token() log = LogModel(account.id).save_to_db() return { 'token': token.decode('ascii'), 'type': account.type, 'id': account.id }, 200 return {'message': "Password is invalid"}, 400 return { 'message': "Account with email [{}] Not found".format(data["email"]) }, 404
def delete(self, branch_name, name): is_admin = Item.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 branch = BranchModel.find_by_name(branch_name) if not branch: return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400 item = ItemModel.find_by_name_in_branch(branch.id, name) if item: log = LogModel("remove item '{}'".format(name), g.user.username, Item.admin) item.delete_from_db() log.save_to_db() return {'message': 'Item deleted.'}
def _log_month(self): year, month, day = datetime.now().year, datetime.now( ).month, datetime.now().day _, ndays = monthrange(year, month) logs = {day: 0 for day in range(1, day + 1)} for i in LogModel.find_by_month_year(year, month): logs[i.day] += 1 return logs
def post(self): try: user = g.user except: return {'message': "You are not privileged to continue!"}, 400 data = UserRegister.parser.parse_args() error_validation = validators.user_register_validator(**data) if error_validation['error validation']: return error_validation position = PositionModel.find_by_id(user.position_id) print(position) if position.name != 'admin': return { 'message': "You are not privileged to create user's account!" }, 400 if UserModel.find_by_username(data['username']): return { "message": "A user with that username already exists." }, 400 if CustomerModel.find_by_username(data['username']): return { "message": "A customer with that username already exists." }, 400 user = UserModel(**data) # user.save_to_db() log = LogModel("add user '{}'".format(data['username']), g.user.username, auth.admin) try: user.save_to_db() log.save_to_db() except: return { 'message': 'An error occurred inserting the user.' }, 500 # Internal Server Error # return {'user': user.fake_json()}, 201 # return {'users': [user.short_json() for user in UserModel.query.all()]}, 201 return {"message": "User created successfully."}, 201
def get(self): data = arguments_log.parse_args() try: return { 'logs': [log.json() for log in LogModel.get_log(data['device_id'])] } except: return {'message': 'Device not found.'}, 404
def delete(self, branch_name, name): is_admin = Car.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 branch = BranchModel.find_by_name(branch_name) if not branch: return { 'message': "Branch '{}' does not exist.".format(branch_name) }, 400 car = CarModel.find_by_name_in_branch(branch.id, name) if car: log = LogModel("remove car '{}'".format(name), g.user.username, Car.admin) car.delete_from_db() log.save_to_db() return {'message': 'Car deleted.'}
def get(self, _id): log = LogModel.find_by_id(int(_id)) is_admin = auth.is_admin() if log: if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 return log.json() return {'message': 'Log not found.'}, 404
def logout(): try: log_entries = LogModel.find_log(session['username'], session['time']) except: return unset_jwt(), 302 for logout_log in log_entries: logout_log.time_end = str(datetime.datetime.now().time())[:5] logout_log.save_to_db() return unset_jwt(), 302
def delete(cls, name: str): claims = get_jwt_claims() if not claims["is_admin"]: return {'message': gettext("admin_privileges_required")}, 401 log = LogModel.find_by_name(name) if log: log.delete_from_db() return {'message': gettext("log_deleted")}, 200 return {'message': gettext("log_not_found")}, 404
def delete(self, _id): log = LogModel.find_by_id(int(_id)) is_admin = auth.is_admin() if log: if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 log.delete_from_db() return {'message': 'Item deleted.'} return {'message': 'Log not found.'}, 404
def put(self, branch_name, name): is_admin = Car.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 branch = BranchModel.find_by_name(branch_name) if not branch: return { 'message': "Branch '{}' does not exist.".format(branch_name) }, 400 data = Car.parser.parse_args() error_validation = validators.car_validator(**data) if error_validation['error validation']: return error_validation car = CarModel.find_by_name_in_branch(branch.id, name) log = LogModel("update car '{}'".format(name), g.user.username, Car.admin) if car is None: car = CarModel(name, **data) else: car.price = data['price'] car.year = data['year'] car.car_type = data['car_type'] car.vendor = data['vendor'] car.model = data['model'] car.colour = data['colour'] car.seats = data['seats'] car.transmission = data['transmission'] car.drive = data['drive'] car.fuel = data['fuel'] car.engine_power = data['engine_power'] car.branch_id = data['branch_id'] car.save_to_db() log.save_to_db() return car.json()
def post(self, name): # begin is_admin = Position.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 # end data = Position.parser.parse_args() error_validation = validators.position_validator(**data) if error_validation['error validation']: return error_validation user = g.user # this # start if not user.verify_password(data['password']): return { 'message': 'You can not add a new position because you have typed a wrong password!' }, 400 # end if PositionModel.find_by_name(name): return { 'message': "A position with name '{}' already exists.".format(name) }, 400 position = PositionModel(name) log = LogModel("add position '{}'".format(name), g.user.username, Position.admin) try: position.save_to_db() log.save_to_db() except: return { 'message': 'An error occurred while creating the position.' }, 500 return position.json(), 201
def put(self, branch_name, name): branch = BranchModel.find_by_name(branch_name) if not branch: return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400 item = ItemModel.find_by_name_in_branch(branch.id, name) if item is None: return {'message': 'Item does not exist.'} if item.available == 0: return {"message": "Item is already reserved."}, 400 item.available = 0 is_user = Item.is_user() if is_user: position = (PositionModel.find_by_id(g.user.position_id)).name item.reserved_by = g.user.username log = LogModel("reserve item '{}'".format(name), g.user.username, position) else: item.reserved_by = g.customer.username log = LogModel("reserve item '{}'".format(name), g.customer.username, auth.customer) item.save_to_db() log.save_to_db() # return item.short_json() return {"message": "Item reserved."}
def delete(self, name): is_admin = Position.is_admin() if not is_admin: return {'message': 'You are not privileged to continue!'}, 400 data = Position.parser.parse_args() user = g.user if not user.verify_password(data['password']): return { 'message': 'You can not delete a position because you have typed a wrong password!' }, 400 position = PositionModel.find_by_name(name) if position: log = LogModel("remove position '{}'".format(name), g.user.username, Position.admin) position.delete_from_db() log.save_to_db() return {'message': 'Position deleted.'}
def get_record(): """获取操作日志 """ try: user_id = request.args.get("user_id") page = request.args.get("page", '1') current_app.logger.info("router:{},params:{}".format(request.path, request.args)) if not user_id: return JsonResponse.response(code=-1000) result = LogModel.get_list(user_id, int(page)) return JsonResponse.response(data=result) except Exception as e: current_app.logger.error(e) return JsonResponse.response(code=-1)
def delete(cls, s_id, b_id, user_id): """ 删除分桶 """ try: bucket_col.remove({"_id": b_id}) log_str = "策略ID:{};分桶ID:{}".format(s_id, b_id) log_result = LogModel.add_log("删除分桶", log_str, user_id, "remove") if bucket_col.find({"s_id": s_id}).count() == 0: stragegy_col.remove({"s_id": s_id}) return 1 except Exception as e: current_app.logger.error(e) return 0
def delete_platform(cls, p_id, user_id): """ 软删除应用 """ try: data = platform_col.remove({"_id": p_id}) test_data = test_col.remove({"p_id": p_id}) if data["ok"] == 1 and test_data["ok"] == 1: log_str = "应用ID:{};".format(p_id) log_result = LogModel.add_log("删除应用", log_str, user_id, "delete") return 1 else: return 0 except Exception as e: current_app.logger.error(e) return -1