~homeworkprod/byceps

ref: 43bf3630475839afde6e691f0ac561dcb01aaba6 byceps/bootstrap/validators.py -rw-r--r-- 1.3 KiB
43bf3630 — Jochen Kupperschmidt Upgrade pytest on Travis CI to get v3.3 rather than v3.0 provided by default 4 years ago
                                                                                
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
"""
bootstrap.validators
~~~~~~~~~~~~~~~~~~~~

Validators for use with Click_.

.. _Click: http://click.pocoo.org/

:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

import click

from byceps.services.brand import service as brand_service
from byceps.services.party import service as party_service
from byceps.services.user import service as user_service


def validate_brand(ctx, param, brand_id):
    brand = brand_service.find_brand(brand_id)

    if not brand:
        raise click.BadParameter('Unknown brand ID "{}".'.format(brand_id))

    return brand


def validate_party(ctx, param, party_id):
    party = party_service.find_party(party_id)

    if not party:
        raise click.BadParameter('Unknown party ID "{}".'.format(party_id))

    return party


def validate_user_id(ctx, param, user_id):
    user = user_service.find_user(user_id)

    if not user:
        raise click.BadParameter('Unknown user ID "{}".'.format(user_id))

    return user


def validate_user_screen_name(ctx, param, screen_name):
    user = user_service.find_user_by_screen_name(screen_name)

    if not user:
        raise click.BadParameter('Unknown user screen name "{}".'
                                 .format(screen_name))

    return user