-
Notifications
You must be signed in to change notification settings - Fork 92
Implemented and added media chooser into the edit translation workflow for audio and video files #847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Implemented and added media chooser into the edit translation workflow for audio and video files #847
Changes from 1 commit
a482f17
7d8779c
fc85a34
83d0beb
17f2ef3
703341d
f16a115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
import gettext from 'gettext'; | ||
|
||
interface MediaAPI { | ||
result: { | ||
id: number | ||
title: string; | ||
edit_url: string; | ||
}; | ||
} | ||
|
||
interface MediaChooserProps { | ||
adminBaseUrl: string; | ||
mediaId: number | null; | ||
} | ||
|
||
const MediaChooser: FunctionComponent<MediaChooserProps> = ({ | ||
adminBaseUrl, | ||
mediaId, | ||
}) => { | ||
const [mediaInfo, setMediaInfo] = React.useState<MediaAPI | null>(null); | ||
|
||
React.useEffect(() => { | ||
setMediaInfo(null); | ||
if (mediaId) { | ||
fetch(`${adminBaseUrl}media/chooser/${mediaId}/`) | ||
.then((response) => response.json()) | ||
.then(setMediaInfo); | ||
} | ||
}, [mediaId]); | ||
|
||
// Render | ||
let classNames = ['chooser', 'media-chooser']; | ||
let inner; | ||
if (mediaId) { | ||
if (mediaInfo) { | ||
inner = ( | ||
<div className="chosen"> | ||
<div className="preview-media"> | ||
<strong>{mediaInfo.result.title} (ID: {mediaInfo.result.id})</strong> | ||
</div> | ||
|
||
<ul className="actions" style={{ listStyleType: 'none' }}> | ||
<li> | ||
<a | ||
href={`${mediaInfo.result.edit_url}`} | ||
className="edit-link button button-small button-secondary" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
{gettext('Edit this media')} | ||
</a> | ||
</li> | ||
</ul> | ||
</div> | ||
); | ||
} else { | ||
inner = <p>{gettext('Fetching media information...')}</p>; | ||
} | ||
} else { | ||
classNames.push('blank'); | ||
|
||
inner = ( | ||
<div className="unchosen"> | ||
<button | ||
type="button" | ||
className="button action-choose button-small button-secondary" | ||
> | ||
{gettext('Choose a media')} | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
return <div className={classNames.join(' ')}>{inner}</div>; | ||
}; | ||
|
||
export default MediaChooser; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
window.chooserUrls = { | ||
imageChooser: "{% url "wagtailimages_chooser:choose" %}", | ||
documentChooser: "{% url "wagtaildocs_chooser:choose" %}", | ||
mediaChooser: "{% url "wagtailmedia:chooser" %}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this work without the hard dependency, we'll need to move the chooserUrls logic to a template tag and conditionally add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As suggested, I create a new template tag method which checks the wagtailmedia installation and adds the "wagtailmedia:chooser" conditionally into the translation templates. My local tests did work on test environment instance. |
||
pageChooser: "{% url "wagtailadmin_choose_page" %}", | ||
} | ||
} | ||
|
@@ -65,6 +66,8 @@ | |
<script src="{% versioned_static 'wagtaildocs/js/document-chooser-modal.js' %}"></script> | ||
<script src="{% versioned_static 'wagtaildocs/js/document-chooser.js' %}"></script> | ||
<script src="{% versioned_static 'wagtailadmin/js/chooser-modal.js' %}"></script> | ||
<script src="{% versioned_static 'wagtailmedia/js/media-chooser-modal.js' %}"></script> | ||
<script src="{% versioned_static 'wagtailmedia/js/tabs.js' %}"></script> | ||
<script src="{% versioned_static 'wagtailsnippets/js/snippet-chooser.js' %}"></script> | ||
<script src="{% versioned_static 'wagtail_localize/js/wagtail-localize.js' %}"></script> | ||
{% endblock %} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -51,6 +51,7 @@ | |||||
from wagtail.snippets.models import get_snippet_models | ||||||
from wagtail.snippets.permissions import get_permission_name, user_can_edit_snippet_type | ||||||
from wagtail.utils.decorators import xframe_options_sameorigin_override | ||||||
from wagtailmedia.blocks import AudioChooserBlock, VideoChooserBlock | ||||||
|
||||||
from wagtail_localize.compat import DATE_FORMAT | ||||||
from wagtail_localize.machine_translators import get_machine_translator | ||||||
|
@@ -366,6 +367,9 @@ def widget_from_block(block, content_components=None): | |||||
elif isinstance(block, ImageChooserBlock): | ||||||
return {"type": "image_chooser"} | ||||||
|
||||||
elif isinstance(block, AudioChooserBlock) or isinstance(block, VideoChooserBlock): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrap in an
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the blocks into an inline import if the wagtailmedia has been installed. The code structure didn't change. |
||||||
return {"type": "media_chooser"} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, need to check whether the direct field (that is the ForeignKey) is to a media class and set the widget type for it as well in wagtail-localize/wagtail_localize/views/edit_translation.py Lines 321 to 322 in b619a32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above - same procedure applied here. |
||||||
elif isinstance(block, SnippetChooserBlock): | ||||||
chooser_url = reverse( | ||||||
f"wagtailsnippetchoosers_{block.target_model._meta.app_label}_{block.target_model._meta.model_name}:choose" | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs to move to dev dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the "wagtailmedia" dependency from
tox.ini
intopyproject.toml
under the testing label.