Skip to content

Commit

Permalink
Add checkboxes for copying authors and subjects to new work
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini committed Aug 27, 2024
1 parent 9c4db66 commit 7f28168
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3937,6 +3937,14 @@ msgstr ""
msgid "WARNING: Any edits made to the work from this page will be ignored."
msgstr ""

#: books/edit/edition.html
msgid "Copy authors to new work"
msgstr ""

#: books/edit/edition.html
msgid "Copy subjects to new work"
msgstr ""

#: books/edit/edition.html
msgid "Please select an identifier."
msgstr ""
Expand Down
5 changes: 5 additions & 0 deletions openlibrary/plugins/openlibrary/js/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export function initWorksMultiInputAutocomplete() {
});
});
});

// Show the new work options checkboxes only if "New work" selected
$('input[name="works--0"]').on('autocompleteselect', function(_event, ui) {
$('.new-work-options').toggle(ui.item.key === '__new__');
});
}

export function initSeedsMultiInputAutocomplete() {
Expand Down
29 changes: 27 additions & 2 deletions openlibrary/plugins/upstream/addbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,33 @@ def save(self, formdata: web.Storage) -> None:
# Create a new work if so desired
new_work_key = (edition_data.get('works') or [{'key': None}])[0]['key']
if new_work_key == "__new__" and self.work is not None:
self.work = self.new_work(self.edition)
edition_data.works = [{'key': self.work.key}]
new_work = self.new_work(self.edition)
edition_data.works = [{'key': new_work.key}]

new_work_options = formdata.get(
'new_work_options',
{
'copy_authors': 'no',
'copy_subjects': 'no',
},
)

if (
new_work_options.get('copy_authors') == 'yes'
and 'authors' in self.work
):
new_work.authors = self.work.authors
if new_work_options.get('copy_subjects') == 'yes':
for field in (
'subjects',
'subject_places',
'subject_times',
'subject_people',
):
if field in self.work:
new_work[field] = self.work[field]

self.work = new_work
saveutil.save(self.work)

identifiers = edition_data.pop('identifiers', [])
Expand Down
10 changes: 10 additions & 0 deletions openlibrary/templates/books/edit/edition.html
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@
$else:
$:render_work_field(0, storage(title="", key=""))
</div>
<div class="new-work-options" style="display:none">
<label>
<input type="checkbox" name="new_work_options--copy_authors" value="yes">
$_("Copy authors to new work")
</label>
<label>
<input type="checkbox" name="new_work_options--copy_subjects" value="yes">
$_("Copy subjects to new work")
</label>
</div>
</div>

</div>
Expand Down

0 comments on commit 7f28168

Please sign in to comment.