~sirn/fanboi2

ref: a68873a108d20a6a49a1b6caf374bfb2cf6a94fe fanboi2/fanboi2/services/board.py -rw-r--r-- 2.5 KiB
a68873a1Kridsada Thanabulpong Coding style cleanups and setup pre-commit hooks (#42) 3 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
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
from ..models import Board


class BoardCreateService(object):
    """Board create service provides a service for creating board."""

    def __init__(self, dbsession):
        self.dbsession = dbsession

    def create(self, slug, title, description, status, agreements, settings):
        """Create a new board.

        :param slug: An identifier of the board. Also used in URL.
        :param title: Name of the board.
        :param description: Descripton explaining what the board is about.
        :param status: Status of the board.
        :param agreements: Term of use for the board.
        :param settings: Settings for the board.
        """
        board = Board(
            slug=slug,
            title=title,
            description=description,
            status=status,
            agreements=agreements,
            settings=settings,
        )

        self.dbsession.add(board)
        return board


class BoardQueryService(object):
    """Board query service provides a service for querying a board
    or a collection of boards from the database.
    """

    def __init__(self, dbsession):
        self.dbsession = dbsession

    def list_all(self):
        """Query all boards."""
        return list(self.dbsession.query(Board).order_by(Board.title).all())

    def list_active(self):
        """Query all boards that are not archived."""
        return list(
            self.dbsession.query(Board)
            .order_by(Board.title)
            .filter(Board.status != "archived")
            .all()
        )

    def board_from_slug(self, board_slug):
        """Query a board from the given board slug.

        :param board_slug: The slug :type:`str` identifying a board.
        """
        return self.dbsession.query(Board).filter_by(slug=board_slug).one()


class BoardUpdateService(object):
    """Board update service provides a service for updating board."""

    def __init__(self, dbsession):
        self.dbsession = dbsession

    def update(self, slug, **kwargs):
        """Update the given board slug with the given :param:`kwargs`.
        This method will raise ``NoResultFound`` if the given slug does not
        already exists. Slug cannot be updated.

        :param slug: The board identifier.
        :param **kwargs: Attributes to update.
        """
        board = self.dbsession.query(Board).filter_by(slug=slug).one()

        for key in ("title", "description", "status", "agreements", "settings"):
            if key in kwargs:
                setattr(board, key, kwargs[key])

        self.dbsession.add(board)
        return board