Skip to content

Commit

Permalink
Refactory data model
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpenlin committed Jan 7, 2014
1 parent c313609 commit 3df4614
Show file tree
Hide file tree
Showing 22 changed files with 665 additions and 943 deletions.
16 changes: 0 additions & 16 deletions billy/api/subscription/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,3 @@ class SubscriptionCreateForm(Form):
validators.Optional(),
NoPastValidator(),
])


class SubscriptionCancelForm(Form):
prorated_refund = BooleanField('Prorated refund', [
validators.Optional(),
], default=False)
refund_amount = IntegerField('Refund amount', [
validators.Optional(),
RefundAmountConflict(),
validators.NumberRange(min=MINIMUM_AMOUNT),
])
appears_on_statement_as = TextField('Appears on statement as', [
validators.Optional(),
validators.Regexp(STATEMENT_REXP),
validators.Length(max=18),
])
41 changes: 3 additions & 38 deletions billy/api/subscription/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import transaction as db_transaction
from pyramid.view import view_config
from pyramid.settings import asbool
from pyramid.security import authenticated_userid
from pyramid.httpexceptions import HTTPForbidden
from pyramid.httpexceptions import HTTPBadRequest

from billy.models.subscription import SubscriptionModel
from billy.models.transaction import TransactionModel
from billy.api.utils import validate_form
from billy.api.utils import form_errors_to_bad_request
from billy.api.utils import list_by_context
from billy.api.resources import IndexResource
from billy.api.resources import EntityResource
from billy.api.views import IndexView
from billy.api.views import EntityView
from billy.api.views import api_view_defaults
from .forms import SubscriptionCreateForm
from .forms import SubscriptionCancelForm


class SubscriptionResource(EntityResource):
Expand Down Expand Up @@ -86,11 +83,11 @@ def post(self):
appears_on_statement_as=appears_on_statement_as,
started_at=started_at,
)
transactions = sub_model.yield_transactions([subscription])
invoices = subscription.invoices
# this is not a deferred subscription, just process transactions right away
if started_at is None:
with db_transaction.manager:
tx_model.process_transactions(transactions)
tx_model.process_transactions(invoices[0].transactions)

return subscription

Expand All @@ -104,47 +101,15 @@ def get(self):

@view_config(name='cancel', request_method='POST')
def cancel(self):
# TODO: it appears a DELETE request with body is not a good idea
# for HTTP protocol as many server doesn't support this, this is why
# we use another view with post method, maybe we should use a better
# approach later
request = self.request
subscription = self.context.entity
form = validate_form(SubscriptionCancelForm, request)

prorated_refund = asbool(form.data.get('prorated_refund', False))
refund_amount = form.data.get('refund_amount')
appears_on_statement_as = form.data.get('appears_on_statement_as')
if not appears_on_statement_as:
appears_on_statement_as = None

sub_model = request.model_factory.create_subscription_model()
tx_model = request.model_factory.create_transaction_model()

# TODO: maybe we can find a better way to integrate this with the
# form validation?
if refund_amount is not None:
amount = subscription.effective_amount
if refund_amount > amount:
return form_errors_to_bad_request(dict(
refund_amount=['refund_amount cannot be greater than '
'subscription amount {}'.format(amount)]
))

if subscription.canceled:
return HTTPBadRequest('Cannot cancel a canceled subscription')

with db_transaction.manager:
transaction = sub_model.cancel(
subscription,
prorated_refund=prorated_refund,
refund_amount=refund_amount,
appears_on_statement_as=appears_on_statement_as,
)
if transaction is not None:
with db_transaction.manager:
tx_model.process_transactions([transaction])

sub_model.cancel(subscription)
return subscription

@view_config(name='transactions')
Expand Down
5 changes: 3 additions & 2 deletions billy/api/transaction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pyramid.view import view_config
from pyramid.security import authenticated_userid

from billy.models.invoice import InvoiceModel
from billy.models.transaction import TransactionModel
from billy.api.utils import list_by_context
from billy.api.resources import IndexResource
Expand All @@ -16,8 +17,8 @@ class TransactionResource(EntityResource):
@property
def company(self):
# make sure only the owner company can access the customer
if self.entity.transaction_cls == TransactionModel.CLS_SUBSCRIPTION:
company = self.entity.subscription.plan.company
if self.entity.invoice.invoice_type == InvoiceModel.TYPE_SUBSCRIPTION:
company = self.entity.invoice.subscription.plan.company
else:
company = self.entity.invoice.customer.company
return company
Expand Down
Loading

0 comments on commit 3df4614

Please sign in to comment.