Skip to content

Commit

Permalink
fixed: taibol
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed Jun 24, 2024
1 parent e3404ea commit 63dec70
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 40 deletions.
32 changes: 17 additions & 15 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand Down Expand Up @@ -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'
)

Expand Down Expand Up @@ -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', '')
Expand All @@ -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
Expand Down Expand Up @@ -684,8 +684,9 @@ def record_list():
@admin.route('/<collection_name>/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(
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions app/blueprints/admin_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand All @@ -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' },
Expand All @@ -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': '標題' },
Expand All @@ -82,7 +82,7 @@
'display': 'label',
'resource_name': 'article_categories',
'model': ArticleCategory,
'filter_by': 'organization',
'filter_by': 'site',
'fields': {
'label': { 'label': '標題' },
'name': { 'label': 'key' },
Expand Down Expand Up @@ -162,7 +162,7 @@
'display': 'label',
'resource_name': 'collections',
'model': Collection,
'filter_by': 'organization',
'filter_by': 'site',
'fields': {
'label': { 'label': '標題' },
'name': { 'label': 'key',},
Expand Down
6 changes: 3 additions & 3 deletions app/blueprints/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)

Expand Down
23 changes: 22 additions & 1 deletion app/blueprints/frontpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def specimen_detail_legacy(lang_code):
#@frontpage.route('/specimens/<record_key>')
def specimen_detail(record_key, lang_code):
entity = None

# TODO: 判斷domain
if 'ark:/' in record_key:
#ark:<naan>/<key>
naan, identifier = record_key.replace('ark:/', '').split('/')
Expand All @@ -167,6 +167,27 @@ def specimen_detail(record_key, lang_code):

return abort(404)


@frontpage.route('/records/<int:record_id>', defaults={'lang_code': DEFAULT_LANG_CODE})
@frontpage.route('/<lang_code>/records/<int:record_id>')
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/<int:taxon_id>', defaults={'lang_code': DEFAULT_LANG_CODE})
@frontpage.route('/<lang_code>/species/<int:taxon_id>')
def species_detail(taxon_id, lang_code):
Expand Down
72 changes: 69 additions & 3 deletions app/helpers_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import csv
import math
import time
import sqlite3
import re

from sqlalchemy import (
select,
Expand All @@ -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():

Expand Down Expand Up @@ -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()
9 changes: 8 additions & 1 deletion app/helpers_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 0 additions & 7 deletions app/models/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions app/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<nav class="uk-navbar uk-light" data-uk-navbar="mode:click; duration: 250">
<div class="uk-navbar-left">
<div class="uk-navbar-item uk-visible@m">
<a href="{{ url_for('admin.index' ) }}">{{ current_user.organization.name }}</a>{#<a class="uk-logo" href="#"><img class="custom-logo" src="img/dashboard-logo-white.svg" alt=""></a>#}
<a href="{{ url_for('admin.index' ) }}">{{ current_user.site.name|upper }}</a>{#<a class="uk-logo" href="#"><img class="custom-logo" src="img/dashboard-logo-white.svg" alt=""></a>#}
</div>
<ul class="uk-navbar-nav uk-visible@m">
<li>
Expand Down Expand Up @@ -49,7 +49,7 @@
</header>
<aside id="left-col" class="uk-light uk-visible@m">
<div class="left-logo uk-flex uk-flex-middle">
{#<img class="custom-logo" src="img/dashboard-logo.svg" alt="">#}{{ current_user.organization.code }}
{#<img class="custom-logo" src="img/dashboard-logo.svg" alt="">#}{{ current_user.site.title }}
</div>
{#
<div class="left-content-box content-box-dark">
Expand All @@ -60,7 +60,7 @@ <h4 class="uk-text-center uk-margin-remove-vertical text-light">John Doe</h4>
<!-- user dropdown -->
<div class="uk-dropdown user-drop" data-uk-dropdown="mode: click; pos: bottom-center; animation: uk-animation-slide-bottom-small; duration: 150">
<ul class="uk-nav uk-dropdown-nav uk-text-left">
{% for collection in current_user.organization.collections %}
{% for collection in current_user.site.collections %}
<li><a href="{{ url_for('admin.dashboard', collection=collection.name )}}">{{ collection.label }}</a></li>
{% endfor %}
<li><a href="#"><span data-uk-icon="icon: info"></span> Summary</a></li>
Expand Down
4 changes: 2 additions & 2 deletions app/templates/admin/record-list-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<div>
新增
<div class="uk-button-group">
{% for collection in current_user.organization.collections %}
{% for collection in current_user.site.collections %}
{# url_for('admin.record_create', collection_name=collection.name) #}
<a href="{{ url_for('admin.create_frontend_collection_record', collection_id=collection.id) }}?uid={{ current_user.id }}&uname={{ current_user.username }}" class="uk-button uk-button-default">{{ collection.label }}</a>
{% endfor %}
Expand Down Expand Up @@ -171,7 +171,7 @@
<div>
<p uk-margin>
新增:
{% for collection in current_user.organization.collections %}
{% for collection in current_user.site.collections %}
<a href="{{ url_for('admin.record_create', collection_name=collection.name) }}" class="uk-button uk-button-default">{{ collection.label }}</a>
{% endfor %}
</p>
Expand Down

0 comments on commit 63dec70

Please sign in to comment.