add support for is_multiple custom columns

pull/16/head
Cervinko Cera 9 years ago
parent 3f1a54ea59
commit b2146ba4b9

@ -47,11 +47,17 @@
{% for c in cc %} {% for c in cc %}
<div class="form-group"> <div class="form-group">
<label for="{{ 'custom_column_' ~ c.id }}">{{ c.name }}</label> <label for="{{ 'custom_column_' ~ c.id }}">{{ c.name }}</label>
{% if c.datatype in ['text', 'series'] %} {% if c.datatype in ['text', 'series'] and not c.is_multiple %}
<input type="text" class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}" <input type="text" class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}"
{% if book['custom_column_' ~ c.id]|length > 0 %} {% if book['custom_column_' ~ c.id]|length > 0 %}
{% for column in book['custom_column_' ~ c.id] %} value="{{ book['custom_column_' ~ c.id][0].value }}"
value="{{ column.value }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}> {% endif %}>
{% endif %}
{% if c.datatype in ['text', 'series'] and c.is_multiple %}
<input type="text" class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}"
{% if book['custom_column_' ~ c.id]|length > 0 %}
value="{% for column in book['custom_column_' ~ c.id] %}{{ column.value.strip() }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}>
{% endif %} {% endif %}
{% if c.datatype == 'enumeration' %} {% if c.datatype == 'enumeration' %}

@ -840,37 +840,83 @@ def edit_book(book_id):
for c in cc: for c in cc:
cc_string = "custom_column_" + str(c.id) cc_string = "custom_column_" + str(c.id)
if len(getattr(book, cc_string)) > 0: if not c.is_multiple:
cc_db_value = getattr(book, cc_string)[0].value if len(getattr(book, cc_string)) > 0:
else: cc_db_value = getattr(book, cc_string)[0].value
cc_db_value = None else:
if to_save[cc_string].strip(): cc_db_value = None
if c.datatype == 'rating': if to_save[cc_string].strip():
to_save[cc_string] = str(int(float(to_save[cc_string]) *2)) if c.datatype == 'rating':
print to_save[cc_string] to_save[cc_string] = str(int(float(to_save[cc_string]) *2))
if to_save[cc_string].strip() != cc_db_value: print to_save[cc_string]
if to_save[cc_string].strip() != cc_db_value:
if cc_db_value != None:
#remove old cc_val
del_cc = getattr(book, cc_string)[0]
getattr(book, cc_string).remove(del_cc)
if len(del_cc.books) == 0:
db.session.delete(del_cc)
cc_class = db.cc_classes[c.id]
new_cc = db.session.query(cc_class).filter(cc_class.value == to_save[cc_string].strip()).first()
# if no cc val is found add it
if new_cc == None:
new_cc = cc_class(value=to_save[cc_string].strip())
db.session.add(new_cc)
new_cc = db.session.query(cc_class).filter(cc_class.value == to_save[cc_string].strip()).first()
# add cc value to book
getattr(book, cc_string).append(new_cc)
else:
if cc_db_value != None: if cc_db_value != None:
#remove old cc_val #remove old cc_val
del_cc = getattr(book, cc_string)[0] del_cc = getattr(book, cc_string)[0]
getattr(book, cc_string).remove(del_cc) getattr(book, cc_string).remove(del_cc)
if len(del_cc.books) == 0: if len(del_cc.books) == 0:
db.session.delete(del_cc) db.session.delete(del_cc)
cc_class = db.cc_classes[c.id]
new_cc = db.session.query(cc_class).filter(cc_class.value == to_save[cc_string].strip()).first()
# if no cc val is found add it
if new_cc == None:
new_cc = cc_class(value=to_save[cc_string].strip())
db.session.add(new_cc)
new_cc = db.session.query(cc_class).filter(cc_class.value == to_save[cc_string].strip()).first()
# add cc value to book
getattr(book, cc_string).append(new_cc)
else: else:
if cc_db_value != None: input_tags = to_save[cc_string].split(',')
#remove old cc_val input_tags = map(lambda it: it.strip(), input_tags)
del_cc = getattr(book, cc_string)[0] input_tags = [x for x in input_tags if x != '']
getattr(book, cc_string).remove(del_cc) # we have all author names now
if len(del_cc.books) == 0: # 1. search for tags to remove
db.session.delete(del_cc) del_tags = []
for c_tag in getattr(book, cc_string):
found = False
for inp_tag in input_tags:
if inp_tag == c_tag.value:
found = True
break;
# if the tag was not found in the new list, add him to remove list
if not found:
del_tags.append(c_tag)
# 2. search for tags that need to be added
add_tags = []
for inp_tag in input_tags:
found = False
for c_tag in getattr(book, cc_string):
if inp_tag == c_tag.value:
found = True
break;
if not found:
add_tags.append(inp_tag)
# if there are tags to remove, we remove them now
if len(del_tags) > 0:
for del_tag in del_tags:
getattr(book, cc_string).remove(del_tag)
if len(del_tag.books) == 0:
db.session.delete(del_tag)
# if there are tags to add, we add them now!
if len(add_tags) > 0:
for add_tag in add_tags:
# check if a tag with that name exists
new_tag = db.session.query(db.cc_classes[c.id]).filter(db.cc_classes[c.id].value == add_tag).first()
# if no tag is found add it
if new_tag == None:
print add_tag
new_tag = db.cc_classes[c.id](value=add_tag)
db.session.add(new_tag)
new_tag = db.session.query(db.cc_classes[c.id]).filter(db.cc_classes[c.id].value == add_tag).first()
# add tag to book
getattr(book, cc_string).append(new_tag)
db.session.commit() db.session.commit()
author_names = [] author_names = []

Loading…
Cancel
Save