Added tag

This commit is contained in:
Alice 2018-05-23 11:16:25 +02:00
parent 1b63206a80
commit 923da074a6
6 changed files with 12 additions and 3 deletions

View File

@ -5,4 +5,5 @@ from wtforms.validators import InputRequired
class UserForm(FlaskForm):
title = StringField('title', validators=[InputRequired()])
author = StringField('author', validators=[InputRequired()])
tag = StringField('tag', validators=[InputRequired()])
file = FileField()

View File

@ -6,12 +6,14 @@ class Book(db.Model):
title = db.Column(db.String(255))
author = db.Column(db.String(255))
file = db.Column(db.String(255))
tag = db.Column(db.String(255))
def __init__(self, title, author, file):
def __init__(self, title, author, file, tag):
self.title = title
self.author = author
self.file = file
self.tag = tag
def __repr__(self):
return '<Title %r>' % self.title
@ -22,7 +24,8 @@ class BookSchema(Schema):
title = fields.Str()
author = fields.Str()
file = fields.Str()
tag = fields.Str()
def must_not_be_blank(data):
if not data:
raise ValidationError('Data not provided.')
raise ValidationError('You forgot to write stuff.')

View File

@ -18,6 +18,8 @@
{{ form.csrf_token }}
<div class="form-group">{{ form.title.label }} {{ form.title(size=20, class="form-control") }}</div>
<div class="form-group">{{ form.author.label }} {{ form.author(size=20, class="form-control") }}</div>
<div class="form-group">{{ form.tag.label }} {{ form.tag(size=20, class="form-control") }}</div>
{{ form.file }}
<button type="submit" class="btn btn-primary">Upload</button>
</form>

View File

@ -19,11 +19,13 @@
<tr>
<th>Title</th>
<th>Author</th>
<th>Tag</th>
</tr>
{% for book in books %}
<tr>
<td><a href="books/{{ book.id }}">{{ book.title }}</a></td>
<td>{{ book.author }}</td>
<td>{{ book.tag}}</td>
</tr>
{% endfor %}
</table>

View File

@ -82,8 +82,9 @@ def add_book():
title = user_form.title.data # You could also have used request.form['name']
author = user_form.author.data # You could also have used request.form['email']
tag = user_form.tag.data
# save user to database
book = Book(title, author, filename)
book = Book(title, author, filename, tag)
db.session.add(book)
db.session.commit()
flash("%s added to the library" % (title))