Merge remote-tracking branch 'identifier/develop/edit_identifiers' into Develop
This commit is contained in:
commit
8cbc345f36
|
@ -147,6 +147,26 @@ def modify_database_object(input_elements, db_book_object, db_object, db_session
|
|||
db_book_object.append(db_element)
|
||||
|
||||
|
||||
def modify_identifiers(input_identifiers, db_identifiers, db_session):
|
||||
"""Modify Identifiers to match input information.
|
||||
input_identifiers is a list of read-to-persist Identifiers objects.
|
||||
db_identifiers is a list of already persisted list of Identifiers objects."""
|
||||
input_dict = dict([ (identifier.type.lower(), identifier) for identifier in input_identifiers ])
|
||||
db_dict = dict([ (identifier.type.lower(), identifier) for identifier in db_identifiers ])
|
||||
# delete db identifiers not present in input or modify them with input val
|
||||
for identifier_type, identifier in db_dict.items():
|
||||
if identifier_type not in input_dict.keys():
|
||||
db_session.delete(identifier)
|
||||
else:
|
||||
input_identifier = input_dict[identifier_type]
|
||||
identifier.type = input_identifier.type
|
||||
identifier.val = input_identifier.val
|
||||
# add input identifiers not present in db
|
||||
for identifier_type, identifier in input_dict.items():
|
||||
if identifier_type not in db_dict.keys():
|
||||
db_session.add(identifier)
|
||||
|
||||
|
||||
@editbook.route("/delete/<int:book_id>/", defaults={'book_format': ""})
|
||||
@editbook.route("/delete/<int:book_id>/<string:book_format>/")
|
||||
@login_required
|
||||
|
@ -459,6 +479,10 @@ def edit_book(book_id):
|
|||
else:
|
||||
book.comments.append(db.Comments(text=to_save["description"], book=book.id))
|
||||
|
||||
# Handle identifiers
|
||||
input_identifiers = identifier_list(to_save, book)
|
||||
modify_identifiers(input_identifiers, book.identifiers, db.session)
|
||||
|
||||
# Handle book tags
|
||||
input_tags = to_save["tags"].split(',')
|
||||
input_tags = list(map(lambda it: it.strip(), input_tags))
|
||||
|
@ -548,6 +572,19 @@ def merge_metadata(to_save, meta):
|
|||
to_save["description"] = to_save["description"] or Markup(
|
||||
getattr(meta, 'description', '')).unescape()
|
||||
|
||||
def identifier_list(to_save, book):
|
||||
"""Generate a list of Identifiers from form information"""
|
||||
id_type_prefix = 'identifier-type-'
|
||||
id_val_prefix = 'identifier-val-'
|
||||
result = []
|
||||
for type_key, type_value in to_save.items():
|
||||
if not type_key.startswith(id_type_prefix):
|
||||
continue
|
||||
val_key = id_val_prefix + type_key[len(id_type_prefix):]
|
||||
if val_key not in to_save.keys():
|
||||
continue
|
||||
result.append( db.Identifiers(to_save[val_key], type_value, book.id) )
|
||||
return result
|
||||
|
||||
@editbook.route("/upload", methods=["GET", "POST"])
|
||||
@login_required_if_no_ano
|
||||
|
|
|
@ -61,6 +61,21 @@
|
|||
<label for="description">{{_('Description')}}</label>
|
||||
<textarea class="form-control" name="description" id="description" rows="7">{% if book.comments %}{{book.comments[0].text}}{%endif%}</textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>{{_('Identifiers')}}</label>
|
||||
<table class="table" id="identifier-table">
|
||||
{% for identifier in book.identifiers %}
|
||||
<tr>
|
||||
<td><input type="text" class="form-control" name="identifier-type-{{identifier.type}}" value="{{identifier.type}}" required="required" placeholder="{{_('Identifier Type')}}"></td>
|
||||
<td><input type="text" class="form-control" name="identifier-val-{{identifier.type}}" value="{{identifier.val}}" required="required" placeholder="{{_('Identifier Value')}}"></td>
|
||||
<td><a class="btn btn-default" onclick="removeIdentifierLine(this)">{{_('Remove')}}</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<a id="add-identifier-line" class="btn btn-default">{{_('Add Identifier')}}</a>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="tags">{{_('Tags')}}</label>
|
||||
<input type="text" class="form-control typeahead" name="tags" id="tags" value="{% for tag in book.tags %}{{tag.name.strip()}}{% if not loop.last %}, {% endif %}{% endfor %}">
|
||||
|
@ -274,6 +289,21 @@
|
|||
'source': {{_('Source')|safe|tojson}},
|
||||
};
|
||||
var language = '{{ g.user.locale }}';
|
||||
|
||||
$("#add-identifier-line").click(function() {
|
||||
// create a random identifier type to have a valid name in form. This will not be used when dealing with the form
|
||||
var rand_id = Math.floor(Math.random() * 1000000).toString();
|
||||
var line = '<tr>';
|
||||
line += '<td><input type="text" class="form-control" name="identifier-type-'+ rand_id +'" required="required" placeholder="{{_('Identifier Type')}}"></td>';
|
||||
line += '<td><input type="text" class="form-control" name="identifier-val-'+ rand_id +'" required="required" placeholder="{{_('Identifier Value')}}"></td>';
|
||||
line += '<td><a class="btn btn-default" onclick="removeIdentifierLine(this)">{{_('Remove')}}</a></td>';
|
||||
line += '</tr>';
|
||||
$("#identifier-table").append(line);
|
||||
});
|
||||
function removeIdentifierLine(el) {
|
||||
$(el).parent().parent().remove();
|
||||
}
|
||||
|
||||
</script>
|
||||
<script src="{{ url_for('static', filename='js/libs/typeahead.bundle.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='js/libs/bootstrap-rating-input.min.js') }}"></script>
|
||||
|
|
Loading…
Reference in New Issue
Block a user