Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make an admin view for updating prices (bug 800520)

  • Loading branch information...
commit e7e059fdcd36fda45a087b26576c2c82c6e297a6 1 parent 6d7cb3b
@andymckay andymckay authored
View
37 apps/market/tests/test_utils_.py
@@ -0,0 +1,37 @@
+from StringIO import StringIO
+
+from nose.tools import eq_
+
+import amo.tests
+
+from market.models import Price, PriceCurrency
+from market.utils import update, update_from_csv
+
+tiers = [
+ {'USD': '0.99', 'BRL': '1.99'},
+ # This row should be ignored, no tier of value 3.
+ {'USD': '3.00'},
+ # This row should be ignored, not US tier.
+ {'CAD': '10'}
+]
+
+csv = StringIO("""USD\tCAD\tBRL\n0.99\t1.99\t1.00""")
+
+class TestUpdate(amo.tests.TestCase):
+
+ def setUp(self):
+ self.tier = Price.objects.create(name='tier 1', price='0.99')
+
+ def test_create(self):
+ update(tiers)
+ eq_(str(PriceCurrency.objects.get(currency='BRL').price), '1.99')
+ assert not PriceCurrency.objects.filter(currency='CAD').exists()
+
+ def test_update(self):
+ PriceCurrency.objects.create(currency='BRL', tier=self.tier, price='2')
+ update(tiers)
+ eq_(str(PriceCurrency.objects.get(currency='BRL').price), '1.99')
+
+ def test_csv(self):
+ update_from_csv(csv)
+ assert PriceCurrency.objects.filter(currency='CAD').exists()
View
62 apps/market/utils.py
@@ -0,0 +1,62 @@
+import csv
+
+import commonware.log
+
+from decimal import Decimal
+from market.models import Price, PriceCurrency
+
+log = commonware.log.getLogger('z.market')
+
+
+def update(tiers):
+ """
+ Updates the prices and price currency objects based on the tiers.
+
+ Tiers should be a list containing a dictionary of currency / value pairs.
+ The value of US is required so that we can look up the price tier. If the
+ price tier for US isn't found, we skip whole tier. If the currency isn't
+ found but the tier is, we create the currency.
+
+ This is intended to be called via a migration or other command.
+ """
+ output = []
+ for row in tiers:
+ us = row.get('USD')
+ if not us:
+ output.append('No USD in row, skipped')
+ continue
+
+ try:
+ tier = Price.objects.get(price=Decimal(us))
+ except Price.DoesNotExist:
+ output.append('Tier not found, skipping: %s' % us)
+ continue
+
+ for currency, value in row.iteritems():
+ if currency == 'USD':
+ continue
+
+ try:
+ curr = PriceCurrency.objects.get(tier=tier, currency=currency)
+ except PriceCurrency.DoesNotExist:
+ curr = PriceCurrency(tier=tier, currency=currency)
+
+ curr.price = Decimal(value)
+ curr.save()
+ output.append('Currency updated: %s, %s, tier %s' %
+ (currency, value, us))
+
+ return output
+
+
+def update_from_csv(handle):
+ reader = csv.reader(handle, delimiter='\t')
+ headers = []
+ output = []
+ for row in reader:
+ if not headers:
+ headers = row
+ continue
+ output.append(dict(zip(headers, row)))
+
+ return update(output)
View
4 apps/zadmin/forms.py
@@ -329,3 +329,7 @@ class IOError(Exception):
1 / 0
except:
settings.METLOG.raven('metlog_sentry error triggered')
+
+
+class PriceTiersForm(happyforms.Form):
+ prices = forms.FileField()
View
28 apps/zadmin/templates/zadmin/update-prices.html
@@ -0,0 +1,28 @@
+{% extends "admin/base.html" %}
+
+{% block title %}Update Prices {{ super() }}{% endblock %}
+
+{% block extrahead %}
+<style>form { margin-bottom: 1em }</style>
+{% endblock %}
+
+{% block content %}
+ {% if result %}
+ <p>{% for row in result %}{{ row }}{% if not loop.last %}<br>{% endif %}{% endfor %}</p>
+ {% endif %}
+ <p>
+ This form accepts a tab delimited CSV file containing prices and currencies.
+ Columns can be in any order. Do not include any currency symbols.
+ Format:
+ <pre>
+ USD CAD BRL
+ 0.99 1.99 2.99
+ 1.99 2.99 3.99
+ </pre>
+ </p>
+ <form enctype="multipart/form-data" method="post">
+ {{ csrf() }}
+ {{ form }}
+ <input type="submit" value="Update">
+ </form>
+{% endblock %}
View
2  apps/zadmin/urls.py
@@ -69,6 +69,8 @@
url('^email_addresses_file$', views.email_addresses_file,
name='zadmin.email_addresses_file'),
+ url('^price-tiers$', views.price_tiers, name='zadmin.price_tiers'),
+
# Site Event admin.
url('^events/(?P<event_id>\d+)?$', views.site_events,
name='zadmin.site_events'),
View
14 apps/zadmin/views.py
@@ -43,13 +43,14 @@
from files.models import Approval, File
from files.tasks import start_upgrade as start_upgrade_task
from files.utils import find_jetpacks, JetpackUpgrader
+from market.utils import update_from_csv
from stats.cron import index_latest_stats
from stats.search import setup_indexes
from users.cron import reindex_users
from users.models import UserProfile
from versions.compare import version_int as vint
from versions.models import Version
-from zadmin.forms import GenerateErrorForm, SiteEventForm
+from zadmin.forms import GenerateErrorForm, PriceTiersForm, SiteEventForm
from zadmin.models import SiteEvent
from . import tasks
@@ -902,3 +903,14 @@ def email_addresses_file(request):
if e is not None:
resp.write(e + '\n')
return resp
+
+
+@admin_required
+def price_tiers(request):
+ output = []
+ form = PriceTiersForm(request.POST or None, request.FILES)
+ if request.method == 'POST' and form.is_valid():
+ output = update_from_csv(form.cleaned_data['prices'])
+
+ return jingo.render(request, 'zadmin/update-prices.html',
+ {'result': output, 'form': form})
View
1  default/site/helpers.py
@@ -31,6 +31,7 @@ def admin_site_links():
('View site settings', url('zadmin.settings')),
('Django admin pages', url('zadmin.home')),
('Site Events', url('zadmin.site_events')),
+ ('Update prices', url('zadmin.price_tiers')),
],
'tools': [
('View request environment', url('amo.env')),
View
1  mkt/purchase/webpay.py
@@ -117,6 +117,7 @@ def prepare_pay(request, addon):
'aud': 'tu.com',
'memo': contrib_for}
+ data['app_description'] = 'foo'
return {'webpayJWT': prepare_webpay_pay(data),
'contribStatusURL': reverse('webpay.pay_status',
args=[addon.app_slug, uuid_])}
Please sign in to comment.
Something went wrong with that request. Please try again.