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

make django-oscar-accounts really could work without oscar #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from setuptools import find_packages, setup

install_requires=[
'django-oscar>=1.1.1',
'python-dateutil>=2.4,<3.0',
]

tests_require = [
'django-oscar>=1.1.1',
'django-webtest==1.7.8',
'pytest==2.9.0',
'pytest-cov==2.1.0',
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from django.utils import six, timezone
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from oscar.core.compat import AUTH_USER_MODEL
from treebeard.mp_tree import MP_Node

from oscar_accounts import exceptions
from oscar_accounts.compact_oscar import AUTH_USER_MODEL


class ActiveAccountManager(models.Manager):
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/api/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.conf.urls import patterns, url
from django.views.decorators.csrf import csrf_exempt
from oscar.core.application import Application

from oscar_accounts.api import decorators, views
from oscar_accounts.compact_oscar import Application


class APIApplication(Application):
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.views import generic
from oscar.core.loading import get_model

from oscar_accounts import codes, exceptions, facade, names
from oscar_accounts.api import errors
from oscar_accounts.compact_oscar import get_model

Account = get_model('oscar_accounts', 'Account')
AccountType = get_model('oscar_accounts', 'AccountType')
Expand Down
4 changes: 2 additions & 2 deletions src/oscar_accounts/checkout/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from django import forms
from django.utils.translation import ugettext_lazy as _
from oscar.core.loading import get_model
from oscar.templatetags.currency_filters import currency

from oscar_accounts.compact_oscar import get_model, currency

Account = get_model('oscar_accounts', 'Account')

Expand Down
3 changes: 1 addition & 2 deletions src/oscar_accounts/checkout/gateway.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.utils.translation import ugettext_lazy as _
from oscar.apps.payment.exceptions import UnableToTakePayment
from oscar.core.loading import get_model

from oscar_accounts import codes, core, exceptions, facade
from oscar_accounts.compact_oscar import get_model, UnableToTakePayment

Account = get_model('oscar_accounts', 'Account')
Transfer = get_model('oscar_accounts', 'Transfer')
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/codes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
import string

from oscar.core.loading import get_model
from oscar_accounts.compact_oscar import get_model

Account = get_model('oscar_accounts', 'Account')

Expand Down
95 changes: 95 additions & 0 deletions src/oscar_accounts/compact_oscar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
try:
import oscar.core

except ImportError:
from importlib import import_module
from django.apps import apps
from django.apps.config import MODELS_MODULE_NAME
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, AppRegistryNotReady
from django import forms

# from oscar.core.compat
# A setting that can be used in foreign key declarations
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
try:
AUTH_USER_APP_LABEL, AUTH_USER_MODEL_NAME = AUTH_USER_MODEL.rsplit('.', 1)
except ValueError:
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form"
" 'app_label.model_name'")

# from oscar.core.loading
def get_model(app_label, model_name):
"""
Fetches a Django model using the app registry.
This doesn't require that an app with the given app label exists,
which makes it safe to call when the registry is being populated.
All other methods to access models might raise an exception about the
registry not being ready yet.
Raises LookupError if model isn't found.
"""
try:
return apps.get_model(app_label, model_name)
except AppRegistryNotReady:
if apps.apps_ready and not apps.models_ready:
# If this function is called while `apps.populate()` is
# loading models, ensure that the module that defines the
# target model has been imported and try looking the model up
# in the app registry. This effectively emulates
# `from path.to.app.models import Model` where we use
# `Model = get_model('app', 'Model')` instead.
app_config = apps.get_app_config(app_label)
# `app_config.import_models()` cannot be used here because it
# would interfere with `apps.populate()`.
import_module('%s.%s' % (app_config.name, MODELS_MODULE_NAME))
# In order to account for case-insensitivity of model_name,
# look up the model through a private API of the app registry.
return apps.get_registered_model(app_label, model_name)
else:
# This must be a different case (e.g. the model really doesn't
# exist). We just re-raise the exception.
raise


# from oscar.core.loading
def is_model_registered(app_label, model_name):
"""
Checks whether a given model is registered. This is used to only
register Oscar models if they aren't overridden by a forked app.
"""
try:
apps.get_registered_model(app_label, model_name)
except LookupError:
return False
else:
return True

# from oscar.apps.payment.exceptions
class UnableToTakePayment(Exception):
"""
Exception to be used for ANTICIPATED payment errors (eg card number wrong,
expiry date has passed). The message passed here will be shown to the end
user.
"""

# dummy for oscar.templatetags.currency_filters.currency
def currency(value, currency=None):
return value

# dummy for oscar.forms.widgets.DatePickerInput
DatePickerInput = forms.DateInput

# dummy for oscar.application.Application
class Application(object):
def post_process_urls(self, urlpatterns):
return urlpatterns


else: # oscar is installed, use it.
from oscar.apps.payment.exceptions import UnableToTakePayment
from oscar.core.application import Application
from oscar.core.compat import AUTH_USER_MODEL
from oscar.core.loading import get_model, is_model_registered
from oscar.forms.widgets import DatePickerInput
from oscar.templatetags.currency_filters import currency

2 changes: 1 addition & 1 deletion src/oscar_accounts/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from oscar.core.loading import get_model
from oscar_accounts.compact_oscar import get_model

from oscar_accounts import names

Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/dashboard/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import patterns, url
from django.contrib.admin.views.decorators import staff_member_required
from oscar.core.application import Application

from oscar_accounts.compact_oscar import Application
from oscar_accounts.dashboard import views


Expand Down
4 changes: 1 addition & 3 deletions src/oscar_accounts/dashboard/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from django.conf import settings
from django.core import exceptions
from django.utils.translation import ugettext_lazy as _
from oscar.core.loading import get_model
from oscar.forms.widgets import DatePickerInput
from oscar.templatetags.currency_filters import currency

from oscar_accounts import codes, names
from oscar_accounts.compact_oscar import get_model, currency, DatePickerInput

Account = get_model('oscar_accounts', 'Account')
AccountType = get_model('oscar_accounts', 'AccountType')
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/dashboard/reports.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from decimal import Decimal as D

from django.db.models import Sum
from oscar.core.loading import get_model

from oscar_accounts import names
from oscar_accounts.compact_oscar import get_model

AccountType = get_model('oscar_accounts', 'AccountType')
Account = get_model('oscar_accounts', 'Account')
Expand Down
3 changes: 1 addition & 2 deletions src/oscar_accounts/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from oscar.core.loading import get_model
from oscar.templatetags.currency_filters import currency

from oscar_accounts import exceptions, facade, names
from oscar_accounts.compact_oscar import get_model, currency
from oscar_accounts.dashboard import forms, reports

AccountType = get_model('oscar_accounts', 'AccountType')
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/facade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from oscar.core.loading import get_model
from oscar_accounts.compact_oscar import get_model

from oscar_accounts import core, exceptions

Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms
from django.utils.translation import ugettext_lazy as _
from oscar.core.loading import get_model
from oscar_accounts.compact_oscar import get_model

Account = get_model('oscar_accounts', 'Account')

Expand Down
3 changes: 1 addition & 2 deletions src/oscar_accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from oscar.core.loading import is_model_registered

from oscar_accounts import abstract_models
from oscar_accounts.compact_oscar import is_model_registered

if not is_model_registered('oscar_accounts', 'AccountType'):
class AccountType(abstract_models.AccountType):
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/security.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from oscar.core.loading import get_model
from oscar_accounts.compact_oscar import get_model

IPAddressRecord = get_model('oscar_accounts', 'IPAddressRecord')

Expand Down
3 changes: 2 additions & 1 deletion src/oscar_accounts/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from oscar.core.loading import get_model


def create_default_accounts():
"""Create the default structure"""
from oscar_accounts import names
from oscar_accounts.compact_oscar import get_model

AccountType = get_model('oscar_accounts', 'AccountType')

assets = AccountType.add_root(name=names.ASSETS)
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_accounts/test_factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from decimal import Decimal as D

import factory
from oscar.core.loading import get_model
from oscar_accounts.compact_oscar import get_model


class AccountFactory(factory.DjangoModelFactory):
Expand Down