Skip to content

Commit

Permalink
fix: admin-record many bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed May 13, 2024
1 parent 7302e47 commit c98d6a0
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 89 deletions.
46 changes: 33 additions & 13 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def check_auth():

def save_record(record, payload, collection):
#print(record, payload, flush=True)
is_debug = True
is_debug = False

uid = payload.get('uid')
is_new_record = False
Expand Down Expand Up @@ -153,9 +153,9 @@ def save_record(record, payload, collection):
new_val = selected['value']
if name in pv:
if pv[name].value != new_val:

pv[name].value = new_val
changes[name] = ['UPDATE', pv[name].value, new_val]
pv[name].value = new_val

else:
if a_type := AssertionType.query.filter(AssertionType.name==name).first():
new_ra = RecordAssertion(record_id=record.id, assertion_type_id=a_type.id, value=new_val)
Expand Down Expand Up @@ -201,7 +201,10 @@ def save_record(record, payload, collection):
iden_modify = make_editable_values(iden, i)
if len(iden_modify):
iden.update(iden_modify)
changes[iden.id] = iden_modify
iden_changes = inspect_model(iden)
if len(iden_changes):
changes[iden.id] = iden_changes

if len(changes):
relate_changes['identifications'] = changes

Expand Down Expand Up @@ -233,7 +236,9 @@ def save_record(record, payload, collection):
unit_modify = make_editable_values(unit, i)
if len(unit_modify):
unit.update(unit_modify)
changes[unit.id] = unit_modify
unit_changes = inspect_model(unit)
if len(unit_changes):
changes[unit.id] = unit_changes

if assertions := i.get('assertions'):
changes_assertions = {}
Expand All @@ -248,12 +253,12 @@ def save_record(record, payload, collection):
# update
pure_value = v
if assertion_type_map[k].input_type == 'select':
# print(v, '---',flush=True)
if isinstance(v, dict):
pure_value = v.get('value')
pv_assertions[k].value = pure_value
#print('update', k, pure_value, flush=True)
changes_assertions[k] = ['UPDATE', k, pv_assertions[k].value, pure_value]
if pv_assertions[k].value != pure_value:
pv_assertions[k].value = pure_value
#print('update', k, pure_value, flush=True)
changes_assertions[k] = ['UPDATE', k, pv_assertions[k].value, pure_value]
else:
# delete
session.delete(pv_assertions[k])
Expand All @@ -266,7 +271,10 @@ def save_record(record, payload, collection):
session.add(a)
#print('insert', k, flush=True)
changes_assertions[k] = ['CREATE', k, v]
changes[unit.id]['assertions'] = changes_assertions
if len(changes_assertions):
if unit.id not in changes:
changes[unit.id] = {}
changes[unit.id]['assertions'] = changes_assertions

if annotations := i.get('annotations'):
changes_annotations = {}
Expand Down Expand Up @@ -299,7 +307,10 @@ def save_record(record, payload, collection):
session.add(a)
#print('insert', k, flush=True)
changes_annotations[k] = ['CREATE', k, v]
changes[unit.id]['annotations'] = changes_annotations
if len(changes_annotations):
if unit.id not in changes:
changes[unit.id] = {}
changes[unit.id]['annotations'] = changes_annotations

if len(changes):
relate_changes['units'] = changes
Expand Down Expand Up @@ -627,7 +638,7 @@ def record_list():

cat_lists= UserList.query.filter(UserList.user_id==current_user.id, UserList.entity_id==entity_id).all()

print(r, flush=True)
#print(r, flush=True)
item = {
'collection_id': r[11],
'accession_number': r[1] or '',
Expand All @@ -644,11 +655,20 @@ def record_list():
}
items.append(item)

plist = Person.query.filter(Person.is_collector==True).all()
collector_list = [{
'id': x.id,
'full_name': x.full_name,
'full_name_en': x.full_name_en,
'display_name': x.display_name,
} for x in plist]
return render_template(
'admin/record-list-view.html',
items=items,
total=total,
pagination=pagination)
pagination=pagination,
collector_list=collector_list,
)


@admin.route('/<collection_name>/records/create', methods=['GET', 'POST'])
Expand Down
8 changes: 4 additions & 4 deletions app/blueprints/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
AssertionType,
Collection,
MultimediaObject,
#LogEntry,
#get_structed_list,
)
from app.models.gazetter import (
NamedArea,
Expand Down Expand Up @@ -875,12 +873,14 @@ def api_create_admin_record(collection_id):
res.headers['Access-Control-Allow-Headers'] = 'Content-Type'
return res
elif request.method == 'POST':
record, is_new = save_record(None, request.json, collection_id)
if col := session.get(Collection, collection_id):
record, is_new = save_record(None, request.json, col)

if is_new:
uid = request.json.get('uid')
return jsonify({
'message': 'ok',
'next': url_for('admin.modify_frontend_collection_record', collection_id=record.collection_id, record_id=record.id),
'next': url_for('admin.modify_frontend_collection_record', collection_id=record.collection_id, record_id=record.id)+f'?uid={uid}',
})
else:
return jsonify({'message': 'ok'})
11 changes: 9 additions & 2 deletions app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
Column,
String,
DateTime,
ForeignKey
ForeignKey,
)
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
Session,
relationship,
)
from sqlalchemy.orm import scoped_session, sessionmaker, Session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql import JSONB

Expand Down Expand Up @@ -99,3 +104,5 @@ class ModelHistory(Base):
changes = Column(JSONB)
created = Column(DateTime, default=get_time)
remarks = Column(String(500))

user = relationship('User')
21 changes: 19 additions & 2 deletions app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
)
from sqlalchemy import(
inspect,
desc,
)
from app.database import (
session,
ModelHistory,
)
from app.database import session
from app.models.site import Organization
from app.models.collection import (
Collection,
Expand Down Expand Up @@ -178,7 +182,20 @@ def get_record_values(record):
if x.named_area.area_class_id in [5, 6] or x.named_area.area_class_id >= 7:
data['named_areas'][x.named_area.area_class.name] = x.named_area.to_dict()

data['named_areas__legacy'] = [x.to_dict() for x in record.get_named_area_list('legacy')],
data['named_areas__legacy'] = [x.to_dict() for x in record.get_named_area_list('legacy')]

histories = ModelHistory.query.filter(ModelHistory.tablename=='record*', ModelHistory.item_id==str(record.id)).order_by(desc(ModelHistory.created)).all()
data['__histories__'] = [{
'id': x.id,
'changes': x.changes,
'action': x.action,
'created': x.created.strftime('%Y-%m-%d %H:%M:%S'),
'user': {
'username': x.user.username,
'uid': x.user_id,
}
} for x in histories]

return data

def make_editable_values(model, data):
Expand Down
3 changes: 1 addition & 2 deletions app/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,7 @@ def get_editable_fields(field_types=['date', 'str', 'bool']):
'kind_of_unit',
'preparation_type',
'preservation_text',
'acquisition_type'
'acquired_from',
'acquisition_type',
'acquisition_source_text',
'type_status',
'typified_name',
Expand Down
9 changes: 9 additions & 0 deletions app/templates/admin/record-list-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
<input type="search" name="q" class="search-input uk-input" autocapitalize="none" autocorrect="off" autocomplete="off" value="{%if request.args.q %}{{ request.args.q }}{% endif%}"/>
</div>
<div class="uk-text-meta">keyword: 館號、物種、採集者、採集號</div>
<hr />
<input type="search" name="q" class="search-input uk-input" autocapitalize="none" autocorrect="off" autocomplete="off" value="{%if request.args.q %}{{ request.args.q }}{% endif%}"/>
<div class="uk-inline box-search-container">
<span class="uk-form-icon" uk-icon="icon: search"></span>
<input class="uk-input uk-form-small" />
<ul class="box-items-list">
<li class="box-items">aeu</li>
</ul>
</div>
</form>
<p>Results: {{ "{:,}".format(total) }}</p>
</div>
Expand Down
Loading

0 comments on commit c98d6a0

Please sign in to comment.