def upload_photo(id_employee): req = Req().request req.add_argument('photo_file', type=werkzeug.datastructures.FileStorage, location='files') photo_file = req.parse_args()['photo_file'] employee = g.db_session.query(Employee).get(id_employee) resp = Resp().response if employee is None: resp['meta']['code'] = 404 resp['meta']['description'] = 'employee not found' return jsonify(resp), 404 # app.logger.debug('File is saved as %s', filename) if not employee.save_photo(photo_file): resp['meta']['code'] = 500 resp['meta']['description'] = 'save photo error' return jsonify(resp), 500 g.db_session.commit() g.db_session.close() resp['meta']['description'] = 'success' return jsonify(resp), 200
def post_season_ticket(): req = Req().request req.add_argument('season_ticket') reqargs = req.parse_args() resp = Resp().response if not reqargs['season_ticket']: resp['meta']['code'] = 403 resp['meta']['description'] = 'incorrect request' return jsonify(resp), 403 season_ticket = SeasonTicket() if not season_ticket.create( json.loads(reqargs['season_ticket'].replace("'", '"'))): resp['meta']['code'] = 404 resp['meta'][ 'description'] = 'column not found/failed/login already exists' return jsonify(resp), 404 season_ticket.id_registered_by = g.employee.id_employee try: g.db_session.add(season_ticket) g.db_session.commit() except sqlalchemy.exc.IntegrityError as ex: log.warning('resources.SeasonTickets.post_seasonTicket(): ' + str(ex)) resp['meta']['code'] = 403 resp['meta']['description'] = str(ex) return jsonify(resp), 403 resp['meta']['description'] = 'success' resp['content']['id_season_ticket'] = season_ticket.id_season_ticket return jsonify(resp), 200
def wrapper(*args, **kwargs): req = Req().request reqargs = req.parse_args() resp = Resp().response if not (reqargs['login'] and reqargs['password']): resp['meta']['code'] = 400 resp['meta']['description'] = 'both login and password required' return jsonify(resp), 400 if not g.db_session.query(Employee.login).filter(Employee.role.in_(roles)).filter( Employee.login == reqargs['login']).one_or_none(): resp['meta']['code'] = 403 resp['meta']['description'] = 'login not found' return jsonify(resp), 403 employee = g.db_session.query(Employee).filter(Employee.role.in_(roles)).filter( Employee.login == reqargs['login']).one_or_none() if not employee.verify_password(reqargs['password']): resp['meta']['code'] = 403 resp['meta']['description'] = 'incorrect password' return jsonify(resp), 403 g.employee = employee return func(*args, **kwargs)
def login(): """ main handler, returns user data""" req = Req().request reqargs = req.parse_args() employee = g.db_session.query(Employee).filter(Employee.login == reqargs['login']).first() resp = Resp().response resp['content']['employee'] = employee.read() return jsonify(resp), 200
def put_client(id_client): req = Req().request req.add_argument('client') reqargs = req.parse_args() resp = Resp().response if not reqargs['client']: resp['meta']['code'] = 403 resp['meta']['description'] = 'incorrect request' return jsonify(resp), 403 client = g.db_session.query(Client).get(id_client) if client is None: resp['meta']['code'] = 404 resp['meta']['description'] = 'client not found' return jsonify(resp), 404 if not client.update(json.loads(reqargs['client'].replace("'", '"'))): resp['meta']['code'] = 404 resp['meta']['description'] = 'column not found/failed' return jsonify(resp), 404 try: g.db_session.commit() except sqlalchemy.exc.IntegrityError as ex: log.warning('resources.Clients.put_client(): '+str(ex)) if 'UNIQUE constraint failed: clients.login' in str(ex): resp['meta']['code'] = 403 resp['meta']['description'] = 'login already exist' return jsonify(resp), 403 elif 'NOT NULL constraint failed: clients.login' in str(ex): resp['meta']['code'] = 403 resp['meta']['description'] = 'login is empty' return jsonify(resp), 403 else: resp['meta']['code'] = 403 resp['meta']['description'] = str(ex) return jsonify(resp), 403 resp['meta']['description'] = 'success' return jsonify(resp), 200
def post_client(): req = Req().request req.add_argument('client') reqargs = req.parse_args() resp = Resp().response if not reqargs['client']: resp['meta']['code'] = 403 resp['meta']['description'] = 'incorrect request' return jsonify(resp), 403 client = Client() if not client.create(json.loads(reqargs['client'].replace("'", '"'))): resp['meta']['code'] = 404 resp['meta']['description'] = 'column not found/failed/login already exists' return jsonify(resp), 404 client.id_registered_by = g.employee.id_employee try: g.db_session.add(client) g.db_session.commit() except sqlalchemy.exc.IntegrityError as ex: log.warning('resources.Clients.post_client(): '+str(ex)) if 'UNIQUE constraint failed: clients.login' in str(ex): resp['meta']['code'] = 403 resp['meta']['description'] = 'login already exist' return jsonify(resp), 403 elif 'NOT NULL constraint failed: clients.login' in str(ex): resp['meta']['code'] = 403 resp['meta']['description'] = 'login is empty' return jsonify(resp), 403 else: resp['meta']['code'] = 403 resp['meta']['description'] = str(ex) return jsonify(resp), 403 resp['meta']['description'] = 'success' resp['content']['id_client'] = client.id_client return jsonify(resp), 200
def put_employee(id_employee): req = Req().request req.add_argument('employee') reqargs = req.parse_args() resp = Resp().response if not reqargs['employee']: resp['meta']['code'] = 403 resp['meta']['description'] = 'incorrect request' return jsonify(resp), 403 employee = g.db_session.query(Employee).filter( Employee.id_employee == id_employee).first() if employee is None: resp['meta']['code'] = 404 resp['meta']['description'] = 'employee not found' return jsonify(resp), 404 if not employee.update( json.loads(reqargs['employee'].replace("'", '"'))): resp['meta']['code'] = 404 resp['meta']['description'] = 'column not found/failed' return jsonify(resp), 404 try: g.db_session.commit() except sqlalchemy.exc.IntegrityError as ex: log.warning('resources.Employees.put_employee(): ' + str(ex)) if 'UNIQUE constraint failed: employees.login' in str(ex): resp['meta']['code'] = 403 resp['meta']['description'] = 'login already exist' return jsonify(resp), 403 else: resp['meta']['code'] = 403 resp['meta']['description'] = str(ex) return jsonify(resp), 403 resp['meta']['description'] = 'success' return jsonify(resp), 200