def post(self): data = CompanyController.parser.parse_args() name = data['name'] old_company = CompanyModel.find_by_name(name) if old_company: abort(409, message="Company already exists.") admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") company = CompanyModel(name=data['name'], description=data['description']) admin_user.companies.append(company) company.admin_id = admin_user # save the database try: db.session.add(admin_user) db.session.add(company) db.session.commit() except (ArgumentError, DataError): abort(500, message="Server internal error due to invalid argument.") return company.json(), 201
def post(self): data = EmployeeController.parser.parse_args() username = data['username'] email = data['email'] if (UserModel.find_by_username(username) or UserModel.find_by_email(email)): return {'message': "User already exists."}, 400 # Because only admin can create employee admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") # Create a new Employee user employee = EmployeeModel(isAdmin=data['isAdmin']) user_data = data del user_data['isAdmin'] user = UserModel(**user_data) user = user.hash_password() user.employee_id = employee employee.user_id = user # save the database try: db.session.add(user) db.session.add(employee) db.session.commit() except (ArgumentError, DataError): abort(500, message="Server internal error due to invalid argument.") return employee.get_employee(user), 201
def get(self, admin_id): admin = AdminModel.find_by_id(admin_id) if not admin: abort(404, message="no admin data exists.") user_id = current_identity.id user = UserModel.find_by_id(user_id) return admin.get_admin(user), 200
def admin_required(cls): admin_id = get_jwt_identity() if AdminModel.objects(id=admin_id).first() is None: abort(403) return admin_id
def post(self): """ 관리자 양식장 정보 작성 """ payload = request.json farm_name = payload['farm_name'] farm_phone_num = payload['farm_phone_num'] farm_address = payload['farm_address'] details = payload['details'] rooms = payload['rooms'] admin = AdminModel.objects(id=get_jwt_identity()).first() minifarms = [MiniFarmModel(**data, admin=admin) for data in rooms] try: FarmModel(farm_name=farm_name, admin=admin, farm_phone_number=farm_phone_num, farm_address=farm_address, farm_details=details, mini_farms=minifarms).save() except TypeError: abort(406) return '', 201
def get(self): admins = AdminModel.query_for_admin() if admins: admin = admins[0] user_id = admin.id user = UserModel.find_by_id(user_id) if user: return admin.get_admin(user), 200 abort(404, message="no super admin exist.")
def validate_adminId(self, value): try: adminId = ObjectId(value.data) except: raise ValueError('检验管理员信息失败') admin = AdminModel.objects(id=adminId).first() if not admin: raise ValueError('检验管理员信息失败')
def post(self): existing_admin = AdminModel.query_for_admin() if existing_admin: abort(404, message="Super Admin already exist.") data = AdminController.parser.parse_args() user = UserModel(**data) user = user.hash_password() admin = AdminModel() user.admin_id = admin # Save to database try: db.session.add(user) db.session.add(admin) db.session.commit() except (ArgumentError, DataError): abort(500, message="Server internal error due to invalid argument.") return {"message": "Successfully created super admin."}, 201
def delete(self, company_id): user = UserModel.find_by_id(current_identity.id) if not user: abort(404, message="no user. Please use valid account") admin = AdminModel.find_by_id(user.admin_id.id) if not admin: abort(404, message="no admin data exists.") company = CompanyModel.find_by_id(company_id) if not company: abort(404, message="no company exist with the provided company_id.") company.delete_from_db() return company.json(), 200
def validate_password(self, value): account = self.account.data password = value.data admin = AdminModel.objects(account=account).first() if not admin: raise FormValidateError(msg='用户名不存在') if check_password_hash(admin['password'], password) == False: raise FormValidateError(msg='密码不正确') self.userInfo = { "id": str(admin['id']), 'name': admin['name'], "is_admin": True }
def post(self): """ 관리자 회원가입 """ admin_id = request.json['id'] admin_pw = request.json['pw'] admin_name = request.json['name'] admin_phone_number = request.json['phone_number'] if AdminModel.objects(id=admin_id).first(): abort(409) admin_hashed_pw = generate_password_hash(admin_pw) AdminModel( id=admin_id, pw=admin_hashed_pw, name=admin_name, phone_number=admin_phone_number ).save() return '', 201
def put(self, admin_id): data = SingleAdminController.parser.parse_args() admin = AdminModel.find_by_id(admin_id) user_id = current_identity.id if not admin: abort(404, message="no admin data exists.") if not admin.user_id == user_id: abort(402, message="Only admin can updtae itself") user = UserModel.find_by_id(user_id) user.firstname = data.firstname if data.firstname else user.firstname user.lastname = data.lastname if data.lastname else user.lastname user.save_to_db() return admin.get_admin(user), 200
def put(self, company_id): data = SingleCompanyController.parser.parse_args() user = UserModel.find_by_id(current_identity.id) if not user: abort(404, message="no user. Please use valid account") admin = AdminModel.find_by_id(user.admin_id.id) if not admin: abort(404, message="no admin data exists.") company = CompanyModel.find_by_id(company_id) if not company: abort(404, message="no company exist with the provided company_id.") company.description = data.description if data.description else company.description company.save_to_db() return company.json()
def get(self): """ 관리자 양식장 정보 조회 """ admin = AdminModel.objects(id=get_jwt_identity()).first() farm = FarmModel.objects(farm_hostname=admin.name).first() return self.unicode_safe_json_dumps( [{ 'room_cost': data.farm_cost, 'room_fish_max': data.farm_fish_max, 'room_number': data.farm_number, 'room_temperature': data.temperature } for data in farm.mini_farms], 200) if admin or farm else abort(406)
def get(self): user = UserModel.find_by_id(current_identity.id) if not user: abort(500, message="Some internal fault on user.") admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") companies = CompanyModel.query.all() if not len(companies): return {"companies": companies} res = {"companies": []} for company in companies: res["companies"].append(company.json()) return res, 200
def customized_jwt_response(access_token, identity): admins = AdminModel.query_for_admin() if (len(admins) > 0): if admins[0].user_id == identity.id: return jsonify({ 'access_token': access_token.decode('utf-8'), 'user_id': identity.id, 'isAdmin': True, 'admin_id': admins[0].id }) else: return jsonify({ 'access_token': access_token.decode('utf-8'), 'user_id': identity.id, 'isAdmin': False, 'admin_id': None })
def post(self): """ 관리자 로그인 """ payload = request.json admin_id = payload['id'] admin_pw = payload['pw'] admin = AdminModel.objects(id=admin_id).first() if admin is None: abort(406) return { 'access_token': create_access_token(identity=admin_id), 'refresh_token': create_refresh_token(identity=admin_id) }, 200 if check_password_hash(admin.pw, admin_pw) else abort(406)
def get(self): """ 유저가 보낸 신청을 불러오는 API """ farm = FarmModel.objects(admin=AdminModel.objects(id=get_jwt_identity()).first()).first() apply = ApplyModel.objects(farm=farm).all() return self.unicode_safe_json_dumps([{ 'farm_name': apply.farm.farm_name, 'user_phone_number': apply.user.phone_number, 'applyDate': apply.applyDate, 'message': apply.message, 'roominfo': [{ 'itemNum': apply.roominfo.itemNum, 'itemName': apply.roominfo.itemName, 'money': apply.money }] } for apply in apply], 200)
def post(self, num): """ 관리자 내 양식장 상황 작성 """ payload = request.json admin = AdminModel.objects(id=get_jwt_identity()).first() farm = FarmModel.objects(farm_hostname=admin.name).first() mini_farm = farm.mini_farms[num - 1] mini_farm.update( temperature=payload['temperature'], farm_cost=payload['cost'], fish_kind=payload['fish_kind'], # payload['fish_kind'] = [{ # 'kind': 'String' # 'amount': 'Int' # } details=payload['details']).save() return '', 201
def get(self, num): admin = AdminModel.objects(id=get_jwt_identity()).first() apply = ApplyModel.objects(farm=FarmModel.objects( admin=admin).first()).first() minifarm = apply.farm.mini_farms[num - 1] return self.unicode_safe_json_dumps({ 'name': apply.user.name, 'phone_number': apply.user.phone_number, 'period': apply.period, 'temperature': minifarm.temperature, 'farm_cost': minifarm.farm_cost, 'fish_kind': [{ 'kind': data.kind, 'amount': data.amount } for data in minifarm.fish_kind], 'details': minifarm.details })
#!/usr/bin/env python # encoding: utf-8 """ @author: Pawn @blog: https:blog.lcylove.cn @time: 18-12-5 下午2:06 """ from app.models.admin import AdminModel from mongoengine import connect connect('python_class_stu') # 脚本 => 生成 admin 管理员账号 if __name__ == '__main__': data = { 'account': 'pawn', 'name': 'Pawn', 'password': '******', 'avatar': '1.jpg' } admin = AdminModel.add_admin(data= data) print(admin)