def get(self): user_id = self.request.matchdict.get('user_id') with create_transaction() as transaction: state = transaction.query(UserSql).filter( UserSql.id == user_id).first() return HTTPOk(state.val()) if state is not None \ else HTTPNotFound(FailResultSimple("UserNotFound", "Пользователь не найдена"))
def session(self): with create_transaction() as transaction: auth_sid = self.request.matchdict.get('auth_sid') state = transaction.query(SessionStateSql).filter( SessionStateSql.id == auth_sid).first() return HTTPOk(state.val()) if state is not None \ else HTTPNotFound(FailResultSimple("SessionNotFound", "Сессия не найдена"))
def get_form(self): id = self.request.matchdict.get('id') with create_transaction() as transaction: form = transaction.query(FormSql) \ .filter(FormSql.id == id) \ .first() return HTTPOk(form.val()) if form \ else HTTPNotFound(FailResultSimple('FormNotFound', 'Форма не найдена'))
def delete_answer(self): id = self.request.matchdict.get('id') with create_transaction() as transaction: deleted = transaction.query(AnswerSql) \ .filter(AnswerSql.id == id) \ .delete() return HTTPOk() if deleted \ else HTTPNotFound(FailResultSimple('AnswerNotFound', 'Ответ не найдена'))
def get_answer(self): id = self.request.matchdict.get('id') with create_transaction() as transaction: answer = transaction.query(AnswerSql) \ .filter(AnswerSql.id == id) \ .first() return HTTPOk(answer.val()) if answer \ else HTTPNotFound(FailResultSimple('AnswerNotFound', 'Ответ не найдена'))
def delete_binding(self): form_id = self.request.matchdict.get('form_id') place_id = self.request.matchdict.get('place_id') with create_transaction() as transaction: deleted = transaction.query(BindingSql) \ .filter(BindingSql.form_id == form_id and BindingSql.place_id == place_id) \ .delete() return HTTPOk() if deleted \ else HTTPNotFound(FailResultSimple('BindingNotFound', 'Привязка не найдена'))
def set_answer(self): id = self.request.matchdict.get('id') answer = self.request.body.decode() with create_transaction() as transaction: patched = transaction.query(AnswerSql) \ .filter(AnswerSql.id == id) \ .update({AnswerSql.answer: answer}) return HTTPOk() if patched \ else HTTPNotFound(FailResultSimple('AnswerNotFound', 'Ответ не найдена'))
def set_form(self): id = self.request.matchdict.get('id') data = self.request.body.decode() data = json.loads(data) with create_transaction() as transaction: patched = transaction.query(FormSql) \ .filter(FormSql.id == id) \ .update(data) return HTTPOk() if patched \ else HTTPNotFound(FailResultSimple('FormNotFound', 'Форма не найдена'))
def put_form(self): id = self.request.matchdict.get('id') data = self.request.body.decode() data = json.loads(data) data["id"] = id form = FormSql(**data) try: with create_transaction() as transaction: transaction.add(form) return HTTPOk() except IntegrityError: return HTTPNotFound(FailResultSimple('FormNotCreated', 'Форма не создана'))
def put_answer(self): id = self.request.matchdict.get('id') data = self.request.body.decode() data = json.loads(data) data["id"] = id answer = AnswerSql(**data) try: with create_transaction() as transaction: transaction.add(answer) return HTTPOk() except IntegrityError: return HTTPNotFound(FailResultSimple('AnswerNotCreated', 'Ответ не создана'))
def put(self): login = self.request.params.get('login') email = self.request.params.get('email') password = self.request.body.decode() try: with create_transaction() as transaction: user = UserSql(login, password, email) transaction.add(user) return HTTPOk(user.id) except IntegrityError: return HTTPBadResponse( FailResultSimple("FailRegister", "Не удалось зарегистрировать"))
def authenticate_by_pass(self): login = self.request.params.get('login') client_addr = self.request.params.get('client_addr') password = self.request.body.decode() with create_transaction() as transaction: user = transaction.query(UserSql)\ .filter((UserSql.login == login) & (UserSql.password == password))\ .first() if user is None: return HTTPForbidden( FailResultSimple("UserNotFound", "Пользователь не найден")) state = SessionStateSql(user_id=user.id, auth_mode="ByPass", ip_address=client_addr) transaction.add(state) return HTTPOk(sid=state.id)
def put(self): latitude = self.request.params.get('lat') longitude = self.request.params.get('lon') nominatim = ConfigurationWrapper.instance('api').get('nominatim') request = Request.blank( nominatim + '/reverse?format=json&lat=%s&lon=%s&addressdetails=1' % (latitude, longitude)) request.user_agent = 'CoolMap/1.0.0' response = request.get_response() if response.status_code != HTTPStatus.OK.value: return HTTPBadResponse( FailResultSimple('FailGeodecoding', 'Fail to geodecoding')) data = response.json_body osm_type = data.get('osm_type')[0].upper() osm_id = data.get('osm_id') title = data.get('display_name') address = json.dumps(data.get('address')) place = self._add_or_update(osm_id, osm_type, address, title) return HTTPOk(place)
def forbidden_view(req: Request): return HTTPForbidden(FailResultSimple('Forbidden', 'Authorization has been denied for this request'))