def login(username, password): try: user = User.query.filter( func.lower(User.username) == func.lower(username)).first() if user and user.verify_password(password): token = encode_auth_token(user) return web_response.login_success( ['Authorization', 'Bearer ' + token.decode('utf-8')]) else: return web_response.login_failed() except Exception: return web_response.database_error()
def delete_blinds_task(context): json = request.get_json() if not validate_json.validate(validate_json.delete_task_schema, json): return web_response.bad_request() try: BlindsTask.query.filter_by(id=json['task_id']).delete() db.session.commit() except: return web_response.database_error() return web_response.blinds_task_deleted()
def delete_mac_address(context, user_id): if user_id is None: user_id = context['user']['id'] json = request.get_json() if not validate_json.validate(validate_json.delete_mac_address_schema, json): return web_response.bad_request() try: MacAddress.query.filter_by(id=json['mac_address_id']).delete() db.session.commit() except: return web_response.database_error() return web_response.ok_request()
def register(username, name, password, role): try: if not User.query.filter_by(username=username).first(): api_key = gen_api_key() db.session.add( User(username=username, name=name, role=role, api_key=api_key).hash_password(password)) db.session.commit() return web_response.user_added() else: return web_response.user_already_exist() except Exception: return web_response.database_error()
def add_mac_address(context, user_id): if user_id is None: user_id = context['user']['id'] json = request.get_json() if not validate_json.validate(validate_json.add_mac_address_schema, json): return web_response.bad_request() try: db.session.add(MacAddress( user_id=user_id, mac_address=json['mac_address'].lower() )) db.session.commit() except: return web_response.database_error() return web_response.ok_request()
def add_blinds_task(context): json = request.get_json() if not validate_json.validate(validate_json.add_blinds_tasks_schema, json): return web_response.bad_request() try: db.session.bulk_save_objects( list( map( lambda x: BlindsTask(time=json['unix_time'], device=x, action=json['action_id'], user_id=context['user']['id'], timeout=5 * 60, active=True), json['devices_ids']))) db.session.commit() except: return web_response.database_error() return web_response.blind_task_added()
def add_blinds_schedule(context): json = request.get_json() if not validate_json.validate(validate_json.add_blinds_schedule_schema, json): return web_response.bad_request() try: db.session.bulk_save_objects( list( map( lambda x: BlindsSchedule( device=x, action=json['action_id'], hour_type=json['hour_type'], time_offset=json['time_offset'], user_id=context['user']['id']), json['devices_ids']))) db.session.commit() except: return web_response.database_error() return web_response.blinds_schedule_added()