Add ability to edit publish date

If a date is not set, default to “0101-01-01”
pull/227/head
Jonathan Rehm 7 years ago
parent cf7196ae5e
commit 04b04e200f

@ -230,6 +230,8 @@ class Data(Base):
class Books(Base):
__tablename__ = 'books'
DEFAULT_PUBDATE = "0101-01-01"
id = Column(Integer, primary_key=True)
title = Column(String)
sort = Column(String)

@ -52,6 +52,10 @@
<input type="text" class="form-control" name="cover_url" id="cover_url" value="">
</div>
<div class="form-group">
<label for="pubdate">{{_('Published')}}</label>
<input type="date" class="form-control" name="pubdate" id="pubdate" value="{% if book.pubdate %}{{book.pubdate|formatdateinput}}{% endif %}">
</div>
<div class="form-group">
<label for="languages">{{_('Language')}}</label>
<input type="text" class="form-control typeahead" name="languages" id="languages" value="{% for language in book.languages %}{{language.language_name.strip()}}, {% endfor %}">

@ -415,6 +415,14 @@ def formatdate(val):
return format_date(formatdate, format='medium', locale=get_locale())
@app.template_filter('formatdateinput')
def format_date_input(val):
conformed_timestamp = re.sub(r"[:]|([-](?!((\d{2}[:]\d{2})|(\d{4}))$))", '', val)
date_obj = datetime.datetime.strptime(conformed_timestamp[:15], "%Y%m%d %H%M%S")
input_date = date_obj.isoformat().split('T', 1)[0] # Hack to support dates <1900
return '' if input_date == "0101-01-01" else input_date
@app.template_filter('strftime')
def timestamptodate(date, fmt=None):
date = datetime.datetime.fromtimestamp(
@ -2763,6 +2771,14 @@ def edit_book(book_id):
input_languages = to_save["languages"].split(',')
input_languages = map(lambda it: it.strip().lower(), input_languages)
if to_save["pubdate"]:
try:
book.pubdate = datetime.datetime.strptime(to_save["pubdate"], "%Y-%m-%d")
except ValueError:
book.pubdate = db.Books.DEFAULT_PUBDATE
else:
book.pubdate = db.Books.DEFAULT_PUBDATE
# retranslate displayed text to language codes
languages = db.session.query(db.Languages).all()
input_l = []

Loading…
Cancel
Save