Skip to content

Commit

Permalink
Merge pull request #26 from hivesolutions/gcandal_orderowner
Browse files Browse the repository at this point in the history
Merge ripe-tech/budy
  • Loading branch information
joamag committed Sep 28, 2016
2 parents fca9cfd + ac17555 commit c3d41af
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/budy/controllers/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from . import media
from . import order
from . import product
from . import referral
from . import root
from . import subscription
from . import voucher
Expand All @@ -66,6 +67,7 @@
from .media import MediaApiController
from .order import OrderApiController
from .product import ProductApiController
from .referral import ReferralApiController
from .root import RootApiController
from .subscription import SubscriptionApiController
from .voucher import VoucherApiController
18 changes: 16 additions & 2 deletions src/budy/controllers/api/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def complex_csv(self):
"size",
"quantity",
"total",
"taxes",
"currency",
"first_name",
"last_name",
Expand All @@ -97,14 +98,16 @@ def complex_csv(self):
"shipping_state",
"shipping_postal_code",
"shipping_country",
"shipping_phone"
"shipping_phone",
"shipping_cost"
)]
for order in orders:
for line in order.lines:
if not line.product: continue
account = order.account
shipping_address = order.shipping_address
billing_address = order.billing_address
shipping_cost = order.shipping_cost
order_s = (
order.id,
order.reference,
Expand All @@ -118,6 +121,7 @@ def complex_csv(self):
line.size,
line.quantity,
line.total,
line.taxes,
line.currency,
billing_address.first_name,
billing_address.last_name,
Expand All @@ -132,7 +136,8 @@ def complex_csv(self):
shipping_address and shipping_address.state,
shipping_address and shipping_address.postal_code,
shipping_address and shipping_address.country,
shipping_address and shipping_address.phone_number
shipping_address and shipping_address.phone_number,
shipping_cost
)
orders_s.append(order_s)
result = appier.serialize_csv(orders_s, delimiter = ",")
Expand Down Expand Up @@ -362,6 +367,15 @@ def set_meta(self, key):
order = order.reload(map = True)
return order

@appier.route("/api/orders/<str:key>/account", "PUT", json = True)
@appier.ensure(token = "user")
def set_account(self, key):
order = budy.Order.get(key = key, rules = False)
account = budy.BudyAccount.from_session()
order.set_account_s(account)
order = order.reload(map = True)
return order

@appier.route("/api/orders/<str:key>/wait_payment", "PUT", json = True)
@appier.ensure(token = "user")
def wait_payment(self, key):
Expand Down
52 changes: 52 additions & 0 deletions src/budy/controllers/api/referral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

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

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

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2016 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import appier

import budy

from . import root

class ReferralApiController(root.RootApiController):

@appier.route("/api/referrals", "GET", json = True)
def list(self):
object = appier.get_object(alias = True, find = True)
referrals = budy.Referral.find(map = True, **object)
return referrals
11 changes: 10 additions & 1 deletion src/budy/models/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,21 @@ def build_discount(self):
return discount

def build_taxes(self):
return self.__class__.eval_taxes(
taxes = 0.0
join_taxes = appier.conf("BUDY_JOIN_TAXES", True, cast = bool)
taxes_dynamic = self.__class__.eval_taxes(
self.sub_total,
self.taxes,
self.quantity,
self
)
taxes_lines = sum(line.total_taxes for line in self.lines)
if join_taxes:
taxes += taxes_dynamic
taxes += taxes_lines
else:
taxes += max(taxes_dynamic, taxes_lines)
return taxes

def build_shipping(self):
return self.__class__.eval_shipping(
Expand Down
25 changes: 25 additions & 0 deletions src/budy/models/bundle_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class BundleLine(base.BudyBase):
type = commons.Decimal
)

taxes = appier.field(
type = commons.Decimal
)

currency = appier.field()

country = appier.field()
Expand All @@ -63,6 +67,11 @@ class BundleLine(base.BudyBase):
initial = commons.Decimal(0.0)
)

total_taxes = appier.field(
type = commons.Decimal,
initial = commons.Decimal(0.0)
)

size = appier.field(
type = int
)
Expand Down Expand Up @@ -98,6 +107,11 @@ def pre_save(self):
def calculate(self, currency = None, country = None, force = False):
currency = currency or self.currency
country = country or self.country
self.total_taxes = self.quantity * self.get_taxes(
currency = currency,
country = country,
force = force
)
self.total = self.quantity * self.get_price(
currency = currency,
country = country,
Expand All @@ -120,10 +134,20 @@ def get_price(self, currency = None, country = None, force = False):
country = country,
attributes = self.attributes
)
self.taxes = self.merchandise.get_taxes(
currency = currency,
country = country,
attributes = self.attributes
)
self.currency = self.merchandise.get_currency(currency = currency)
self.country = country
self.get_taxes(currency = currency, country = country)
return self.price

def get_taxes(self, currency = None, country = None, force = False):
self.get_price(currency = currency, country = country, force = force)
return self.taxes

def get_size(self, currency = None, country = None, force = False):
if not self.product: return None, None
return self.product.get_size(
Expand Down Expand Up @@ -152,6 +176,7 @@ def is_dirty(self, currency = None, country = None):
is_dirty = not self.currency == currency
is_dirty |= not self.country == country
is_dirty |= not hasattr(self, "price") or self.price == None
is_dirty |= not hasattr(self, "taxes") or self.taxes == None
return is_dirty

def is_valid(self):
Expand Down
8 changes: 8 additions & 0 deletions src/budy/models/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ def get_price(
):
return self.price

def get_taxes(
self,
currency = None,
country = None,
attributes = None
):
return self.taxes

def get_currency(self, currency = None):
return currency

Expand Down
11 changes: 11 additions & 0 deletions src/budy/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ def build_discount(self):
return base_discount

def set_account_s(self, account):
self.verify_base()
self.verify_open()
self.account = account
self.store = self.account.store
self.save()
Expand Down Expand Up @@ -508,6 +510,15 @@ def verify_base(self):

appier.verify(len(self.lines) > 0)

def verify_open(self):
"""
Verifies that the current order is considered open,
meaning that the it is still under the checkout stage.
"""

appier.verify(self.status == "created")
appier.verify(self.paid == False)

def verify_shippable(self):
appier.verify(not self.shipping_address == None)
appier.verify(not self.billing_address == None)
Expand Down
49 changes: 47 additions & 2 deletions src/budy/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,59 @@ def get_price(
attributes = attributes
)

def get_taxes(
self,
currency = None,
country = None,
attributes = None
):
if not self.price_provider: return self.taxes
method = getattr(self, "get_taxes_%s" % self.price_provider, None)
if not method: return self.taxes
return method(
currency = currency,
country = country,
attributes = attributes
)

def get_price_ripe(
self,
currency = None,
country = None,
attributes = None
):
if not self.price_url: return self.price

result = self.get_availability_ripe(
currency = currency,
country = country,
attributes = attributes
)
total = result["total"]
return total["price_final"]

def get_taxes_ripe(
self,
currency = None,
country = None,
attributes = None
):
if not self.price_url: return self.price

result = self.get_availability_ripe(
currency = currency,
country = country,
attributes = attributes
)
total = result["total"]
return total["ddp"] + total["vat"]

def get_availability_ripe(
self,
currency = None,
country = None,
attributes = None
):
attributes_m = json.loads(attributes)
p = []
parts = attributes_m.get("parts", {})
Expand All @@ -581,8 +627,7 @@ def get_price_ripe(
self.price_url,
params = params
)
total = result["total"]
return total["price_final"]
return result

def get_currency(self, currency = None):
if not self.price_provider: return self.currency or currency
Expand Down
20 changes: 20 additions & 0 deletions src/budy/models/referral.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,23 @@ def list_names(cls):
@classmethod
def order_name(self):
return ["id", -1]

@classmethod
@appier.operation(
name = "Import CSV",
parameters = (
("CSV File", "file", "file"),
("Empty source", "empty", bool, True)
)
)
def import_csv_s(cls, file, empty):
def callback(line):
store, name = line
composed_name = "%s|%s" % (name, store)
referral = cls(
name = composed_name
)
referral.save()

if empty: cls.delete_c()
cls._csv_import(file, callback)

0 comments on commit c3d41af

Please sign in to comment.