Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add several list filtering improvements #186

Merged
merged 5 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions localstripe/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _api_delete(cls, id):
if key not in store.keys():
raise UserError(404, 'Not Found')
del store[key]
return {"deleted": True, "id": id}
return {'deleted': True, 'id': id}

@classmethod
def _api_list_all(cls, url, limit=None, starting_after=None, **kwargs):
Expand Down Expand Up @@ -944,6 +944,25 @@ def _api_list_subscriptions(cls, id, **kwargs):

return cls._api_retrieve(id).subscriptions

@classmethod
def _api_list_sources(cls, id, object=None, **kwargs):
if kwargs:
raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))
thrau marked this conversation as resolved.
Show resolved Hide resolved

try:
if object is not None:
assert type(object) is str
assert object in ['card', 'bank_account']
Comment on lines +953 to +955
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing it in the end 👍

except AssertionError:
raise UserError(400, 'Bad request')

li = cls._api_retrieve(id).sources

if object is not None:
li._list = [i for i in li._list if i.object == object]

return li

@classmethod
def _api_add_subscription(cls, id, **data):
return Subscription._api_create(customer=id, **data)
Expand Down Expand Up @@ -973,6 +992,7 @@ def _api_update_subscription(cls, id, subscription_id, **data):


extra_apis.extend((
('GET', '/v1/customers/{id}/sources', Customer._api_list_sources),
('POST', '/v1/customers/{id}/sources', Customer._api_add_source),
# Retrieve single source by id:
('GET', '/v1/customers/{id}/sources/{source_id}',
Expand Down Expand Up @@ -2204,6 +2224,32 @@ def name(self): # Support Stripe API <= 2018-02-05
def statement_descriptor(self): # Support Stripe API <= 2018-02-05
return Product._api_retrieve(self.product).statement_descriptor

@classmethod
def _api_list_all(cls, url, active=None, product=None, limit=None,
starting_after=None, **kwargs):
if kwargs:
raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))

active = try_convert_to_bool(active)
try:
if active is not None:
assert type(active) is bool
if product is not None:
assert type(product) is str
except AssertionError:
raise UserError(400, 'Bad request')

li = super(Plan, cls)._api_list_all(
url, limit=limit, starting_after=starting_after
)

if active is not None:
li._list = [obj for obj in li._list if obj.active == active]
if product is not None:
li._list = [obj for obj in li._list if obj.product == product]

return li


class Payout(StripeObject):
object = 'payout'
Expand Down Expand Up @@ -2254,7 +2300,7 @@ def __init__(self, amount=None, currency=None, description=None,
# manually created
self.automatic = False
# Balance Transactions are no implemented yet so we fake one
self.balance_transaction = f"txn_{random_id(24)}"
self.balance_transaction = f'txn_{random_id(24)}'

self.failure_balance_transaction = None
self.failure_code = None
Expand Down Expand Up @@ -2350,6 +2396,28 @@ def __init__(self, id=None, name=None, type='service', active=True,

schedule_webhook(Event('product.created', self))

@classmethod
def _api_list_all(cls, url, active=None, limit=None, starting_after=None,
**kwargs):
if kwargs:
raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))

active = try_convert_to_bool(active)
try:
if active is not None:
assert type(active) is bool
except AssertionError:
raise UserError(400, 'Bad request')

li = super(Product, cls)._api_list_all(
url, limit=limit, starting_after=starting_after
)

if active is not None:
li._list = [obj for obj in li._list if obj.active == active]

return li

thrau marked this conversation as resolved.
Show resolved Hide resolved

class Refund(StripeObject):
object = 'refund'
Expand Down Expand Up @@ -2593,6 +2661,7 @@ class Subscription(StripeObject):

def __init__(self, customer=None, metadata=None, items=None,
trial_end=None, default_tax_rates=None,
trial_from_plan=False,
backdate_start_date=None,
plan=None, quantity=None, # legacy support
tax_percent=None, # deprecated
Expand All @@ -2609,6 +2678,7 @@ def __init__(self, customer=None, metadata=None, items=None,
items = [{'plan': plan, 'quantity': quantity}]

trial_end = try_convert_to_int(trial_end)
trial_from_plan = try_convert_to_bool(trial_from_plan)
thrau marked this conversation as resolved.
Show resolved Hide resolved
tax_percent = try_convert_to_float(tax_percent)
enable_incomplete_payments = try_convert_to_bool(
enable_incomplete_payments)
Expand All @@ -2623,6 +2693,9 @@ def __init__(self, customer=None, metadata=None, items=None,
trial_end = int(time.time())
assert type(trial_end) is int
assert trial_end > 1500000000
assert not trial_from_plan
if trial_from_plan is not None:
assert type(trial_from_plan) is bool
if tax_percent is not None:
assert default_tax_rates is None
assert type(tax_percent) is float
Expand Down Expand Up @@ -2695,6 +2768,7 @@ def __init__(self, customer=None, metadata=None, items=None,
self.trial_end = trial_end
self.trial_start = None
self.trial_period_days = trial_period_days
self.trial_from_plan = trial_from_plan
self.latest_invoice = None
self.start_date = backdate_start_date or int(time.time())
self.billing_cycle_anchor = billing_cycle_anchor
Expand Down
26 changes: 26 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,26 @@ curl -sSfg -u $SK: $HOST/v1/products/PRODUCT1234

curl -sSfg -u $SK: $HOST/v1/plans?expand[]=data.product

count=$(curl -sSfg -u $SK: $HOST/v1/products?active=false \
| grep -oP 'total_count": \K([0-9]+)')
[ "$count" -eq 0 ]

count=$(curl -sSfg -u $SK: $HOST/v1/products?active=true \
| grep -oP 'total_count": \K([0-9]+)')
[ "$count" -eq 9 ]

code=$(curl -sg -o /dev/null -w '%{http_code}' -u $SK: \
$HOST/v1/plans?expand[]=data.doesnotexist)
[ "$code" -eq 400 ]

count=$(curl -sSfg -u $SK: $HOST/v1/plans?active=false \
| grep -oP 'total_count": \K([0-9]+)')
[ "$count" -eq 0 ]

count=$(curl -sSfg -u $SK: $HOST/v1/plans?active=true \
| grep -oP 'total_count": \K([0-9]+)')
[ "$count" -eq 6 ]

curl -sSfg -u $SK: $HOST/v1/coupons \
-d id=PARRAIN \
-d percent_off=30 \
Expand Down Expand Up @@ -197,6 +213,11 @@ res=$(
| grep -oE $card)
[ -n "$res" ]

# make sure cards exist in customer sources
count=$(curl -sSfg -u $SK: $HOST/v1/customers/$cus/sources?object=card \
| grep -oP 'total_count": \K([0-9]+)')
[ "$count" -eq 3 ]

# delete the card
curl -sSfg -u $SK: $HOST/v1/customers/$cus/sources/$card \
-X DELETE
Expand All @@ -207,6 +228,11 @@ res=$(
| grep -oE $card || true)
[ -z "$res" ]

# make sure cards is removed from customer sources
count=$(curl -sSfg -u $SK: $HOST/v1/customers/$cus/sources?object=card \
| grep -oP 'total_count": \K([0-9]+)')
[ "$count" -eq 2 ]

# add a new card
card=$(
curl -sSfg -u $SK: $HOST/v1/customers/$cus/cards \
Expand Down