Add gettext feature (/gettext/<aoid>)

This commit is contained in:
jar3b 2016-03-21 13:08:49 +03:00
parent aa604cc7b7
commit 2b71f7680e
3 changed files with 35 additions and 0 deletions

View File

@ -21,6 +21,7 @@ class App(BottleCL):
def init_routes(self): def init_routes(self):
self.add_route(r'/expand/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__expand) self.add_route(r'/expand/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__expand)
self.add_route(r'/normalize/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__normalize) self.add_route(r'/normalize/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__normalize)
self.add_route(r'/gettext/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__gettext)
self.add_route(r'/find/<text>', self.__find) self.add_route(r'/find/<text>', self.__find)
self.add_route(r'/find/<text>/<strong>', self.__find) self.add_route(r'/find/<text>/<strong>', self.__find)
self.add_error(404, self.basic_error_handler) self.add_error(404, self.basic_error_handler)
@ -45,6 +46,12 @@ class App(BottleCL):
return json.dumps(self._factory.find(text, strong)) return json.dumps(self._factory.find(text, strong))
def __gettext(self, aoid):
response.content_type = 'application/json'
response.set_header('Access-Control-Allow-Origin', '*')
return json.dumps(self._factory.gettext(aoid))
@staticmethod @staticmethod
def basic_error_handler(error): def basic_error_handler(error):
response.content_type = 'application/json' response.content_type = 'application/json'

View File

@ -19,6 +19,7 @@ class FiasFactory:
self.searcher = SphinxSearch(self.db) self.searcher = SphinxSearch(self.db)
self.expand_templ = template('aore/templates/postgre/expand_query.sql', aoid="//aoid") self.expand_templ = template('aore/templates/postgre/expand_query.sql', aoid="//aoid")
self.normalize_templ = template('aore/templates/postgre/normalize_query.sql', aoid="//aoid") self.normalize_templ = template('aore/templates/postgre/normalize_query.sql', aoid="//aoid")
self.gettext_templ = template('aore/templates/postgre/gettext_query.sql', aoid="//aoid")
# Проверка, что строка является действительым UUID v4 # Проверка, что строка является действительым UUID v4
@staticmethod @staticmethod
@ -97,3 +98,20 @@ class FiasFactory:
return dict(error=err.args[0]) return dict(error=err.args[0])
return rows return rows
# Возвращает простую текстовую строку по указанному AOID (при AOGUID будет
# ошибка, так что нужно предварительно нормализовать), ищет и в
def gettext(self, aoid):
try:
self.__check_param(aoid, "uuid")
sql_query = self.gettext_templ.replace("//aoid", aoid)
rows = self.db.get_rows(sql_query, True)
assert len(rows), "Record with this AOID not found in DB"
except Exception, err:
if BasicConfig.logging:
logging.error(traceback.format_exc(err))
return dict(error=err.args[0])
return rows

View File

@ -0,0 +1,10 @@
WITH RECURSIVE r AS (
SELECT ao.parentguid, ao.shortname || ' ' || ao.formalname AS fullname
FROM "ADDROBJ" AS ao
WHERE aoid='{{ aoid }}'
UNION
SELECT parent.parentguid, parent.shortname || ' ' || parent.formalname || ', ' || r.fullname AS fullname
FROM "ADDROBJ" AS parent, r
WHERE parent.aoguid = r.parentguid AND actstatus = TRUE AND livestatus = TRUE AND nextid IS NULL
)
SELECT fullname FROM r WHERE parentguid IS NULL LIMIT 1