Skip to content

Commit

Permalink
fix about media
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed Nov 7, 2024
1 parent 381bb9f commit 4445bc8
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 46 deletions.
29 changes: 29 additions & 0 deletions alembic/versions/50c149e49827_add_media_guid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""add-media-guid
Revision ID: 50c149e49827
Revises: bd8fe670a01d
Create Date: 2024-11-06 03:28:22.861377
"""
from alembic import op
import sqlalchemy as sa

import geoalchemy2

# revision identifiers, used by Alembic.
revision = '50c149e49827'
down_revision = 'bd8fe670a01d'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('multimedia_object', sa.Column('guid', sa.String(length=500), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('multimedia_object', 'guid')
# ### end Alembic commands ###
6 changes: 3 additions & 3 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def record_list():
image_url = ''
if r[0]:
if unit := session.get(Unit, r[0]):
image_url = unit.get_image()
image_url = unit.get_cover_image('s')

loc_list = [x.named_area.display_name for x in record.named_area_maps]
if loc_text := record.locality_text:
Expand Down Expand Up @@ -722,8 +722,8 @@ def api_delete_unit_media(unit_id, media_id):
site = get_current_site(request)
res = delete_image(site, serv_key, mo.file_url)
mo.unit.cover_image_id = None
#session.delete(mo)
#session.commit()
session.delete(mo)
session.commit()
return jsonify({'message': 'ok'})

return jsonify({'error': 'media_id not found'})
Expand Down
2 changes: 1 addition & 1 deletion app/blueprints/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def get_search():
#instance_id = f'{accession_number_int:06}'
#first_3 = instance_id[0:3]
#image_url = f'https://brmas-pub.s3-ap-northeast-1.amazonaws.com/hast/{first_3}/S_{instance_id}_s.jpg'
image_url = unit.get_image()
image_url = unit.get_cover_image()
except:
pass

Expand Down
22 changes: 0 additions & 22 deletions app/blueprints/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,6 @@ def assets(filename):
#return send_from_directory('/build/data-search', filename)
return send_from_directory('/app/static/assets/data-search', filename)

def get_image(hast_id, short_name):
import urllib.request
from pathlib import Path

hast_id = int(hast_id)
hast_id = f'{hast_id:06}'

short_name = short_name.replace(' ', '_')
p = Path(f'dist/{short_name}')
if not p.exists():
p.mkdir()

first_3 = hast_id[0:3]
fname = f'S_{hast_id}_l.jpg'
imgURL = f'http://brmas-pub.s3-ap-northeast-1.amazonaws.com/hast/{first_3}/{fname}'
try:
print('downloading...', imgURL, flush=True)
urllib.request.urlretrieve(imgURL, f'dist/{short_name}/{fname}')
return True
except:
return False


'''
@main.route('/zh')
Expand Down
14 changes: 11 additions & 3 deletions app/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ def to_dict(self, mode='with-collection'):
'type_note': self.type_note,
'assertions': {}, #self.get_assertions(),
'annotations': {}, #self.get_annotations(),
'image_url': self.get_image(),
'image_url': self.get_cover_image(),
'transactions': [x.transaction.to_dict() for x in self.transactions],
'guid': self.guid,
'pub_status': self.pub_status,
Expand Down Expand Up @@ -992,8 +992,15 @@ def get_annotation(self, type_name='', part=''):
if x.annotation_type.name == type_name:
return getattr(x, part) if part else x

#def get_cover(self, thumbnail):
def get_image(self, thumbnail='s'):
def get_cover_image(self, size=''):
if self.cover_image_id:
if size:
# TODO, custom suffix pattern
return self.cover_image.file_url.replace('-m.jpg', f'-{size}.jpg')
return self.cover_image.file_url
return ''

def get_image__deprecated(self, thumbnail='s'):
if self.collection_id == 1:
#if self.multimedia_objects:
# accession_number_int = int(self.accession_number)
Expand Down Expand Up @@ -1339,6 +1346,7 @@ class MultimediaObject(Base, TimestampMixin):

id = Column(Integer, primary_key=True)
#collection_id = Column(ForeignKey('collection.id', ondelete='SET NULL'))
guid = Column(String(500))
unit_id = Column(ForeignKey('unit.id', ondelete='SET NULL'))
unit = relationship('Unit', back_populates='multimedia_objects', foreign_keys=[unit_id])
record_id = Column(ForeignKey('record.id', ondelete='SET NULL'))
Expand Down
34 changes: 21 additions & 13 deletions app/templates/admin/inc_record-form2.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ $( document ).ready(function() {
printBtn.dataset.index = index;
printBtn.setAttribute('href', `/admin/print-label?entities=u${unit.id}`);

let frontendLink = unitCard.querySelector('#card-frontend-link');
frontendLink.id = `unit-${index}-card-frontend-link`;
frontendLink.dataset.index = index;
frontendLink.setAttribute('href', `/collections/${unit.id}`);

let detailToggle = unitCard.querySelector('#card-detail-toggle');
detailToggle.id = `unit-${index}-card-detail-toggle`;
detailToggle.setAttribute('href', `#unit-${index}-modal`);
Expand Down Expand Up @@ -640,22 +645,25 @@ $( document ).ready(function() {
coverImageDelete.textContent = "{{ _('刪除') }}";
coverImageDelete.onclick = (e) => {
e.preventDefault();
fetch(`/admin/api/units/${unit.id}/media/${unit.cover_image.id}`, {
method: 'DELETE',
})
.then(resp => resp.json())
.then(json => {
if (json.message === 'ok') {
UIkit.notification('已刪除', {timeout: 500});
const timeoutID = window.setTimeout(( () => {
location.reload();
}), 800);
}
});
};
if (confirm("{{ _('確定刪除照片?') }}")) {
fetch(`/admin/api/units/${unit.id}/media/${unit.cover_image.id}`, {
method: 'DELETE',
})
.then(resp => resp.json())
.then(json => {
if (json.message === 'ok') {
UIkit.notification('已刪除', {timeout: 500});
const timeoutID = window.setTimeout(( () => {
location.reload();
}), 800);
}
});
}
}; {# endof on click #}
coverImageWrapper.appendChild(coverImage);
coverImageWrapper.appendChild(coverImageDelete);
} else {
// upload new cover image
let coverImageFile = unitModal.querySelector(`[data-unit="cover-image-file"]`);
coverImageFile.id = `unit-${index}-cover-image-id`;
coverImageFile.setAttribute('value', coverImageUrl);
Expand Down
2 changes: 1 addition & 1 deletion app/templates/admin/inc_record_template-modal.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h6 class="uk-text-bold">{{ _('館號') }}: <span id="card-catalog-number"></spa
<div class="uk-card-footer">
<div class="uk-grid uk-grid-small uk-grid-divider uk-flex uk-flex-middle" data-uk-grid>
<div class="uk-width-expand uk-text-small">

<a id="card-frontend-link" data-uk-tooltip="title: {{ _('前台網頁') }}" href="#" class="uk-icon-link drag-icon" data-uk-icon="icon:image; ratio: 1" target="_blank"></a>
</div>
<div class="uk-width-auto uk-text-right">
{#
Expand Down
2 changes: 1 addition & 1 deletion app/templates/sites/hast/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h4 class="uk-heading-line uk-text-bold"><span>Featured Specimen</span></h4>
<div class="uk-grid uk-grid-medium uk-flex uk-flex-middle" data-uk-grid>
<div class="uk-width-1-3@s uk-width-2-5@m uk-height-1-1">
{#<img src="https://picsum.photos/500/500/?random=1" alt="">#}
<img src="{{ u.get_image() }}" alt="">
<img src="{{ u.get_cover_image() }}" alt="">
</div>
<div class="uk-width-2-3@s uk-width-3-5@m">
<span class="uk-label uk-label-warning" style="font-size: 0.75rem">Herbarium Sheet</span>
Expand Down
4 changes: 2 additions & 2 deletions app/templates/sites/hast/specimen-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
{% if current_user.is_authenticated %}<a class="uk-link-muted" href="{{ url_for('admin.modify_collection_record', collection_id=entity.record.collection_id, record_id=entity.record.id ) }}" target="_blank">編輯</a>{% endif %}
<div class="uk-child-width-1-2" uk-grid>
<div>{# left #}
{% with image_url = entity.get_image('l') %}{# TODO: x #}
{% with image_url = entity.get_cover_image('l') %}{# TODO: x #}
{% if image_url %}
<img width="480" height="480" alt="{{ entity }}" src="{{ image_url }}">
更大解析度: <a target="_blank" href="{{ image_url|replace('-l', '-x') }}">1365x2048 pixels</a> | <a target="_blank" href="{{ image_url|replace('-l', '-o') }}">2731x4096 pixels</a>
更大解析度: <a target="_blank" href="{{ entity.get_cover_image('x') }}">1365x2048 pixels</a> | <a target="_blank" href="{{ entity.get_cover_image('o') }}">2731x4096 pixels</a>
{% endif %}
{% endwith %}
{#
Expand Down

0 comments on commit 4445bc8

Please sign in to comment.