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 _get_permission(sub: Urn, obj: Urn): with create_transaction() as transaction: permission = transaction.query(PermissionSql) \ .filter((PermissionSql.subject == str(sub)) & (PermissionSql.object == str(obj)))\ .first() return HTTPOk(permission.val()) if permission is not None \ else HTTPNotFound(Permission(sub, obj, Permissions.Null))
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 add_binding(self): form_id = self.request.matchdict.get('form_id') place_id = self.request.matchdict.get('place_id') with create_transaction() as transaction: binding = BindingSql(form_id, place_id) transaction.add(binding) return HTTPOk()
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 _get_permissions(sub: Urn, skip: int, take: int): with create_transaction() as transaction: count = transaction.query(PermissionSql) \ .filter(PermissionSql.subject == str(sub)) \ .count() if count == 0 or skip >= count: return HTTPNotFound(ItemsResult([], skip, take, count)) permissions = transaction.query(PermissionSql)\ .filter(PermissionSql.subject == sub)\ [skip:take] items = list(map(lambda p: p.val().__dict__, permissions)) return HTTPOk(ItemsResult(items, skip, take, count))
def get_forms_by_place(self): place_id = self.request.matchdict.get('place_id') skip = int(self.request.matchdict.get('skip', 0)) take = int(self.request.matchdict.get('take', 50000)) with create_transaction() as transaction: count = transaction.query(FormSql) \ .join(BindingSql, BindingSql.form_id == FormSql.id) \ .filter(BindingSql.place_id == place_id) \ .count() if count == 0 or skip >= count: return HTTPNotFound(ItemsResult([], skip, take, count)) answers = transaction.query(FormSql) \ .join(BindingSql, BindingSql.form_id == FormSql.id) \ .filter(BindingSql.place_id == place_id) \ [skip:take] items = list(map(lambda p: p.val().__dict__, answers)) return HTTPOk(ItemsResult(items, skip, take, count))
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 get_forms(self): user_id = self.request.matchdict.get('user_id') skip = int(self.request.matchdict.get('skip', 0)) take = int(self.request.matchdict.get('take', 50000)) with create_transaction() as transaction: count = transaction.query(FormSql)\ .filter(FormSql.creator == user_id)\ .count() if count == 0 or skip >= count: return HTTPNotFound(ItemsResult([], skip, take, count)) answers = transaction.query(FormSql)\ .filter(FormSql.creator == user_id)\ [skip:take] items = list(map(lambda p: p.val().__dict__, answers)) return HTTPOk(ItemsResult(items, skip, take, count))
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)