Skip to content

Commit

Permalink
Account view and more user friendly action views
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed Aug 7, 2013
1 parent cf4c216 commit 1be83c1
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 46 deletions.
5 changes: 4 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ History
0.1.3 (2013-08-7)
++++++++++++++++++

* Added account view
* Added Customer.get_or_create method
* sync file for all code that keeps things in sync with stripe
* Added djstripe_sync_customers management command
* sync file for all code that keeps things in sync with stripe
* Use client-side JavaScript to get history data asynchronously
* More user friendly action views

0.1.2 (2013-08-6)
++++++++++++++++++
Expand Down
6 changes: 6 additions & 0 deletions djstripe/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def load_path_attr(path):
"PAYMENTS_TRIAL_PERIOD_FOR_USER_CALLBACK",
None
)
PLAN_LIST = []
for p in settings.PAYMENTS_PLANS:
if settings.PAYMENTS_PLANS[p].get("stripe_plan_id"):
plan = settings.PAYMENTS_PLANS[p]
PLAN_LIST.append(plan)

if PY3:
if isinstance(TRIAL_PERIOD_FOR_USER_CALLBACK, str):
TRIAL_PERIOD_FOR_USER_CALLBACK = load_path_attr(
Expand Down
Binary file added djstripe/static/img/in-progress.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions djstripe/templates/djstripe/account.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% extends "payments/base.html" %}

{% block title %}Account{% endblock title %}

{% block content %}
{{ block.super }}
<div class="container">
<div class="row">
<div class="col-12">
<h2>Account Information</h2>
{% include "djstripe/includes/_subscription_status.html" %}
</div>
</div>
<div class="row">
<div class="col-6">
<p>
Credit Card on file.
<a class="btn btn-info" href="">Show card</a>
</p>
</div>
<div class="col-6">
<p class="pull-right">
Next billed on {{ subscription.current_period_end }}
<a class="btn btn-primary" href="{% url 'djstripe:subscribe' %}">Update credit card</a>
</p>
</div>
</div>
<div class="row">
<div class="col-12">
<table class="table">
<thead>
<tr>
<th>Plan</th>
<th>Interval</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody>

{% for plan in plans %}
<tr>
<td>{{ plan.name }}</td>
<td>{{ plan.interval.title }}ly</td>
<td>{{ plan.description }}</td>
<td>Your plan</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>


{% endblock content %}

{% block javascript %}
{{ block.super }}
<script>
$(function() {
$.post("{% url 'djstripe:sync_history' %}", function(data) {
$('#history-table').html(data);
});
});
</script>
{% endblock javascript %}
52 changes: 16 additions & 36 deletions djstripe/templates/djstripe/base.html
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
{% extends "base.html" %}
{% load static %}

{% block content %}
{% with customer.current_subscription as subscription %}
{% if subscription %}
{% if subscription.status == "active" %}
<div class="alert alert-info">
Your subscription will automatically renew in <strong>{{ subscription.current_period_end|timeuntil }}</strong>.
<!-- Modal -->
<div class="modal fade" id="in-progress">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Server Update In-Progress <img src="{% static 'img/in-progress.gif' %}" /></h4>
</div>
<div class="modal-body">
<div class="progress">
<div class="progress-bar" style="width: 0%;"></div>
</div>
{% else %}
{% if subscription.status == "trialing" %}
{% if request.user.customer.plan and request.user.customer.card_kind %}
<div class="alert alert-info">
Your free trial will end in <strong>{{ subscription.current_period_end|timeuntil }}</strong> after which you commence a <strong>{{ subscription.plan_display }}</strong> plan.
</div>
{% else %}
<div class="alert alert-warning lead">
Your free trial will end in <strong>{{ subscription.current_period_end|timeuntil }}</strong> after which you will need to get a subscription to continue using the site.
</div>
{% endif %}
{% else %}
{% if subscription.status == "canceled" %}
{% if subscription.is_period_current %}
<div class="alert alert-warning lead">
Your subscription has been <strong>canceled</strong> but you can continue to use the site for another <strong>{{ subscription.current_period_end|timeuntil }}</strong>.
</div>
{% else %}
<div class="alert alert-danger lead">
Your subscription has been <strong>canceled</strong>.
</div>
{% endif %}
{% else %}
<div class="alert alert-danger lead">
Your subscription is <strong>{{ subscription.status }}</strong>.
</div>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endwith %}
</div>

</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
{% endblock %}
31 changes: 31 additions & 0 deletions djstripe/templates/djstripe/includes/_subscription_status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% if subscription %}
{% if subscription.status != "active" %}
{% if subscription.status == "trialing" %}
{% if request.user.customer.plan and request.user.customer.card_kind %}
<div class="alert alert-info">
Your free trial will end in <strong>{{ subscription.current_period_end|timeuntil }}</strong> after which you commence a <strong>{{ subscription.plan_display }}</strong> plan.
</div>
{% else %}
<div class="alert alert-warning lead">
Your free trial will end in <strong>{{ subscription.current_period_end|timeuntil }}</strong> after which you will need to get a subscription to continue using the site.
</div>
{% endif %}
{% else %}
{% if subscription.status == "canceled" %}
{% if subscription.is_period_current %}
<div class="alert alert-warning lead">
Your subscription has been <strong>canceled</strong> but you can continue to use the site for another <strong>{{ subscription.current_period_end|timeuntil }}</strong>.
</div>
{% else %}
<div class="alert alert-danger lead">
Your subscription has been <strong>canceled</strong>.
</div>
{% endif %}
{% else %}
<div class="alert alert-danger lead">
Your subscription is <strong>{{ subscription.status }}</strong>.
</div>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
11 changes: 6 additions & 5 deletions djstripe/templates/djstripe/subscribe_form.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% extends "payments/base.html" %}
{% extends "djstripe/base.html" %}

{% block title %}Subscription Form{% endblock title %}

{% block content %}
{{ block.super }}
<h2>Subscription Form</h2>

{% if error %}
Expand All @@ -22,6 +23,7 @@ <h2>Subscription Form</h2>
</div>
</form>


{% endblock content %}

{% block javascript %}
Expand All @@ -36,10 +38,9 @@ <h2>Subscription Form</h2>
token = function(res) {
$form.find("input[name=stripe_token]").val(res.id);

// work on this for pure HTML edition. Or maybe JS edition?
//$(".main-content").hide();
//$("div.progress").removeClass("hidden");
//$('.progress-bar').animate({width:'+=100%'}, 2000);
$("button[type=submit]").attr("disabled", "true");
$('#in-progress').modal({"keyboard": false})
$('.progress-bar').animate({width:'+=100%'}, 2000);
$form.trigger("submit");
};

Expand Down
5 changes: 5 additions & 0 deletions djstripe/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@


urlpatterns = patterns("",
url(
r"^$",
views.AccountView.as_view(),
name="account"
),
url(
r"^subscribe/$",
views.SubscribeFormView.as_view(),
Expand Down
19 changes: 15 additions & 4 deletions djstripe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .models import Event
from .models import EventProcessingException
from .settings import PLAN_CHOICES
from .settings import PLAN_LIST
from .settings import PY3
from .sync import sync_customer
from .viewmixins import PaymentsContextMixin
Expand All @@ -37,6 +38,7 @@ class SubscribeFormView(
def get_context_data(self, *args, **kwargs):
context = super(SubscribeFormView, self).get_context_data(**kwargs)
context['is_plans_plural'] = bool(len(PLAN_CHOICES) > 1)
context['customer'], created = Customer.get_or_create(self.request.user)
return context

def post(self, request, *args, **kwargs):
Expand All @@ -48,10 +50,7 @@ def post(self, request, *args, **kwargs):
form = self.get_form(form_class)
if form.is_valid():
try:
try:
customer = self.request.user.customer
except ObjectDoesNotExist:
customer = Customer.create(self.request.user)
customer, created = Customer.get_or_create(self.request.user)
customer.update_card(self.request.POST.get("stripe_token"))
customer.subscribe(form.cleaned_data["plan"])
except stripe.StripeError as e:
Expand Down Expand Up @@ -118,3 +117,15 @@ def post(self, request, *args, **kwargs):
"djstripe/includes/_history_table.html",
{"customer": sync_customer(request.user)}
)


class AccountView(LoginRequiredMixin, SelectRelatedMixin, TemplateView):
template_name = "djstripe/account.html"

def get_context_data(self, *args, **kwargs):
context = super(AccountView, self).get_context_data(**kwargs)
customer, created = Customer.get_or_create(self.request.user)
context['customer'] = customer
context['subscription'] = customer.current_subscription
context['plans'] = PLAN_LIST
return context

0 comments on commit 1be83c1

Please sign in to comment.