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)
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)
def delete(self, code): """Удаление справочника""" obj = DictionaryNames() try: obj.delete(code=code) except TypeError, e: raise InvalidAPIUsage(e.message, status_code=400)
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)
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)
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)
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)
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
def delete(self, code): """Удаление клиента""" obj = Clients() try: document = obj.get_by_code(code) except TypeError, e: raise InvalidAPIUsage(e.message, status_code=400)
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)
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)
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)
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)
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)
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
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)
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)
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())
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())
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)
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)
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)
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)
# 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:
def decorator(*args, **kwargs): if not g.user: raise InvalidAPIUsage(u'Необходимо авторизовать клиента', status_code=401) return f(*args, **kwargs)
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)
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': [{