Skip to content

Services

The pagination_links function generates links for the pagination of a resource. It takes in a Pagination object and returns a dictionary with the keys "self", "first", "prev", "next" and "last". The values are links to the current page, the first page, the previous page if one exists, next page if it exists and last page respectively. If there is no previous or next page then None is returned.

Parameters:

Name Type Description Default
pagination

Determine the number of items to show per page

required
resource

Determine the resource that is being paginated

required
**args

Allow the pagination_links function to accept any number of other parameters

{}

Returns:

Type Description

A dictionary of links that are used to navigate through a list of items

Source code in services/pagination_service.py
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
def pagination_links(pagination, resource, **args):
    """
    The pagination_links function generates links for the pagination of a resource.
    It takes in a Pagination object and returns a dictionary with the keys "self",
    "first", "prev", "next" and "last". The values are links to the current page,
    the first page, the previous page if one exists, next page if it exists and last
    page respectively. If there is no previous or next page then None is returned.

    Args:
        pagination: Determine the number of items to show per page
        resource: Determine the resource that is being paginated
        **args: Allow the pagination_links function to accept any number of other parameters

    Returns:
        A dictionary of links that are used to navigate through a list of items

    """
    nav_links = {}
    per_page = pagination.per_page
    this_page = pagination.page
    last_page = pagination.pages
    nav_links["self"] = url_for(resource, **args, page=this_page, per_page=per_page)
    nav_links["first"] = url_for(resource, page=1, per_page=per_page)
    if pagination.has_prev:
        nav_links["prev"] = url_for(
            resource, **args, page=this_page - 1, per_page=per_page
        )

    if pagination.has_next:
        nav_links["next"] = url_for(
            resource, **args, page=this_page + 1, per_page=per_page
        )

    nav_links["last"] = url_for(resource, **args, page=last_page, per_page=per_page)

    return nav_links

pagination_meta(pagination)

The pagination_meta function takes a pagination object and returns a dictionary with the meta information about the pagination. The meta information includes: has_prev, has_next, page, total_pages, items_per_page and total.

Parameters:

Name Type Description Default
pagination

Get the pagination object from the

required

Returns:

Type Description

A dictionary containing the following data:

Source code in services/pagination_service.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def pagination_meta(pagination):
    """
    The pagination_meta function takes a pagination object and returns a dictionary with the meta information about
    the pagination. The meta information includes: has_prev, has_next, page, total_pages, items_per_page and total.


    Args:
        pagination: Get the pagination object from the

    Returns:
        A dictionary containing the following data:
    """
    meta = {
        "has_prev": pagination.has_prev,
        "has_next": pagination.has_next,
        "page": pagination.page,
        "total_pages": pagination.pages,
        "items_per_page": pagination.per_page,
        "total_items": pagination.total,
    }
    return meta

send_async_email(app, msg)

The send_async_email function is a helper function that sends an email asynchronously. It takes in the application instance and the message to be sent, then uses the flask_mail library to send it.

Parameters:

Name Type Description Default
app

Access the application instance for the

required
msg

Pass in the message that we want to send to our mail server

required

Returns:

Type Description

None

Source code in services/mail_service.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def send_async_email(app, msg):
    """
    The send_async_email function is a helper function that sends an email asynchronously.
    It takes in the application instance and the message to be sent, then uses the flask_mail
    library to send it.

    Args:
        app: Access the application instance for the
        msg: Pass in the message that we want to send to our mail server

    Returns:
        None

    """
    with app.app_context():
        try:
            mail.send(msg)
        except ConnectionRefusedError:
            raise InternalServerError("[MAIL SERVER] not working")

send_email(subject, sender, recipients, text_body, html_body)

The send_email function sends an email to the specified recipients. It takes in a subject, sender, recipients (a list of strings), text_body and html_body.

Parameters:

Name Type Description Default
subject

Set the subject of the email

required
sender

Specify the sender of the email

required
recipients

Specify the recipients of the email

required
text_body

Set the text body of the email, and html_body is used to set the html content of the email

required
html_body

Send html emails

required

Returns:

Type Description

None

Source code in services/mail_service.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def send_email(subject, sender, recipients, text_body, html_body):
    """
    The send_email function sends an email to the specified recipients.
    It takes in a subject, sender, recipients (a list of strings), text_body and html_body.

    Args:
        subject: Set the subject of the email
        sender: Specify the sender of the email
        recipients: Specify the recipients of the email
        text_body: Set the text body of the email, and html_body is used to set the html content of the email
        html_body: Send html emails

    Returns:
        None
    """
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)
    Thread(target=send_async_email, args=(app, msg)).start()