Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoor committed Jun 9, 2017
1 parent b488b7d commit 55ba0b6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 57 deletions.
29 changes: 15 additions & 14 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
# Change Log
All notable changes to this project will be documented in this file.

## [0.1]
### Added
- charging via StripeCharge model
- StripeToken model
- charge_stripe management command

## [0.1.1]
### Added
- related names to user model "stripe_tokens", "stripe_charges"
- utils.py with get_latest_active_token_for_user
- API to create StripeToken (Customer) out of stripe.js card authentication response. (uses Django-REST-Framework)
### Changes
- renamed StripeToken.content to StripeToken.stripe_js_response

## [0.2]
### Added
- creating subscription plans
Expand All @@ -27,3 +13,18 @@ All notable changes to this project will be documented in this file.
- remaned all X.token to X.customer
- changed API url from /aa-stripe/tokens to /aa-stripe/customers
- moved creating stripe customer (on stripe) into instance method, not serializer.create. This way it can be in example used by cron.
- moved ``get_latest_active_customer_for_user`` into StripeCustomer as classmethod

## [0.1.1]
### Added
- related names to user model "stripe_tokens", "stripe_charges"
- utils.py with get_latest_active_token_for_user
- API to create StripeToken (Customer) out of stripe.js card authentication response. (uses Django-REST-Framework)
### Changes
- renamed StripeToken.content to StripeToken.stripe_js_response

## [0.1]
### Added
- charging via StripeCharge model
- StripeToken model
- charge_stripe management command
33 changes: 32 additions & 1 deletion aa_stripe/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
# -*- coding: utf-8 -*-
from aa_stripe.models import StripeCharge, StripeCustomer, StripeSubscription, StripeSubscriptionPlan, StripeWebhook
from aa_stripe.utils import ReadOnly
from django.contrib import admin


class ReadOnlyBase(object):
extra = 0
extra_fields = []

def get_readonly_fields(self, request, obj=None):
from itertools import chain
field_names = list(set(chain.from_iterable(
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
for field in self.model._meta.get_fields()
# For complete backwards compatibility, you may want to exclude
# GenericForeignKey from the results.
# if not (field.many_to_one and field.related_model is None)
# remove all related fields because it causes admin to break
if not field.one_to_many and not field.one_to_one and not field.auto_created
)))
fields = list(self.extra_fields)
for field in field_names:
if not hasattr(self, "editable_fields") or (field not in self.editable_fields):
fields.append(field)
return fields

def has_add_permission(self, request):
return False

def has_delete_permission(self, *args, **kwargs):
return False


class ReadOnly(ReadOnlyBase, admin.ModelAdmin):
editable_fields = []


class StripeCustomerAdmin(ReadOnly):
list_display = ("id", "user", "created", "is_active")

Expand Down
6 changes: 6 additions & 0 deletions aa_stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def create_at_stripe(self):
self.save()
return self

@classmethod
def get_latest_active_customer_for_user(cls, user):
"""Returns last active stripe customer for user"""
customer = cls.objects.filter(user_id=user.id, is_active=True).last()
return customer

class Meta:
ordering = ["id"]

Expand Down
40 changes: 0 additions & 40 deletions aa_stripe/utils.py

This file was deleted.

3 changes: 1 addition & 2 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import mock
import stripe
from aa_stripe.models import StripeCharge, StripeCustomer
from aa_stripe.utils import get_latest_active_customer_for_user
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase
Expand Down Expand Up @@ -30,7 +29,7 @@ def test_charges(self, charge_create_mocked):
user=self.user, stripe_customer_id=data["customer_id"], stripe_js_response="foo")
customer = StripeCustomer.objects.create(
user=self.user, stripe_customer_id=data["customer_id"], stripe_js_response="foo")
self.assertTrue(customer, get_latest_active_customer_for_user(self.user))
self.assertTrue(customer, StripeCustomer.get_latest_active_customer_for_user(self.user))

charge = StripeCharge.objects.create(user=self.user, amount=data["amount"], customer=customer,
description=data["description"])
Expand Down

0 comments on commit 55ba0b6

Please sign in to comment.