Skip to content

Commit

Permalink
Rename identifier functions, prevent new empty fields from being saved
Browse files Browse the repository at this point in the history
  • Loading branch information
schu96 committed Jan 8, 2025
1 parent c94abe0 commit a90ffa5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions openlibrary/components/IdentifiersInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</template>

<script>
import { errorDisplay, validateEditionIdentifiers } from './IdentifiersInput/utils/utils.js';
import { errorDisplay, validateIdentifiers } from './IdentifiersInput/utils/utils.js';
const identifierPatterns = {
wikidata: /^Q[1-9]\d*$/i,
isni: /^[0]{4} ?[0-9]{4} ?[0-9]{4} ?[0-9]{3}[0-9X]$/i,
Expand Down Expand Up @@ -162,7 +162,7 @@ export default {
if (this.saveIdentifiersAsList) {
// collect id values of matching type, or empty array if none present
const existingIds = this.assignedIdentifiers[this.selectedIdentifier] ?? [];
const validEditionId = validateEditionIdentifiers(this.selectedIdentifier, this.inputValue, existingIds, this.output_selector);
const validEditionId = validateIdentifiers(this.selectedIdentifier, this.inputValue, existingIds, this.output_selector);
if (validEditionId) {
if (!this.assignedIdentifiers[this.selectedIdentifier]) {
this.inputValue = [this.inputValue];
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/components/IdentifiersInput/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function validateLccn(value) {
return true;
}

export function validateEditionIdentifiers(name, value, entries, error_output) {
export function validateIdentifiers(name, value, entries, error_output) {
let validId = true;
errorDisplay('', error_output);
if (name === '' || name === '---') {
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/plugins/upstream/addbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def save(self, formdata: web.Storage) -> None:
# we're trying to create an orphan; let's not do that
edition_data.works = [{'key': self.work.key}]
if self.work is not None:
work_identifiers = work_data.pop('identifiers', [])
work_identifiers = work_data.pop('identifiers', {})
self.work.set_identifiers(work_identifiers)
self.work.update(work_data)
saveutil.save(self.work)
Expand Down Expand Up @@ -784,7 +784,7 @@ def read_subject(subjects):
work.subtitle = None

for k in ['excerpts', 'links', 'identifiers']:
work[k] = work.get(k, [])
work[k] = work.get(k, {})

# ignore empty authors
work.authors = [
Expand Down
15 changes: 10 additions & 5 deletions openlibrary/plugins/upstream/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ def set_identifiers(self, identifiers):
else:
self.identifiers[name] = value

if not d.items():
self.identifiers = None

def get_classifications(self):
names = ["dewey_decimal_class", "lc_classifications"]
return self._process_identifiers(
Expand Down Expand Up @@ -388,6 +391,9 @@ def set_classifications(self, classifications):
else:
self.classifications[name] = value

if not self.classifications.items():
self.classifications = None

def get_weight(self):
"""returns weight as a storage object with value and units fields."""
w = self.weight
Expand Down Expand Up @@ -779,7 +785,6 @@ def get_identifiers(self):

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

d = {}
if identifiers:
Expand All @@ -793,10 +798,10 @@ def set_identifiers(self, identifiers):
self.identifiers = {}

for name, value in d.items():
if name in names:
self[name] = value
else:
self.identifiers[name] = value
self.identifiers[name] = value

if not d.items():
self.identifiers = None

def _process_identifiers(self, config_, names, values):
id_map = {}
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/plugins/upstream/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ def _get_identifier_config(identifier: Literal['work', 'edition', 'author']) ->
if identifier == 'edition':
thing = web.ctx.site.get('/config/edition')
classifications = [
Storage(t.dict()) for t in thing.classifications if 'name in t'
Storage(t.dict()) for t in thing.classifications if 'name' in t
]
roles = thing.roles
return Storage(
Expand Down

0 comments on commit a90ffa5

Please sign in to comment.