diff --git a/aore/phias.py b/aore/phias.py index eda9a3a..d82b1b5 100644 --- a/aore/phias.py +++ b/aore/phias.py @@ -21,6 +21,7 @@ class App(BottleCL): def init_routes(self): self.add_route(r'/expand/', self.__expand) self.add_route(r'/normalize/', self.__normalize) + self.add_route(r'/gettext/', self.__gettext) self.add_route(r'/find/', self.__find) self.add_route(r'/find//', self.__find) self.add_error(404, self.basic_error_handler) @@ -45,6 +46,12 @@ class App(BottleCL): 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 def basic_error_handler(error): response.content_type = 'application/json' diff --git a/aore/search/fiasfactory.py b/aore/search/fiasfactory.py index edee085..04a2553 100644 --- a/aore/search/fiasfactory.py +++ b/aore/search/fiasfactory.py @@ -19,6 +19,7 @@ class FiasFactory: self.searcher = SphinxSearch(self.db) 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.gettext_templ = template('aore/templates/postgre/gettext_query.sql', aoid="//aoid") # Проверка, что строка является действительым UUID v4 @staticmethod @@ -97,3 +98,20 @@ class FiasFactory: return dict(error=err.args[0]) 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 diff --git a/aore/templates/postgre/gettext_query.sql b/aore/templates/postgre/gettext_query.sql new file mode 100644 index 0000000..4283050 --- /dev/null +++ b/aore/templates/postgre/gettext_query.sql @@ -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 \ No newline at end of file