| 1 |
.. _utils: |
| 2 |
|
| 3 |
Utilities |
| 4 |
========= |
| 5 |
|
| 6 |
flask-peewee ships with several useful utilities. If you're coming from the |
| 7 |
django world, some of these functions may look familiar to you. |
| 8 |
|
| 9 |
|
| 10 |
Getting objects |
| 11 |
--------------- |
| 12 |
|
| 13 |
:py:func:`get_object_or_404` |
| 14 |
|
| 15 |
Provides a handy way of getting an object or 404ing if not found, useful |
| 16 |
for urls that match based on ID. |
| 17 |
|
| 18 |
.. code-block:: python |
| 19 |
|
| 20 |
@app.route('/blog/<title>/') |
| 21 |
def blog_detail(title): |
| 22 |
blog = get_object_or_404(Blog.select().where(Blog.active==True), Blog.title==title) |
| 23 |
return render_template('blog/detail.html', blog=blog) |
| 24 |
|
| 25 |
:py:func:`object_list` |
| 26 |
|
| 27 |
Wraps the given query and handles pagination automatically. Pagination defaults to ``20`` |
| 28 |
but can be changed by passing in ``paginate_by=XX``. |
| 29 |
|
| 30 |
.. code-block:: python |
| 31 |
|
| 32 |
@app.route('/blog/') |
| 33 |
def blog_list(): |
| 34 |
active = Blog.select().where(Blog.active==True) |
| 35 |
return object_list('blog/index.html', active) |
| 36 |
|
| 37 |
.. code-block:: html+jinja |
| 38 |
|
| 39 |
<!-- template --> |
| 40 |
{% for blog in object_list %} |
| 41 |
{# render the blog here #} |
| 42 |
{% endfor %} |
| 43 |
|
| 44 |
{% if page > 1 %} |
| 45 |
<a href="./?page={{ page - 1 }}">Prev</a> |
| 46 |
{% endif %} |
| 47 |
{% if page < pagination.get_pages() %} |
| 48 |
<a href="./?page={{ page + 1 }}">Next</a> |
| 49 |
{% endif %} |
| 50 |
|
| 51 |
:py:class:`PaginatedQuery` |
| 52 |
|
| 53 |
A wrapper around a query (or model class) that handles pagination. |
| 54 |
|
| 55 |
Example: |
| 56 |
|
| 57 |
.. code-block:: python |
| 58 |
|
| 59 |
query = Blog.select().where(Blog.active==True) |
| 60 |
pq = PaginatedQuery(query, 20) # 20 items per page |
| 61 |
|
| 62 |
# assume url was /?page=3 |
| 63 |
obj_list = pq.get_list() # returns 3rd page of results |
| 64 |
|
| 65 |
pq.get_page() # returns "3" |
| 66 |
|
| 67 |
pq.get_pages() # returns total objects / objects-per-page |
| 68 |
|
| 69 |
pq.get_count() # total number of matching rows |
| 70 |
|
| 71 |
# a windowed list of page numbers for pagination controls; None marks a |
| 72 |
# gap to render as an ellipsis. On page 10 of 20 it returns: |
| 73 |
pq.get_page_range() # [1, None, 7, 8, 9, 10, 11, 12, 13, None, 20] |
| 74 |
|
| 75 |
``get_page_range(window=N)`` controls how many pages are shown on either side |
| 76 |
of the current page (the default is 3). |
| 77 |
|
| 78 |
|
| 79 |
Misc |
| 80 |
---- |
| 81 |
|
| 82 |
|
| 83 |
.. py:function:: slugify(string) |
| 84 |
:no-index: |
| 85 |
|
| 86 |
Convert a string into something suitable for use as part of a URL, |
| 87 |
e.g. "This is a url" becomes "this-is-a-url" |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
from flask_peewee.utils import slugify |
| 92 |
|
| 93 |
|
| 94 |
class Blog(db.Model): |
| 95 |
title = CharField() |
| 96 |
slug = CharField() |
| 97 |
|
| 98 |
def save(self, *args, **kwargs): |
| 99 |
self.slug = slugify(self.title) |
| 100 |
super(Blog, self).save(*args, **kwargs) |
| 101 |
|
| 102 |
.. py:function:: make_password(raw_password) |
| 103 |
|
| 104 |
Create a salted hash for the given plain-text password |
| 105 |
|
| 106 |
.. py:function:: check_password(raw_password, enc_password) |
| 107 |
|
| 108 |
Compare a plain-text password against a salted/hashed password |
| 109 |
|
| 110 |
.. py:data:: PASSWORD_HASH_METHOD |
| 111 |
|
| 112 |
The hashing method :py:func:`make_password` hands to werkzeug's |
| 113 |
``generate_password_hash`` -- ``'scrypt'`` by default. Override it before |
| 114 |
hashing to change the algorithm or work factor, for instance to pick a |
| 115 |
cheaper method that speeds up your test suite: |
| 116 |
|
| 117 |
.. code-block:: python |
| 118 |
|
| 119 |
import flask_peewee.utils |
| 120 |
|
| 121 |
flask_peewee.utils.PASSWORD_HASH_METHOD = 'pbkdf2:sha256:1' |
| 122 |
|
| 123 |
See ``werkzeug.security.generate_password_hash`` for the accepted values. |