Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
sort by purchase date
Browse files Browse the repository at this point in the history
  • Loading branch information
cvan committed Dec 2, 2011
1 parent 29586f6 commit 73a9c49
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
2 changes: 1 addition & 1 deletion apps/users/templates/users/purchases.html
Expand Up @@ -17,7 +17,7 @@ <h1>{{ _('My Purchases') }}</h1>
{{ _('Show all purchases') }}</a></li></ul>
</div>
{% else %}
{{ impala_addon_listing_header(url_base, filter.opts, sorting, filter.extras) }}
{{ impala_addon_listing_header(request.get_full_path(), search_filter=filter) }}
{% endif %}
<div class="items">
{% for addon in addons.object_list %}
Expand Down
73 changes: 43 additions & 30 deletions apps/users/tests/test_views.py
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import json

from django.conf import settings
Expand Down Expand Up @@ -42,7 +42,7 @@ def setUp(self):
self.client = amo.tests.TestClient()
self.client.get('/')
self.user = User.objects.get(id='4043307')
self.user_profile = self.get_profile()
self.user_profile = self.user.get_profile()

def get_profile(self):
return UserProfile.objects.get(id=self.user.id)
Expand Down Expand Up @@ -1096,6 +1096,7 @@ def setUp(self):
self.url = reverse('users.purchases')
self.client.login(username='regular@mozilla.com', password='password')
self.user = User.objects.get(email='regular@mozilla.com')
self.user_profile = self.user.get_profile()

self.addon, self.con = None, None
for x in range(1, 5):
Expand All @@ -1104,7 +1105,7 @@ def setUp(self):
name='t%s' % x,
guid='t%s' % x)
AddonPremium.objects.create(price=price, addon=addon)
con = Contribution.objects.create(user=self.user.get_profile(),
con = Contribution.objects.create(user=self.user_profile,
addon=addon, amount='%s.00' % x,
type=amo.CONTRIB_PURCHASE)
con.created = datetime(2011, 11, 1)
Expand All @@ -1127,12 +1128,12 @@ def check_sidebar_links(self, expected):
def test_in_sidebar(self):
# Populate this user's favorites.
c = Collection.objects.create(type=amo.COLLECTION_FAVORITES,
author=self.user.get_profile())
author=self.user_profile)
CollectionAddon.objects.create(addon=Addon.objects.all()[0],
collection=c)

expected = [
('My Profile', self.user.get_profile().get_url_path()),
('My Profile', self.user_profile.get_url_path()),
('Account Settings', reverse('users.edit')),
('My Collections', reverse('collections.mine')),
('My Favorites', reverse('collections.mine', args=['favorites'])),
Expand All @@ -1143,7 +1144,7 @@ def test_in_sidebar(self):
@patch.object(settings, 'APP_PREVIEW', True)
def test_in_apps_sidebar(self):
expected = [
('My Profile', self.user.get_profile().get_url_path()),
('My Profile', self.user_profile.get_url_path()),
('Account Settings', reverse('users.edit')),
('My Purchases', self.url),
]
Expand Down Expand Up @@ -1182,16 +1183,30 @@ def test_purchase_date(self):

def get_order(self, order):
res = self.client.get('%s?sort=%s' % (self.url, order))
return [str(c.name) for c in
res.context['addons'].object_list]
return [str(c.name) for c in res.context['addons'].object_list]

def test_ordering(self):
eq_(self.get_order('name'), ['t1', 't2', 't3', 't4'])
eq_(self.get_order('price'), ['t4', 't3', 't2', 't1'])

def test_ordering_purchased(self):
for addon in Addon.objects.all():
purchase = addon.addonpurchase_set.all()[0]
purchase.update(created=datetime.now() + timedelta(days=addon.id))
eq_(self.get_order(''), ['t4', 't3', 't2', 't1'])
eq_(self.get_order('purchased'), ['t4', 't3', 't2', 't1'])

Addon.objects.get(guid='t2').addonpurchase_set.all()[0].update(
created=datetime.now() + timedelta(days=999))
eq_(self.get_order('purchased'), ['t2', 't4', 't3', 't1'])

def get_pq(self):
r = self.client.get(self.url, dict(sort='name'))
eq_(r.status_code, 200)
return pq(r.content)('#purchases')

def _test_price(self):
res = self.client.get(self.url)
assert '$1.00' in pq(res.content)('#purchases .purchase').eq(0).text()
assert '$1.00' in self.get_pq()('.purchase').eq(0).text()

def test_price(self):
self._test_price()
Expand All @@ -1201,8 +1216,8 @@ def test_mobile_price(self):
self._test_price()

def _test_price_locale(self):
res = self.client.get(self.url.replace('/en-US', '/fr'))
assert u'1,00' in pq(res.content)('#purchases .purchase').eq(0).text()
self.url = self.url.replace('/en-US', '/fr')
assert u'1,00' in self.get_pq()('.purchase').eq(0).text()

def test_price_locale(self):
self._test_price_locale()
Expand All @@ -1213,24 +1228,23 @@ def test_mobile_price_locale(self):

def test_receipt(self):
res = self.client.get(reverse('users.purchases.receipt',
args=[self.addon.pk]))
eq_(len(res.context['addons'].object_list), 1)
eq_(res.context['addons'].object_list[0].pk, self.addon.pk)
args=[self.addon.pk]))
eq_([a.pk for a in res.context['addons'].object_list], [self.addon.pk])

def test_receipt_404(self):
url = reverse('users.purchases.receipt', args=[545])
eq_(self.client.get(url).status_code, 404)

def test_receipt_view(self):
res = self.client.get(reverse('users.purchases.receipt',
args=[self.addon.pk]))
args=[self.addon.pk]))
eq_(pq(res.content)('#sorter a').attr('href'),
reverse('users.purchases'))

@amo.tests.mobile_test
def test_mobile_receipt_view(self):
res = self.client.get(reverse('users.purchases.receipt',
args=[self.addon.pk]))
args=[self.addon.pk]))
eq_(pq(res.content)('#sort-menu a').attr('href'),
reverse('users.purchases'))

Expand All @@ -1248,13 +1262,12 @@ def test_mobile_purchases_attribute(self):
self._test_purchases_attribute()

def test_no_purchases_attribute(self):
self.user.get_profile().addonpurchase_set.all().delete()
self.user_profile.addonpurchase_set.all().delete()
doc = pq(self.client.get(self.url).content)
eq_(doc('body').attr('data-purchases'), '')

def _test_refund_link(self):
doc = pq(self.client.get(self.url).content)('#purchases')
eq_(doc('.supportable + a.request-support').eq(0).attr('href'),
eq_(self.get_pq()('a.request-support').eq(0).attr('href'),
self.get_url())

def test_refund_link(self):
Expand Down Expand Up @@ -1399,15 +1412,13 @@ def test_others_free_dont(self):
eq_(len(res.context['addons']), 3)

def test_purchase_multiple(self):
Contribution.objects.create(user=self.user.get_profile(),
Contribution.objects.create(user=self.user_profile,
addon=self.addon, amount='1.00',
type=amo.CONTRIB_PURCHASE)
res = self.client.get(self.url)
addon_vitals = pq(res.content)('#purchases .vitals').eq(0)
eq_(addon_vitals('.purchase').length, 2)
eq_(self.get_pq()('.vitals').eq(0)('.purchase').length, 2)

def make_contribution(self, addon, amt, type, day):
c = Contribution.objects.create(user=self.user.get_profile(),
c = Contribution.objects.create(user=self.user_profile,
addon=addon, amount=amt, type=type)
c.created = datetime(2011, 11, day)
c.save()
Expand All @@ -1416,8 +1427,8 @@ def make_contribution(self, addon, amt, type, day):
def _test_refunded(self):
addon = Addon.objects.get(type=amo.ADDON_EXTENSION, guid='t1')
self.make_contribution(addon, '-1.00', amo.CONTRIB_REFUND, 2)
doc = pq(self.client.get(self.url).content)
item = doc('#purchases .item').eq(0)
doc = pq(self.client.get(self.url, {'sort': 'name'}).content)
item = self.get_pq()('.item').eq(0)
assert item.hasClass('refunded'), (
"Expected '.item' to have 'refunded' class")
assert item.find('.refund-notice'), 'Expected refund message'
Expand All @@ -1435,7 +1446,7 @@ def _test_repurchased(self):
self.make_contribution(addon, '-1.00', amo.CONTRIB_REFUND, 2),
self.make_contribution(addon, '1.00', amo.CONTRIB_PURCHASE, 3)
]
item = pq(self.client.get(self.url).content)('#purchases .item').eq(0)
item = self.get_pq()('.item').eq(0)
assert not item.hasClass('reversed'), (
"Unexpected 'refunded' class on '.item'")
assert not item.find('.refund-notice'), 'Unexpected refund message'
Expand All @@ -1454,7 +1465,8 @@ def _test_rerefunded(self):
self.make_contribution(addon, '-1.00', amo.CONTRIB_REFUND, 2)
self.make_contribution(addon, '1.00', amo.CONTRIB_PURCHASE, 3)
self.make_contribution(addon, '-1.00', amo.CONTRIB_REFUND, 4)
item = pq(self.client.get(self.url).content)('#purchases .item').eq(0)
r = self.client.get(self.url, {'sort': 'name'})
item = self.get_pq()('.item').eq(0)
assert item.hasClass('refunded'), (
"Unexpected 'refunded' class on '.item'")
assert item.find('.refund-notice'), 'Expected refund message'
Expand All @@ -1471,7 +1483,8 @@ def test_mobile_rerefunded(self):
def _test_chargeback(self):
addon = Addon.objects.get(type=amo.ADDON_EXTENSION, guid='t1')
self.make_contribution(addon, '-1.00', amo.CONTRIB_CHARGEBACK, 2)
item = pq(self.client.get(self.url).content)('#purchases .item').eq(0)
r = self.client.get(self.url, {'sort': 'name'})
item = self.get_pq()('.item').eq(0)
assert item.hasClass('reversed'), (
"Expected '.item' to have 'reversed' class")
assert not item.find('a.request-support'), (
Expand Down
16 changes: 9 additions & 7 deletions apps/users/views.py
Expand Up @@ -32,7 +32,7 @@
permission_required, write, post_required)
from amo.forms import AbuseForm
from amo.urlresolvers import reverse
from amo.helpers import absolutify
from amo.helpers import absolutify, loc
from amo.utils import send_mail, urlparams
from abuse.models import send_abuse_report
from addons.models import Addon
Expand Down Expand Up @@ -673,12 +673,15 @@ def unsubscribe(request, hash=None, token=None, perm_setting=None):


class AddonsFilter(BaseFilter):
opts = (('price', _lazy(u'Price')),
opts = (('purchased', loc('Purchase Date')),
('price', _lazy(u'Price')),
('name', _lazy(u'Name')))

def filter(self, field):
qs = self.base_queryset
if field == 'price':
if field == 'purchased':
return qs.order_by('-addonpurchase__created')
elif field == 'price':
return qs.order_by('addonpremium__price__price')
elif field == 'name':
return order_by_translation(qs, 'name')
Expand Down Expand Up @@ -715,18 +718,17 @@ def purchases(request, addon_id=None, template=None):
addons = Addon.objects.filter(id__in=ids)
if webapp:
addons = addons.filter(type=amo.ADDON_WEBAPP)
filter = AddonsFilter(request, addons, key='sort', default='name')
filter = AddonsFilter(request, addons, key='sort', default='purchased')

if addon_id and not filter.qs:
# User has requested a receipt for an addon they don't have.
raise http.Http404

addons = amo.utils.paginate(request, filter.qs, count=len(ids))
return jingo.render(request, template,
{'addons': amo.utils.paginate(request, filter.qs,
count=len(ids)),
{'addons': addons,
'webapp': webapp,
'filter': filter,
'url_base': reverse('users.purchases'),
'contributions': contributions,
'single': bool(addon_id)})

Expand Down
1 change: 1 addition & 0 deletions media/css/impala/listing.less
Expand Up @@ -228,6 +228,7 @@
.vitals {
margin-top: 2px;
font-size: 11px;
line-height: 13px;
.stars {
height: 12px;
}
Expand Down

0 comments on commit 73a9c49

Please sign in to comment.