• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hivesolutions / budy / 1102 / 10

Source File

30.43
/src/budy/controllers/api/bag.py
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3

4
# Hive Budy
5
# Copyright (c) 2008-2019 Hive Solutions Lda.
6
#
7
# This file is part of Hive Budy.
8
#
9
# Hive Budy is free software: you can redistribute it and/or modify
10
# it under the terms of the Apache License as published by the Apache
11
# Foundation, either version 2.0 of the License, or (at your option) any
12
# later version.
13
#
14
# Hive Budy is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# Apache License for more details.
18
#
19
# You should have received a copy of the Apache License along with
20
# Hive Budy. If not, see <http://www.apache.org/licenses/>.
21

22
__author__ = "João Magalhães <joamag@hive.pt>"
1✔
23
""" The author(s) of the module """
24

25
__version__ = "1.0.0"
1✔
26
""" The version of the module """
27

28
__revision__ = "$LastChangedRevision$"
1✔
29
""" The revision number of the module """
30

31
__date__ = "$LastChangedDate$"
1✔
32
""" The last change date of the module """
33

34
__copyright__ = "Copyright (c) 2008-2019 Hive Solutions Lda."
1✔
35
""" The copyright for the module """
36

37
__license__ = "Apache License, Version 2.0"
1✔
38
""" The license for the module """
39

40
import appier
1✔
41

42
import budy
1✔
43

44
from . import root
1✔
45

46
class BagAPIController(root.RootAPIController):
1✔
47

48
    @appier.route("/api/bags", "GET", json = True)
1✔
49
    @appier.ensure(token = "admin")
1✔
50
    def list(self):
51
        object = appier.get_object(alias = True, find = True)
×
52
        bags = budy.Bag.find(eager_l = True, map = True, **object)
×
53
        return bags
×
54

55
    @appier.route("/api/bags", "POST", json = True)
1✔
56
    def create(self):
57
        bag = budy.Bag.new()
×
58
        bag.save()
×
59
        bag = bag.map()
×
60
        return bag
×
61

62
    @appier.route("/api/bags/key", "GET", json = True)
1✔
63
    def key(self):
64
        bag = budy.Bag.from_session()
×
65
        return dict(key = bag.key)
×
66

67
    @appier.route("/api/bags/<str:key>", "GET", json = True)
1✔
68
    def show(self, key):
69
        ensure = self.field("ensure", True, cast = bool)
×
70
        bag = budy.Bag.get(key = key, raise_e = not ensure)
×
71
        if not bag: bag = budy.Bag.ensure_s(key = key)
×
72
        bag.refresh_s(
×
73
            currency = self.currency,
74
            country = self.country
75
        )
76
        bag = bag.reload(
×
77
            eager = (
78
                "lines.product.images",
79
                "lines.product.brand"
80
            ),
81
            map = True
82
        )
83
        return bag
×
84

85
    @appier.route("/api/bags/<str:key>/merge/<str:target>", "PUT", json = True)
1✔
86
    def merge(self, key, target):
87
        increment = self.field("increment", False, cast = bool)
×
88
        bag = budy.Bag.get(key = key)
×
89
        target = budy.Bag.get(key = target)
×
90
        bag.merge_s(target.id, increment = increment)
×
91
        bag = bag.reload(map = True)
×
92
        return bag
×
93

94
    @appier.route("/api/bags/<str:key>/lines", "POST", json = True)
1✔
95
    def add_line(self, key):
96
        line = budy.BagLine.new()
×
97
        line.ensure_size_s()
×
98
        line.save()
×
99
        bag = budy.Bag.get(key = key)
×
100
        bag.lines.append(line)
×
101
        bag.save()
×
102
        line = line.reload(map = True)
×
103
        return line
×
104

105
    @appier.route("/api/bags/<str:key>/lines/<int:line_id>", "DELETE", json = True)
1✔
106
    def remove_line(self, key, line_id):
107
        bag = budy.Bag.get(key = key)
×
108
        bag.remove_line_s(line_id)
×
109
        bag = bag.reload(map = True)
×
110
        return bag
×
111

112
    @appier.route("/api/bags/<str:key>/lines/add_update", "POST", json = True)
1✔
113
    def add_update_line(self, key):
114
        increment = self.field("increment", True, cast = bool)
×
115
        line = budy.BagLine.new()
×
116
        line.ensure_size_s()
×
117
        bag = budy.Bag.get(key = key)
×
118
        line = bag.add_update_line_s(line, increment = increment)
×
119
        line = line.reload(map = True)
×
120
        return line
×
121

122
    @appier.route("/api/bags/<str:key>/empty", "GET", json = True)
1✔
123
    def empty(self, key):
124
        bag = budy.Bag.get(key = key)
×
125
        bag.empty_s()
×
126
        bag = bag.reload(map = True)
×
127
        return bag
×
128

129
    @appier.route("/api/bags/<str:key>/order", "GET", json = True)
1✔
130
    def order(self, key):
131
        bag = budy.Bag.get(key = key)
×
132
        order = bag.to_order_s()
×
133
        order = order.reload(map = True)
×
134
        return order
×
  • Back to Build 1067
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc