Skip to content

Commit

Permalink
Lots of minor tidy-up/pep8 related things - plus fixed lots of broken
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
codeinthehole committed Jun 13, 2011
1 parent 3348cfb commit 0dd0f38
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 91 deletions.
2 changes: 1 addition & 1 deletion examples/recreate_project_tables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ then
fi
echo "Recreating all tables in $PROJECT_FOLDER"
echo "Dropping tables"
$MANAGE_COMMAND sqlclear customer promotions analytics payment reviews offer shipping order basket partner image address product datacash | \
$MANAGE_COMMAND sqlclear customer promotions analytics payment reviews offer shipping order basket partner address product datacash | \
awk 'BEGIN {print "set foreign_key_checks=0;"} {print $0}' | \
$MANAGE_COMMAND dbshell && \
$MANAGE_COMMAND syncdb
6 changes: 3 additions & 3 deletions examples/vanilla/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
#'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
Expand Down Expand Up @@ -181,7 +181,7 @@
'django.contrib.admin',
'django.contrib.flatpages',
# External apps
#'django_extensions',
'django_extensions',
'haystack',
'debug_toolbar',
# Apps from oscar
Expand All @@ -205,7 +205,7 @@
'oscar.apps.promotions',
'oscar.apps.reports',
'oscar.apps.search',
#'pyzen',
'pyzen',
)

AUTHENTICATION_BACKENDS = (
Expand Down
1 change: 1 addition & 0 deletions oscar/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf.urls.defaults import patterns, url, include

from oscar.core.application import Application
from oscar.apps.product.app import application as product_app
from oscar.apps.customer.app import application as customer_app
Expand Down
5 changes: 2 additions & 3 deletions oscar/apps/address/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
from django.forms import ModelForm
from django.db.models import get_model

from oscar.core.loading import import_module
import_module('address.models', ['UserAddress'], locals())
UserAddress = get_model('address', 'useraddress')


class UserAddressForm(ModelForm):
Expand Down
52 changes: 26 additions & 26 deletions oscar/apps/basket/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class AbstractBasket(models.Model):
u"""Basket object"""
"""Basket object"""
# Baskets can be anonymously owned (which are merged if the user signs in)
owner = models.ForeignKey('auth.User', related_name='baskets', null=True)
STATUS_CHOICES = (
Expand Down Expand Up @@ -57,13 +57,13 @@ def all_lines(self):
# ============

def flush(self):
u"""Remove all lines from basket."""
"""Remove all lines from basket."""
if self.status == FROZEN:
raise PermissionDenied("A frozen basket cannot be flushed")
self.lines_all().delete()

def add_product(self, item, quantity=1, options=[]):
u"""
"""
Convenience method for adding products to a basket
The 'options' list should contains dicts with keys 'option' and 'value'
Expand All @@ -82,15 +82,15 @@ def add_product(self, item, quantity=1, options=[]):
line.attributes.create(line=line, option=option_dict['option'], value=option_dict['value'])

def set_discounts(self, discounts):
u"""
"""
Sets the discounts that apply to this basket.
This should be a list of dictionaries
"""
self.discounts = discounts

def merge_line(self, line):
u"""
"""
For transferring a line from another basket to this one.
This is used with the "Saved" basket functionality.
Expand Down Expand Up @@ -132,7 +132,7 @@ def thaw(self):
self.save()

def set_as_submitted(self):
u"""Mark this basket as submitted."""
"""Mark this basket as submitted."""
self.status = SUBMITTED
self.date_submitted = datetime.datetime.now()
self.save()
Expand All @@ -142,7 +142,7 @@ def set_as_submitted(self):
# =======

def _create_line_reference(self, item, options):
u"""
"""
Returns a reference string for a line based on the item
and its options.
"""
Expand All @@ -151,7 +151,7 @@ def _create_line_reference(self, item, options):
return "%d_%s" % (item.id, zlib.crc32(str(options)))

def _get_total(self, property):
u"""
"""
For executing a named method on each line of the basket
and returning the total.
"""
Expand All @@ -166,37 +166,37 @@ def _get_total(self, property):

@property
def is_empty(self):
u"""Return bool based on basket having 0 lines"""
"""Return bool based on basket having 0 lines"""
return self.num_lines == 0

@property
def total_excl_tax(self):
u"""Return total line price excluding tax"""
"""Return total line price excluding tax"""
return self._get_total('line_price_excl_tax_and_discounts')

@property
def total_tax(self):
u"""Return total tax for a line"""
"""Return total tax for a line"""
return self._get_total('line_tax')

@property
def total_incl_tax(self):
u"""Return total price for a line including tax"""
"""Return total price for a line including tax"""
return self._get_total('line_price_incl_tax_and_discounts')

@property
def num_lines(self):
u"""Return number of lines"""
"""Return number of lines"""
return self.all_lines().count()

@property
def num_items(self):
u"""Return number of items"""
"""Return number of items"""
return reduce(lambda num,line: num+line.quantity, self.all_lines(), 0)

@property
def num_items_without_discount(self):
u"""Return number of items"""
"""Return number of items"""
num = 0
for line in self.all_lines():
num += line.quantity_without_discount
Expand All @@ -216,7 +216,7 @@ def time_since_creation(self, test_datetime=None):


class AbstractLine(models.Model):
u"""A line of a basket (product and a quantity)"""
"""A line of a basket (product and a quantity)"""

basket = models.ForeignKey('basket.Basket', related_name='lines')
# This is to determine which products belong to the same line
Expand All @@ -241,7 +241,7 @@ def __unicode__(self):
return u"%s, Product '%s', quantity %d" % (self.basket, self.product, self.quantity)

def save(self, *args, **kwargs):
u"""Saves a line or deletes if it's quanity is 0"""
"""Saves a line or deletes if it's quanity is 0"""
if self.basket.status not in (OPEN, SAVED):
raise PermissionDenied("You cannot modify a %s basket" % self.basket.status.lower())
if self.quantity == 0:
Expand All @@ -260,7 +260,7 @@ def consume(self, quantity):
self._affected_quantity += quantity

def get_price_breakdown(self):
u"""
"""
Returns a breakdown of line prices after discounts have
been applied.
"""
Expand Down Expand Up @@ -315,22 +315,22 @@ def discount_value(self):

@property
def unit_price_excl_tax(self):
u"""Return unit price excluding tax"""
"""Return unit price excluding tax"""
return self._get_stockrecord_property('price_excl_tax')

@property
def unit_tax(self):
u"""Return tax of a unit"""
"""Return tax of a unit"""
return self._get_stockrecord_property('price_tax')

@property
def unit_price_incl_tax(self):
u"""Return unit price including tax"""
"""Return unit price including tax"""
return self._get_stockrecord_property('price_incl_tax')

@property
def line_price_excl_tax(self):
u"""Return line price excluding tax"""
"""Return line price excluding tax"""
return self.quantity * self.unit_price_excl_tax

@property
Expand All @@ -339,12 +339,12 @@ def line_price_excl_tax_and_discounts(self):

@property
def line_tax(self):
u"""Return line tax"""
"""Return line tax"""
return self.quantity * self.unit_tax

@property
def line_price_incl_tax(self):
u"""Return line price including tax"""
"""Return line price including tax"""
return self.quantity * self.unit_price_incl_tax

@property
Expand All @@ -353,7 +353,7 @@ def line_price_incl_tax_and_discounts(self):

@property
def description(self):
u"""Return product description"""
"""Return product description"""
d = str(self.product)
ops = []
for attribute in self.attributes.all():
Expand All @@ -364,7 +364,7 @@ def description(self):


class AbstractLineAttribute(models.Model):
u"""An attribute of a basket line"""
"""An attribute of a basket line"""
line = models.ForeignKey('basket.Line', related_name='attributes')
option = models.ForeignKey('product.Option')
value = models.CharField(_("Value"), max_length=255)
Expand Down
2 changes: 2 additions & 0 deletions oscar/apps/basket/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.conf.urls.defaults import patterns, url

from oscar.apps.basket.views import BasketView, SavedView, VoucherView, VoucherAddView, BasketAddView
from oscar.core.application import Application


class BasketApplication(Application):
name = 'basket'
summary_view = BasketView
Expand Down
20 changes: 12 additions & 8 deletions oscar/apps/basket/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
basketline_model = get_model('basket', 'line')
basket_model = get_model('basket', 'basket')


class BasketLineForm(forms.ModelForm):
save_for_later = forms.BooleanField(initial=False, required=False)

Expand All @@ -26,10 +27,20 @@ class BasketVoucherForm(forms.Form):
def __init__(self, *args, **kwargs):
return super(BasketVoucherForm, self).__init__(*args,**kwargs)


class AddToBasketForm(forms.Form):
product_id = forms.IntegerField(widget=forms.HiddenInput(), min_value=1)
quantity = forms.IntegerField(initial=1, min_value=1)

def __init__(self, instance, *args, **kwargs):
super(AddToBasketForm, self).__init__(*args, **kwargs)
self.instance = instance
if instance:
if instance.is_group:
self._create_group_product_fields(instance)
else:
self._create_product_fields(instance)

def _create_group_product_fields(self, item):
u"""
Adds the fields for a "group"-type product (eg, a parent product with a
Expand Down Expand Up @@ -57,11 +68,4 @@ def _add_option_field(self, item, option):
"""
self.fields[option.code] = forms.CharField()

def __init__(self, instance, *args, **kwargs):
self.instance = instance
if instance:
if instance.is_group:
self._create_group_product_fields(instance)
else:
self._create_product_fields(instance)
super(AddToBasketForm, self).__init__(*args, **kwargs)

6 changes: 3 additions & 3 deletions oscar/apps/basket/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ViewTest(TwillTestCase):

def test_for_smoke(self):
self.visit('oscar-basket')
self.visit('basket:summary')
self.assertResponseCodeIs(200)
self.assertPageContains('Basket')
self.assertPageTitleMatches('Oscar')
Expand Down Expand Up @@ -46,14 +46,14 @@ def setUp(self):
self.client = Client()

def test_empty_basket_view(self):
url = reverse('basket')
url = reverse('basket:summary')
response = self.client.get(url)
self.assertEquals(200, response.status_code)
self.assertEquals(0, response.context['basket'].num_lines)

def test_anonymous_add_to_basket_creates_cookie(self):
dummy_product = create_product()
url = reverse('basket-add')
url = reverse('basket:add')
post_params = {'product_id': dummy_product.id,
'action': 'add',
'quantity': 1}
Expand Down
13 changes: 7 additions & 6 deletions oscar/apps/basket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
from django.db.models import get_model
from django.http import HttpResponseRedirect, Http404
from django.views.generic import ListView, FormView
from django.forms.models import modelformset_factory

from extra_views import ModelFormsetView
from oscar.apps.basket.forms import BasketLineForm, AddToBasketForm, \
BasketVoucherForm, SavedLineForm
from django.forms.models import modelformset_factory


class BasketView(ModelFormsetView):
model = get_model('basket', 'line')
basket_model = get_model('basket', 'basket')
model = get_model('basket', 'Line')
basket_model = get_model('basket', 'Basket')
form_class = BasketLineForm
extra = 0
can_delete = True
template_name='basket/basket.html'


def get_queryset(self):
return self.request.basket.lines.all()

Expand Down Expand Up @@ -160,10 +161,10 @@ def formset_valid(self, formset):
messages.info(self.request, msg)
real_basket = self.request.basket
real_basket.merge_line(form.instance)
return HttpResponseRedirect(self.request.META.get('HTTP_REFERER',reverse('basket:summary')))
return HttpResponseRedirect(self.request.META.get('HTTP_REFERER', reverse('basket:summary')))

def formset_invalid(self, formset):
return HttpResponseRedirect(self.request.META.get('HTTP_REFERER',reverse('basket:summary')))
return HttpResponseRedirect(self.request.META.get('HTTP_REFERER', reverse('basket:summary')))


# def do_remove_voucher(self, basket):
Expand Down
2 changes: 1 addition & 1 deletion oscar/apps/checkout/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_anonymous_checkout(self):

# Add a product to the basket
p = create_product(price=D('10.00'))
response = self.client.post(reverse('oscar-basket'), {'action': 'add',
response = self.client.post(reverse('basket:add'), {'action': 'add',
'product_id': str(p.id),
'quantity': 1})
self.assertEqual(302, response.status_code)
Expand Down
1 change: 1 addition & 0 deletions oscar/apps/customer/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf.urls.defaults import patterns, url
from django.contrib.auth.decorators import login_required

from oscar.apps.customer.views import AccountSummaryView, OrderHistoryView, \
OrderDetailView, OrderLineView, AddressListView, AddressCreateView, \
AddressUpdateView, AddressDeleteView, EmailHistoryView, EmailDetailView, \
Expand Down
1 change: 1 addition & 0 deletions oscar/apps/customer/auth_backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend


class Emailbackend(ModelBackend):
def authenticate(self, email=None, password=None, *args, **kwargs):
if not email:
Expand Down
Loading

0 comments on commit 0dd0f38

Please sign in to comment.