Browse Source

Added tag

ansible-setup-and-deploy
Alice 6 years ago
parent
commit
923da074a6
  1. BIN
      app/__pycache__/__init__.cpython-36.pyc
  2. 1
      app/forms.py
  3. 7
      app/models.py
  4. 2
      app/templates/add_book.html
  5. 2
      app/templates/show_books.html
  6. 3
      app/views.py

BIN
app/__pycache__/__init__.cpython-36.pyc

Binary file not shown.

1
app/forms.py

@ -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()

7
app/models.py

@ -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.')

2
app/templates/add_book.html

@ -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>

2
app/templates/show_books.html

@ -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>

3
app/views.py

@ -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))

Loading…
Cancel
Save