예제 #1
0
 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", "Пользователь не найдена"))
예제 #2
0
 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))
예제 #3
0
 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", "Сессия не найдена"))
예제 #4
0
 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()
예제 #5
0
 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', 'Форма не найдена'))
예제 #6
0
 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', 'Ответ не найдена'))
예제 #7
0
 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', 'Ответ не найдена'))
예제 #8
0
 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', 'Привязка не найдена'))
예제 #9
0
 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', 'Ответ не найдена'))
예제 #10
0
 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', 'Форма не найдена'))
예제 #11
0
 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', 'Форма не создана'))
예제 #12
0
 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', 'Ответ не создана'))
예제 #13
0
 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",
                              "Не удалось зарегистрировать"))
예제 #14
0
    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))
예제 #15
0
 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))
예제 #16
0
    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)
예제 #17
0
    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))
예제 #18
0
 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)