diff --git a/app/forms.py b/app/forms.py index 5db22e4..6eeb292 100755 --- a/app/forms.py +++ b/app/forms.py @@ -35,8 +35,8 @@ class StackForm(FlaskForm): create = SubmitField(label='Create') class AddtoStackForm(FlaskForm): - select = SelectField('Stacks', validators=[InputRequired()]) - + select_stack = SelectField('Stacks', validators=[InputRequired()]) + class SearchForm(FlaskForm): choices = [('All', 'All'), ('Title', 'Title'), diff --git a/app/models.py b/app/models.py index 0b281fd..d2361e5 100755 --- a/app/models.py +++ b/app/models.py @@ -97,7 +97,7 @@ class Stack(db.Model): self.stack_description = stack_description def __repr__(self): - return '' % self.stack + return '' % self.stack_name class AuthorSchema(Schema): id = fields.Int(dump_only=True) diff --git a/app/templates/add_to_stacks.html b/app/templates/add_to_stacks.html index 7576cbd..144b15b 100644 --- a/app/templates/add_to_stacks.html +++ b/app/templates/add_to_stacks.html @@ -10,21 +10,14 @@

These are all the stacks that have been built so far.

+{% from "_formhelpers.html" import render_field %} +
+ + +
- - - -

Build a stack

diff --git a/app/views.py b/app/views.py index 862bd12..39ea8e6 100755 --- a/app/views.py +++ b/app/views.py @@ -276,19 +276,6 @@ def add_stack(): stack = Stack(stack_name, stack_description) db.session.add(stack) stacks = db.session.query(Stack).all() -#this potentially deals with the connection between books and stacks - #book = Book(title, filename, cover, file_extension, category,year_published) - - #for book in books: - # book_title = book.get("title") - - # if book_title: - # a = db.session.query(Book).filter_by(book_title=book_title).first() - # if a == None: - # a = Book(book_title=book_title) - # db.session.add(a) - # stacks.book.append(a) - #db.session.commit() flash("%s stack created" % (stack_name)) return render_template('add_stack.html', stacks=stacks, form=form) @@ -371,14 +358,20 @@ def test1(): return Response(json.dumps(autocomplete_suggestions), mimetype='application/json') -@app.route('/add_to_stack/') - +@app.route('/add_to_stack/', methods=['GET', 'POST']) def add_to_stack(id): stacks = db.session.query(Stack).all() - book = Book.query.get(id) - add = AddtoStackForm(request.form) - - return render_template('add_to_stacks.html', id=id, stacks=stacks, book=book, add=add) + add_form = AddtoStackForm(request.form) + add_form.select_stack.choices = [(stack.id, stack.stack_name) for stack in stacks] + if request.method == 'GET': + book = Book.query.get(id) + return render_template('add_to_stacks.html', id=id, stacks=stacks, book=book, add_form=add_form) + else: + stack = Stack.query.get(int(add_form.select_stack.data)) + book = Book.query.get(id) + stack.books.append(book) + db.session.commit() + return render_template('show_stack_detail.html', stack=stack) ### # The API