Skip to content

Commit a90ffa5

Browse files
committed
Rename identifier functions, prevent new empty fields from being saved
1 parent c94abe0 commit a90ffa5

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

openlibrary/components/IdentifiersInput.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</template>
5353

5454
<script>
55-
import { errorDisplay, validateEditionIdentifiers } from './IdentifiersInput/utils/utils.js';
55+
import { errorDisplay, validateIdentifiers } from './IdentifiersInput/utils/utils.js';
5656
const identifierPatterns = {
5757
wikidata: /^Q[1-9]\d*$/i,
5858
isni: /^[0]{4} ?[0-9]{4} ?[0-9]{4} ?[0-9]{3}[0-9X]$/i,
@@ -162,7 +162,7 @@ export default {
162162
if (this.saveIdentifiersAsList) {
163163
// collect id values of matching type, or empty array if none present
164164
const existingIds = this.assignedIdentifiers[this.selectedIdentifier] ?? [];
165-
const validEditionId = validateEditionIdentifiers(this.selectedIdentifier, this.inputValue, existingIds, this.output_selector);
165+
const validEditionId = validateIdentifiers(this.selectedIdentifier, this.inputValue, existingIds, this.output_selector);
166166
if (validEditionId) {
167167
if (!this.assignedIdentifiers[this.selectedIdentifier]) {
168168
this.inputValue = [this.inputValue];

openlibrary/components/IdentifiersInput/utils/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function validateLccn(value) {
6464
return true;
6565
}
6666

67-
export function validateEditionIdentifiers(name, value, entries, error_output) {
67+
export function validateIdentifiers(name, value, entries, error_output) {
6868
let validId = true;
6969
errorDisplay('', error_output);
7070
if (name === '' || name === '---') {

openlibrary/plugins/upstream/addbook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ def save(self, formdata: web.Storage) -> None:
596596
# we're trying to create an orphan; let's not do that
597597
edition_data.works = [{'key': self.work.key}]
598598
if self.work is not None:
599-
work_identifiers = work_data.pop('identifiers', [])
599+
work_identifiers = work_data.pop('identifiers', {})
600600
self.work.set_identifiers(work_identifiers)
601601
self.work.update(work_data)
602602
saveutil.save(self.work)
@@ -784,7 +784,7 @@ def read_subject(subjects):
784784
work.subtitle = None
785785

786786
for k in ['excerpts', 'links', 'identifiers']:
787-
work[k] = work.get(k, [])
787+
work[k] = work.get(k, {})
788788

789789
# ignore empty authors
790790
work.authors = [

openlibrary/plugins/upstream/models.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ def set_identifiers(self, identifiers):
358358
else:
359359
self.identifiers[name] = value
360360

361+
if not d.items():
362+
self.identifiers = None
363+
361364
def get_classifications(self):
362365
names = ["dewey_decimal_class", "lc_classifications"]
363366
return self._process_identifiers(
@@ -388,6 +391,9 @@ def set_classifications(self, classifications):
388391
else:
389392
self.classifications[name] = value
390393

394+
if not self.classifications.items():
395+
self.classifications = None
396+
391397
def get_weight(self):
392398
"""returns weight as a storage object with value and units fields."""
393399
w = self.weight
@@ -779,7 +785,6 @@ def get_identifiers(self):
779785

780786
def set_identifiers(self, identifiers):
781787
"""Updates the work from identifiers specified as (name, value) pairs."""
782-
names = ()
783788

784789
d = {}
785790
if identifiers:
@@ -793,10 +798,10 @@ def set_identifiers(self, identifiers):
793798
self.identifiers = {}
794799

795800
for name, value in d.items():
796-
if name in names:
797-
self[name] = value
798-
else:
799-
self.identifiers[name] = value
801+
self.identifiers[name] = value
802+
803+
if not d.items():
804+
self.identifiers = None
800805

801806
def _process_identifiers(self, config_, names, values):
802807
id_map = {}

openlibrary/plugins/upstream/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ def _get_identifier_config(identifier: Literal['work', 'edition', 'author']) ->
11901190
if identifier == 'edition':
11911191
thing = web.ctx.site.get('/config/edition')
11921192
classifications = [
1193-
Storage(t.dict()) for t in thing.classifications if 'name in t'
1193+
Storage(t.dict()) for t in thing.classifications if 'name' in t
11941194
]
11951195
roles = thing.roles
11961196
return Storage(

0 commit comments

Comments
 (0)