File: utils.rst

package info (click to toggle)
flask-peewee 0.6.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,308 kB
  • sloc: python: 3,826; makefile: 127
file content (98 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
.. _utils:

Utilities
=========

flask-peewee ships with several useful utilities.  If you're coming from the
django world, some of these functions may look familiar to you.


Getting objects
---------------

:py:func:`get_object_or_404`

    Provides a handy way of getting an object or 404ing if not found, useful
    for urls that match based on ID.

    .. code-block:: python
    
        @app.route('/blog/<title>/')
        def blog_detail(title):
            blog = get_object_or_404(Blog.select().where(Blog.active==True), Blog.title==title)
            return render_template('blog/detail.html', blog=blog)

:py:func:`object_list`

    Wraps the given query and handles pagination automatically. Pagination defaults to ``20``
    but can be changed by passing in ``paginate_by=XX``.

    .. code-block:: python
    
        @app.route('/blog/')
        def blog_list():
            active = Blog.select().where(Blog.active==True)
            return object_list('blog/index.html', active)
    
    .. code-block:: html
    
        <!-- template -->
        {% for blog in object_list %}
          {# render the blog here #}
        {% endfor %}
        
        {% if page > 1 %}
          <a href="./?page={{ page - 1 }}">Prev</a>
        {% endif %}
        {% if page < pagination.get_pages() %}
          <a href="./?page={{ page + 1 }}">Next</a>
        {% endif %}

:py:class:`PaginatedQuery`

    A wrapper around a query (or model class) that handles pagination.

    Example:

    .. code-block:: python
    
        query = Blog.select().where(Blog.active==True)
        pq = PaginatedQuery(query)
        
        # assume url was /?page=3
        obj_list = pq.get_list()  # returns 3rd page of results
        
        pq.get_page() # returns "3"
        
        pq.get_pages() # returns total objects / objects-per-page


Misc
----


.. py:function:: slugify(string)

    Convert a string into something suitable for use as part of a URL,
    e.g. "This is a url" becomes "this-is-a-url"

    .. code-block:: python
    
        from flask_peewee.utils import slugify
        
        
        class Blog(db.Model):
            title = CharField()
            slug = CharField()
            
            def save(self, *args, **kwargs):
                self.slug = slugify(self.title)
                super(Blog, self).save(*args, **kwargs)

.. py:function:: make_password(raw_password)

    Create a salted hash for the given plain-text password

.. py:function:: check_password(raw_password, enc_password)

    Compare a plain-text password against a salted/hashed password