Skip to content

Commit

Permalink
examples: fix books_collection example
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Apr 23, 2014
1 parent 4cfa7a9 commit 36cf00c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
10 changes: 6 additions & 4 deletions examples/books_collection/collection/__init__.py
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-

# Copyright 2010 flask-mongoalchemy authors. All rights reserved.
# Copyright 2014 flask-mongoalchemy authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from flask import Flask
from flaskext.mongoalchemy import MongoAlchemy
from flask.ext.mongoalchemy import MongoAlchemy
import string

app = Flask(__name__)
Expand All @@ -14,8 +14,10 @@
app.config['DEBUG'] = True
db = MongoAlchemy(app)


@app.context_processor
def put_letters_on_request():
return { 'letters' : string.ascii_uppercase }
return {'letters': string.ascii_uppercase}

from views import *
import views
_ = views
8 changes: 5 additions & 3 deletions examples/books_collection/collection/documents.py
@@ -1,18 +1,20 @@
# -*- coding: utf-8 -*-

# Copyright 2010 flask-mongoalchemy authors. All rights reserved.
# Copyright 2014 flask-mongoalchemy authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from collection import db
from flaskext.mongoalchemy import BaseQuery
from flask.ext.mongoalchemy import BaseQuery
import re


class BookQuery(BaseQuery):

def starting_with(self, letter):
regex = r'^' + letter
return self.filter({'title' : re.compile(regex, re.IGNORECASE)})
return self.filter({'title': re.compile(regex, re.IGNORECASE)})


class Book(db.Document):
query_class = BookQuery
Expand Down
8 changes: 6 additions & 2 deletions examples/books_collection/collection/forms.py
Expand Up @@ -4,13 +4,17 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import wtforms
from flask.ext import wtf
from wtforms import validators

from collection.documents import Book


class BookForm(wtf.Form):
document_class = Book
title = wtf.TextField(validators=[wtf.Required()])
year = wtf.IntegerField(validators=[wtf.Required()])
title = wtforms.TextField(validators=[validators.Required()])
year = wtforms.IntegerField(validators=[validators.Required()])
instance = None

def __init__(self, document=None, *args, **kwargs):
Expand Down
Expand Up @@ -7,7 +7,7 @@
{% block content %}
<h1 id="">Edit book {{ book.title }}</h1>
<form action="" method="post" accept-charset="utf-8">
{{ form.csrf }}
{{ form.csrf_token }}
<p>{{ form.title.label }}: {{ form.title }}</p>
<p>{{ form.year.label }}: {{ form.year }}</p>
<p><input type="submit" value="Save book"/></p>
Expand Down
Expand Up @@ -7,7 +7,7 @@
{% block content %}
<h1 id="">New book</h1>
<form action="" method="post" accept-charset="utf-8">
{{ form.csrf }}
{{ form.csrf_token }}
<p>{{ form.title.label }}: {{ form.title }}</p>
<p>{{ form.year.label }}: {{ form.year }}</p>
<p><input type="submit" value="Save book"/></p>
Expand Down
14 changes: 11 additions & 3 deletions examples/books_collection/collection/views.py
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-

# Copyright 2010 flask-mongoalchemy authors. All rights reserved.
# Copyright 2014 flask-mongoalchemy authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from flask import render_template, redirect, url_for

from collection import app
from collection.forms import BookForm
from collection.documents import Book
from flask import render_template, redirect, url_for


@app.route('/books/new', methods=['GET', 'POST'])
def new_book():
Expand All @@ -17,32 +19,38 @@ def new_book():
return redirect(url_for('list_books'))
return render_template('/books/new.html', form=form)


@app.route('/books')
@app.route('/books/<int:page>')
def list_books(page=1):
title = u'Books list'
pagination = Book.query.paginate(page=page, per_page=5)
return render_template('/books/list_all.html', pagination=pagination, title=title)


@app.route('/books/<letter>')
@app.route('/books/<letter>/<int:page>')
def list_books_filtering(letter, page=1):
title = u'Books starting with %s' % letter.upper()
pagination = Book.query.starting_with(letter).paginate(page=page, per_page=5)
return render_template('/books/list_filtered.html', pagination=pagination, title=title, letter=letter)
return render_template('/books/list_filtered.html', pagination=pagination,
title=title, letter=letter)


@app.route('/books/delete/<id>')
def delete_book(id):
book = Book.query.get_or_404(id)
book.remove()
return redirect(url_for('list_books'))


@app.route('/books/edit/<id>')
def edit_book(id):
book = Book.query.get(id)
form = BookForm(document=book)
return render_template('/books/edit.html', form=form, book=book)


@app.route('/books/edit/<id>', methods=['POST'])
def update_book(id):
book = Book.query.get(id)
Expand Down
1 change: 1 addition & 0 deletions examples/books_collection/test_collection.py
Expand Up @@ -7,6 +7,7 @@
import unittest
import collection


class BooksCollectionTestCase(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit 36cf00c

Please sign in to comment.