예제 #1
0
파일: rest.py 프로젝트: MarsStirner/vesta
def get_data_hs(code, field, field_value):
    # TODO: try-except
    obj = Dictionary(code)
    obj_names = DictionaryNames()
    document = obj.get_document({str(field): field_value})
    origin_dict = obj_names.get_by_code(code)
    if 'oid' in origin_dict:
        # Работаем с НСИ справочником
        data = document
        oid = origin_dict['oid']
    else:
        try:
            try:
                linked_dict = _get_linked_dict(document, origin_dict)
            except AttributeError:
                raise InvalidAPIUsage(u'Not found', status_code=404)
            except KeyError:
                raise InvalidAPIUsage(u'Not found', status_code=404)
            if not document:
                return make_response(
                    vesta_jsonify(
                        dict(oid=linked_dict['oid'], message="not found")),
                    404)
        except TypeError, e:
            raise InvalidAPIUsage(e.message, status_code=400)
        except InvalidId, e:
            raise InvalidAPIUsage(e.message, status_code=404)
예제 #2
0
def get_street(code):
    obj = Dictionary(STREET_CODE)
    find = {'identcode': code}
    try:
        result = obj.get_list(find)
    except ValueError, e:
        raise InvalidAPIUsage(e.message, status_code=404)
예제 #3
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def delete(self, code):
     """Удаление справочника"""
     obj = DictionaryNames()
     try:
         obj.delete(code=code)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #4
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def post(self, code):
     data = self.parse_request(request)
     obj = Dictionary(code)
     try:
         _id = obj.add_documents(data)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #5
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def document_details(self, code, document_id):
     obj = Dictionary(code)
     #TODO: учесть auth_token
     try:
         result = obj.get_document(dict(_id=document_id))
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #6
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def put(self, code, document_id):
     data = self.parse_request(request)
     obj = Dictionary(code)
     try:
         exists = obj.exists(dict(_id=document_id))
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #7
0
def get_city(code):
    obj = Dictionary(CITY_CODE)
    find = {'identcode': code}
    try:
        cities = obj.get_list(find)
    except ValueError, e:
        raise InvalidAPIUsage(e.message, status_code=404)
예제 #8
0
파일: rest.py 프로젝트: MarsStirner/vesta
def _get_linked_dict(document, collection):
    # Если в документе задана привязка к справочнику - используем её
    if document and 'linked_collection' in document:
        obj_names = DictionaryNames()
        linked_dict = obj_names.get_by_code(document['linked_collection'])
        return linked_dict

    # Иначе смотрим на привязку самого справочника
    try:
        linked_dict = collection['linked']['collection']
    except AttributeError:
        raise InvalidAPIUsage(u'Not found', status_code=404)
    except KeyError:
        raise InvalidAPIUsage(u'Not found', status_code=404)
    else:
        return linked_dict
예제 #9
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def delete(self, code):
     """Удаление клиента"""
     obj = Clients()
     try:
         document = obj.get_by_code(code)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #10
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def put(self, code):
     """Обновление информации о справочнике"""
     data = self.parse_request(request)
     obj = DictionaryNames()
     try:
         document = obj.get_by_code(code)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #11
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def post(self):
     """Заведение информации о справочнике"""
     data = self.parse_request(request)
     obj = DictionaryNames()
     try:
         _id = obj.add(data)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #12
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def put(self, code):
     """Обновление информации о клиенте"""
     data = self.parse_request(request)
     obj = Clients()
     try:
         document = obj.get_by_code(code)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #13
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def post(self):
     """Заведение информации о клиенте"""
     data = self.parse_request(request)
     obj = Clients()
     try:
         _id = obj.add(data)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #14
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def document_by_field(self, code, field, field_value):
     obj = Dictionary(code)
     if field == 'id':
         field_value = int(field_value)
     try:
         result = obj.get_document({str(field): field_value})
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #15
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def parse_request(self, _request):
     data = _request.json
     if not data:
         data = json.loads(_request.data)
     if not data:
         raise InvalidAPIUsage(
             u'Не переданы данные, или переданы неверным методом', 400)
     return data
예제 #16
0
파일: rest.py 프로젝트: MarsStirner/vesta
def find_data_hs(code):
    data = APIMixin().parse_request(request)
    obj = Dictionary(code)
    obj_names = DictionaryNames()
    try:
        _dict = obj_names.get_by_code(code)
        result = obj.get_document(prepare_find_params(data))
    except TypeError, e:
        raise InvalidAPIUsage(e.message, status_code=400)
예제 #17
0
파일: rest.py 프로젝트: MarsStirner/vesta
def get_linked_data(code, field, field_value):
    # TODO: try-except
    obj = Dictionary(code)
    obj_names = DictionaryNames()
    document = obj.get_document({str(field): field_value})
    try:
        origin_dict = obj_names.get_by_code(code)
        try:
            linked_dict = _get_linked_dict(document, origin_dict)
        except AttributeError:
            raise InvalidAPIUsage(u'Not found', status_code=404)
        except KeyError:
            raise InvalidAPIUsage(u'Not found', status_code=404)
        if not document:
            return make_response(
                vesta_jsonify(dict(oid=linked_dict['oid'], data={})), 200)
    except TypeError, e:
        raise InvalidAPIUsage(e.message, status_code=400)
예제 #18
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def get(self, code):
     """Получение списка клиентов или информации по конкретному клиенту"""
     collection = Clients()
     if code is None:
         # return list of Clients
         try:
             result = collection.get_list()
         except TypeError, e:
             raise InvalidAPIUsage(e.message, status_code=400)
         except ValueError, e:
             return vesta_jsonify(dict())
예제 #19
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def get(self, code):
     """Получение списка справочников или информации по конкретному справочнику"""
     collection = DictionaryNames()
     if code is None:
         # return list of Dictionaries
         try:
             result = collection.get_list()
         except TypeError, e:
             raise InvalidAPIUsage(e.message, status_code=400)
         except ValueError, e:
             return vesta_jsonify(dict())
예제 #20
0
def search_city(value, limit=None):
    obj = Dictionary(CITY_CODE)
    find = {
        'is_actual': '1',
        '$or': [{
            'name': prepare_find_params(value)
        }, {
            'identcode': value
        }]
    }
    try:
        cities = obj.get_list(find, 'level', limit)
    except ValueError, e:
        raise InvalidAPIUsage(e.message, status_code=404)
예제 #21
0
def search_street(city_code, value, limit=None):
    obj = Dictionary(STREET_CODE)
    prepared = prepare_find_params(value)
    find = {
        'identparent':
        city_code,
        'is_actual':
        '1',
        '$or': [{
            'name': prepared
        }, {
            'fulltype': prepared
        }, {
            'shorttype': prepared
        }, {
            'identcode': value
        }]
    }
    try:
        result = obj.get_list(find, limit=limit)
    except ValueError, e:
        raise InvalidAPIUsage(e.message, status_code=404)
예제 #22
0
파일: rest.py 프로젝트: MarsStirner/vesta
class DictionaryAPI(MethodView, APIMixin):
    """API для работы с конкретным справочником"""

    #decorators = [user_required]

    def get(self, code, document_id):
        if document_id is None:
            return self.list_documents(code)
        else:
            result = self.document_details(code, document_id)
            if not result:
                abort(404)
            return vesta_jsonify(result)

    def document_by_field(self, code, field, field_value):
        obj = Dictionary(code)
        if field == 'id':
            field_value = int(field_value)
        try:
            result = obj.get_document({str(field): field_value})
        except TypeError, e:
            raise InvalidAPIUsage(e.message, status_code=400)
        except InvalidId, e:
            raise InvalidAPIUsage(e.message, status_code=404)
예제 #23
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def delete(self, code, document_id):
     obj = Dictionary(code)
     try:
         obj.delete(_id=document_id)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)
예제 #24
0
파일: rest.py 프로젝트: MarsStirner/vesta
            # return Dictionary info by code
            result = collection.get_by_code(code)
            if result is None:
                result = dict()
            return vesta_jsonify(result)

    def post(self):
        """Заведение информации о клиенте"""
        data = self.parse_request(request)
        obj = Clients()
        try:
            _id = obj.add(data)
        except TypeError, e:
            raise InvalidAPIUsage(e.message, status_code=400)
        except AttributeError, e:
            raise InvalidAPIUsage(e.message, status_code=400)
        else:
            if _id:
                return make_response(vesta_jsonify(dict(_id=str(_id))), 201)
            raise InvalidAPIUsage(u'Ошибка добавления данных', 500)

    def put(self, code):
        """Обновление информации о клиенте"""
        data = self.parse_request(request)
        obj = Clients()
        try:
            document = obj.get_by_code(code)
        except TypeError, e:
            raise InvalidAPIUsage(e.message, status_code=400)
        else:
            try:
예제 #25
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def decorator(*args, **kwargs):
     if not g.user:
         raise InvalidAPIUsage(u'Необходимо авторизовать клиента',
                               status_code=401)
     return f(*args, **kwargs)
예제 #26
0
파일: rest.py 프로젝트: MarsStirner/vesta
 def list_documents(self, code, find=None):
     obj = Dictionary(code)
     try:
         result = obj.get_list(find)
     except ValueError, e:
         raise InvalidAPIUsage(e.message, status_code=404)
예제 #27
0
def search_city(value, limit=None):
    obj = Dictionary(CITY_CODE)
    find = {
        'is_actual': '1',
        '$or': [{
            'name': prepare_find_params(value)
        }, {
            'identcode': value
        }]
    }
    try:
        cities = obj.get_list(find, 'level', limit)
    except ValueError, e:
        raise InvalidAPIUsage(e.message, status_code=404)
    except AttributeError, e:
        raise InvalidAPIUsage(e.message, status_code=400)
    else:
        result = _set_cities_parents(cities)
    return jsonify(data=list(result))


@module.route('/kladr/psg/search/<value>/', methods=['GET'])
@module.route('/kladr/psg/search/<value>/<int:limit>/', methods=['GET'])
@crossdomain('*', methods=['GET'])
@cache.memoize(86400)
def search_city_country(value, limit=None):
    obj = Dictionary(CITY_CODE)
    find = {
        'is_actual': '1',
        # 'shorttype': {'$in': [u'г', u'с', u'п', u'рп', u'нп']},
        '$or': [{