From 63dec70cb822c1fc4651f80baa4c2a51574a841f Mon Sep 17 00:00:00 2001 From: MooGoo Date: Tue, 25 Jun 2024 06:14:49 +0800 Subject: [PATCH] fixed: taibol --- app/blueprints/admin.py | 32 +++++----- app/blueprints/admin_register.py | 10 ++-- app/blueprints/base.py | 6 +- app/blueprints/frontpage.py | 23 +++++++- app/helpers_data.py | 72 ++++++++++++++++++++++- app/helpers_query.py | 9 ++- app/models/site.py | 7 --- app/templates/admin/base.html | 6 +- app/templates/admin/record-list-view.html | 4 +- 9 files changed, 129 insertions(+), 40 deletions(-) diff --git a/app/blueprints/admin.py b/app/blueprints/admin.py index 630b38a..4de14ec 100644 --- a/app/blueprints/admin.py +++ b/app/blueprints/admin.py @@ -416,8 +416,8 @@ def index(): #return current_app.login_manager.unauthorized() return redirect(url_for('base.login')) - site = current_user.organization - collection_ids = site.collection_ids + site = current_user.site + collection_ids = [x.id for x in site.collections] record_query = session.query( Collection.label, func.count(Record.collection_id) @@ -428,7 +428,7 @@ def index(): ).group_by( Collection ).filter( - Collection.organization_id==site.id + Collection.site_id==site.id ).order_by( Collection.sort, Collection.id ) @@ -442,7 +442,7 @@ def index(): ).group_by( Collection ).filter( - Collection.organization_id==site.id + Collection.site_id==site.id ).order_by( Collection.sort, Collection.id ) @@ -459,7 +459,7 @@ def index(): ).group_by( Collection ).filter( - Collection.organization_id==site.id + Collection.site_id==site.id ).order_by( Collection.sort, Collection.id ) @@ -517,7 +517,7 @@ def index(): ).group_by( text('1') ).filter( - Collection.organization_id==site.id, + Collection.site_id==site.id, Record.created >= '2023-04-01' ) @@ -558,8 +558,8 @@ def index(): @admin.route('/records/', methods=['GET']) @login_required def record_list(): - site = current_user.organization - + site = current_user.site + collection_ids = [x.id for x in site.collections] current_page = int(request.args.get('page', 1)) q = request.args.get('q', '') collectors = request.args.get('collectors', '') @@ -572,7 +572,7 @@ def record_list(): stmt = make_admin_record_query(dict(request.args)) # apply collection filter by site - stmt = stmt.filter(Record.collection_id.in_(site.collection_ids)) + stmt = stmt.filter(Record.collection_id.in_(collection_ids)) # print(stmt, flush=True) base_stmt = stmt @@ -684,8 +684,9 @@ def record_list(): @admin.route('//records/create', methods=['GET', 'POST']) @login_required def record_create(collection_name): - site = current_user.organization - if collection := Collection.query.filter(Collection.id.in_(site.collection_ids), Collection.name==collection_name).one(): + site = current_user.site + collection_ids = [x.id for x in site.collections] + if collection := Collection.query.filter(Collection.id.in_(collection_ids), Collection.name==collection_name).one(): if request.method == 'GET': x = get_record_all_options(collection.id) return render_template( @@ -964,9 +965,10 @@ def dispatch_request(self): if filter_by := self.register.get('filter_by'): if filter_by == 'organization': - query = query.filter(self.register['model'].organization_id==current_user.organization_id) + query = query.filter(self.register['model'].site_id==current_user.site_id) elif filter_by == 'collection': - query = query.filter(self.register['model'].collection_id.in_(current_user.organization.collection_ids)) + collection_ids = [x.id for x in site.collections] + query = query.filter(self.register['model'].collection_id.in_(collection_ids)) #print(query, flush=True) if list_filter := self.register.get('list_filter'): @@ -1036,8 +1038,8 @@ def dispatch_request(self, item_id): # create new instance if self.is_create is True: if 'filter_by' in self.register: - if self.register['filter_by'] == 'organization': - self.item = self.register['model'](organization_id=current_user.organization_id) + if self.register['filter_by'] == 'site': + self.item = self.register['model'](site_id=current_user.site_id) else: self.item = self.register['model']() else: diff --git a/app/blueprints/admin_register.py b/app/blueprints/admin_register.py index 3364d8c..cd4c9e9 100644 --- a/app/blueprints/admin_register.py +++ b/app/blueprints/admin_register.py @@ -37,7 +37,7 @@ 'display': 'title', 'resource_name': 'related_links', 'model': RelatedLink, - 'filter_by': 'organization', + 'filter_by': 'site', 'fields': { 'title': { 'label': '標題' }, 'category': { 'label': '類別', 'type': 'select', 'foreign': RelatedLinkCategory, 'display': 'label'}, @@ -52,7 +52,7 @@ 'display': 'label', 'resource_name': 'related_link_categories', 'model': RelatedLinkCategory, - 'filter_by': 'organization', + 'filter_by': 'site', 'fields': { 'label': { 'label': '標題' }, 'name': { 'label': 'key' }, @@ -66,7 +66,7 @@ 'display': 'subject', 'resource_name': 'articles', 'model': Article, - 'filter_by': 'organization', + 'filter_by': 'site', 'list_query': Article.query.order_by(desc(Article.publish_date)), 'fields': { 'subject': { 'label': '標題' }, @@ -82,7 +82,7 @@ 'display': 'label', 'resource_name': 'article_categories', 'model': ArticleCategory, - 'filter_by': 'organization', + 'filter_by': 'site', 'fields': { 'label': { 'label': '標題' }, 'name': { 'label': 'key' }, @@ -162,7 +162,7 @@ 'display': 'label', 'resource_name': 'collections', 'model': Collection, - 'filter_by': 'organization', + 'filter_by': 'site', 'fields': { 'label': { 'label': '標題' }, 'name': { 'label': 'key',}, diff --git a/app/blueprints/base.py b/app/blueprints/base.py index 2fe15da..4c8610c 100644 --- a/app/blueprints/base.py +++ b/app/blueprints/base.py @@ -82,8 +82,8 @@ @base.route('/portals') def portal_list(): - site_list = Organization.query.filter(Organization.is_site==True).all() - return render_template('portal-list.html', site_list=site_list) + #site_list = Organization.query.filter(Organization.is_site==True).all() + return render_template('portal-list.html', site_list=[]) @base.route('/search') def portal_search(): @@ -185,7 +185,7 @@ def login(): username = request.form.get('username', '') passwd = request.form.get('passwd', '') - if u := User.query.filter(User.username==username, User.organization_id==site.id).first(): + if u := User.query.filter(User.username==username, User.site_id==site.id).first(): if check_password_hash(u.passwd, passwd): login_user(u) diff --git a/app/blueprints/frontpage.py b/app/blueprints/frontpage.py index 5b91efa..8024918 100644 --- a/app/blueprints/frontpage.py +++ b/app/blueprints/frontpage.py @@ -143,7 +143,7 @@ def specimen_detail_legacy(lang_code): #@frontpage.route('/specimens/') def specimen_detail(record_key, lang_code): entity = None - + # TODO: 判斷domain if 'ark:/' in record_key: #ark:/ naan, identifier = record_key.replace('ark:/', '').split('/') @@ -167,6 +167,27 @@ def specimen_detail(record_key, lang_code): return abort(404) + +@frontpage.route('/records/', defaults={'lang_code': DEFAULT_LANG_CODE}) +@frontpage.route('//records/') +def record_detail(record_id, lang_code): + entity = None + # TODO: 判斷domain + try: + id_ = int(record_id) + entity = session.get(Record, id_) + except ValueError: + pass + + if entity: + try: + return render_template(f'sites/{g.site.name}/specimen-detail.html', entity=entity) + except TemplateNotFound: + return render_template('specimen-detail.html', entity=entity) + + return abort(404) + + @frontpage.route('/species/', defaults={'lang_code': DEFAULT_LANG_CODE}) @frontpage.route('//species/') def species_detail(taxon_id, lang_code): diff --git a/app/helpers_data.py b/app/helpers_data.py index 95b98d6..9a60375 100644 --- a/app/helpers_data.py +++ b/app/helpers_data.py @@ -1,6 +1,8 @@ import csv import math import time +import sqlite3 +import re from sqlalchemy import ( select, @@ -16,6 +18,70 @@ make_specimen_query, ) +class MiniMatch(object): + + cur = None + values = [] + hand_match_data = None + + def __init__(self, db_path): + self.db_path = db_path + con = sqlite3.connect("names.sqlite3") + self.cur = con.cursor() + + + def exec_query(self, sql): + rows = [] + res = self.cur.execute(sql) + for row in res.fetchall(): + data = {} + if fields := self.values: + for i,field in enumerate(fields): + data[field] = row[i] + rows.append(data) + #rows.append(row) + + return rows + + def select(self, *args): + self.values = args + + def match(self, name, limit): + ''' + sql = """SELECT bm25(taicol_fts, 0, 10), t.name_id, t.simple_name\ + FROM taicol t INNER JOIN taicol_fts s ON s.name_id = t.name_id\ + WHERE taicol_fts match '{{simple_name}}: {name}' LIMIT {limit}""".format(name=name, limit=limit); + ''' + fields = ', '.join([f'"{x}"'for x in self.values]) + sql = f'SELECT {fields} from taicol where simple_name like \'%{name}%\'' + results = self.exec_query(sql) + + if len(results) > 0: + return results[0] # first one + elif self.hand_match_data: + self.hand_match(name) + + def set_hand_match_data(self, data): + self.hand_match_data = data + + def hand_match(self, name): + # global names resolver copy and paste rank + if ranks := self.hand_match_data.get(name): + taxon = {} + for i in ranks.split('>>'): + for k in ['kingdom', 'phylum', 'class', 'order', 'family', 'genus']: + if f'({k})' in i: + taxon[f'{k}_name'] = i.replace(f'({k})', '').strip() + + taxon_zh = {} + for k, v in taxon.items(): + taxon_zh[k] = v + if res := self.match(v, 1): + if cname := res.get('common_name_c'): + taxon_zh[f'{k}_zh'] = cname + + print(name) + print(taxon_zh) def export_specimen_dwc_csv(): @@ -135,6 +201,6 @@ def import_phase0(data, collection_id): session.add(r) session.commit() - u = Unit(collection_id=collection_id, record_id=r.id) - session.add(u) - session.commit() + #u = Unit(collection_id=collection_id, record_id=r.id) + #session.add(u) + #session.commit() diff --git a/app/helpers_query.py b/app/helpers_query.py index 719d489..c2d37a4 100644 --- a/app/helpers_query.py +++ b/app/helpers_query.py @@ -71,7 +71,14 @@ def make_specimen_query(filtr): if ft := sd.get('filters'): for k, v in ft.items(): - stmt = stmt.where(Record.source_data[k].astext == v) + if v == '__NOT_NULL__': + stmt = stmt.where( + Record.source_data[k].astext != '' + ) + else: + stmt = stmt.where( + Record.source_data[k].astext == v + ) return stmt diff --git a/app/models/site.py b/app/models/site.py index 2a377b7..7e3c37b 100644 --- a/app/models/site.py +++ b/app/models/site.py @@ -121,13 +121,6 @@ def find_by_host(host='foo'): return None - @staticmethod - def find_collection_ids(host): - if site := Site.find_by_host(host): - print(site.organizations,'aaa',site.id, flush=True) - return [x.collections for x in site.organizations] - #return site.get_collection_ids() - def get_units(self, num): from app.models.collection import Unit, Collection, Record #units = Unit.query.filter(Unit.accession_number!='').order_by(func.random()).limit(4).all() diff --git a/app/templates/admin/base.html b/app/templates/admin/base.html index 9cc8c1c..e48ae98 100644 --- a/app/templates/admin/base.html +++ b/app/templates/admin/base.html @@ -15,7 +15,7 @@