Add time ranges to featured apps tool (bug 779898)

This commit is contained in:
Allen Short 2012-08-23 17:26:07 -07:00
Родитель f1b61d5ba3
Коммит 44000f6c0d
10 изменённых файлов: 154 добавлений и 15 удалений

Просмотреть файл

@ -24,11 +24,53 @@ function registerAddonAutocomplete(node) {
};
}
function registerDatepicker(node) {
var $startPicker = node.find('.start-date-picker');
var $tabl = $startPicker.closest('table');
var url = $tabl.data('url');
var appid = $tabl.data('app-id');
$startPicker.datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(dateText) {
node.find('.date-range-start').val(dateText);
saveFeaturedDate('startdate', url, appid, dateText);
}
});
var $endPicker = node.find('.end-date-picker');
$endPicker.datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(dateText) {
node.find('.date-range-end').val(dateText);
saveFeaturedDate('enddate', url, appid, dateText);
}
});
var $start = node.find('.date-range-start');
$start.change(
function (e) {
saveFeaturedDate('startdate', $tabl.data('url'), $tabl.data('app-id'), $start.val());
});
var $end = node.find('.date-range-end');
$end.change(
function (e) {
saveFeaturedDate('enddate', $tabl.data('url'), $tabl.data('app-id'), $end.val());
});
}
function saveFeaturedDate(which, url, appid, val) {
var data = {};
data[which] = val;
data.app = appid;
$.ajax({type: 'POST', url: url, data: data});
}
function newAddonSlot(id) {
var $tbody = $("#featured-webapps");
var $form = $tbody.next().children("tr").clone();
var $input = $form.find('input.placeholder');
registerAddonAutocomplete($input);
registerDatepicker($form);
$tbody.append($form);
}
@ -73,16 +115,17 @@ $(document).ready(function(){
'select.localepicker',
'change',
_pd(function (e) {
var region = $(e.target);
var $region = $(e.target);
var $tabl = $region.closest('table');
$.ajax({
type: 'POST',
url: region.data('url'),
url: $tabl.data('url'),
data: {
'region':
_(region.children('option'))
_($region.children('option'))
.filter(function(opt) {return opt.selected})
.map(function(sopt) {return sopt.value}),
'app': region.data('id')
'app': $tabl.data('app-id')
}
});
})
@ -93,10 +136,16 @@ $(document).ready(function(){
url: categories.data("src")});
p.then(function(data) {
categories.html(data);
showAppsList(categories);
showAppsList(categories).then(
function () {
registerDatepicker(appslist);
});
});
categories.change(function (e) {
showAppsList(categories);
showAppsList(categories).then(
function () {
registerDatepicker(appslist);
});
});
$('#featured-add').click(_pd(function() { newAddonSlot(); }));
});

Просмотреть файл

@ -0,0 +1,2 @@
ALTER TABLE `zadmin_featuredapp` ADD COLUMN `start_date` DATE,
ADD COLUMN `end_date` DATE;

Просмотреть файл

@ -1,3 +1,4 @@
import datetime
from django.conf import settings
from nose import SkipTest
@ -38,6 +39,7 @@ class BrowseBase(amo.tests.ESTestCase):
# Feature in the US region.
FeaturedAppRegion.objects.create(featured_app=f,
region=mkt.regions.US.id)
return f
def setup_featured(self):
self.skip_if_disabled(settings.REGION_STORES)

Просмотреть файл

@ -1,3 +1,5 @@
import datetime
from nose.tools import eq_
from amo.tests import app_factory, mock_es
@ -5,6 +7,7 @@ from amo.urlresolvers import reverse
import mkt
from mkt.browse.tests.test_views import BrowseBase
from mkt.webapps.models import Webapp
from mkt.zadmin.models import FeaturedApp, FeaturedAppRegion
@ -56,3 +59,28 @@ class TestHome(BrowseBase):
def test_popular_region_exclusions(self):
self._test_popular_region_exclusions()
def make_time_limited_feature(self):
a = app_factory()
fa = self.make_featured(app=a, category=None)
fa.startdate = datetime.date(2012, 1, 1)
fa.enddate = datetime.date(2012, 2, 1)
fa.save()
return a
@mock_es
def test_featured_time_excluded(self):
a = self.make_time_limited_feature()
for d in [datetime.date(2012, 1, 1),
datetime.date(2012, 1, 15),
datetime.date(2012, 2, 1)]:
Webapp.now = staticmethod(lambda: d)
eq_(self.get_pks('featured', self.url, {'region': 'us'}),
[a.id])
def test_featured_time_included(self):
self.make_time_limited_feature()
for d in [datetime.date(2011, 12, 15),
datetime.date(2012, 2, 2)]:
Webapp.now = staticmethod(lambda: d)
eq_(self.get_pks('featured', self.url, {'region': 'us'}), [])

Просмотреть файл

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import datetime
import json
import os
import urlparse
@ -399,12 +400,19 @@ class Webapp(Addon):
return list(self.content_ratings.filter(ratings_body__in=rb)
.order_by('rating'))
@classmethod
def now(cls):
return datetime.date.today()
@classmethod
def featured(cls, cat=None, region=None, limit=6):
FeaturedApp = models.get_model('zadmin', 'FeaturedApp')
qs = (FeaturedApp.objects
.filter(app__status=amo.STATUS_PUBLIC,
app__disabled_by_user=False)
app__disabled_by_user=False,
start_date__lte=cls.now(),
end_date__gte=cls.now()
)
.order_by('-app__weekly_downloads'))
if isinstance(cat, list):

Просмотреть файл

@ -10,6 +10,8 @@ class FeaturedApp(models.Model):
app = models.ForeignKey(Webapp, null=False)
category = models.ForeignKey(Category, null=True)
is_sponsor = models.BooleanField(default=False)
start_date = models.DateField(null=True)
end_date = models.DateField(null=True)
class Meta:
db_table = 'zadmin_featuredapp'

Просмотреть файл

@ -2,7 +2,7 @@
{% for row, selected_regions in apps_regions -%}
<tr>
<td>
<table>
<table data-app-id="{{ row.pk }}" data-url="{{ url('zadmin.set_attrs_ajax') }}">
<tr>
<td><img src="{{ row.app.icon_url }}"></td>
<td>{{ row.app.name }}</td>
@ -19,9 +19,7 @@
</td>
<td>{% if row.is_sponsor %}Sponsored{% else %}Not sponsored{% endif %}</td>
<td>
<select class="localepicker" multiple
data-url="{{ url('zadmin.set_region_ajax') }}"
data-id="{{ row.pk }}">
<select class="localepicker" multiple>
{%- for locName, loc in regions -%}
<option value="{{ loc.id }}"
{%- if loc.id in selected_regions %} selected{%- endif -%}
@ -30,6 +28,24 @@
</select>
</td>
</tr>
<tr>
<td>
{{ _('Start date') }}
</td>
<td>
<input type="date" class="date-range-start" value="{{ row.start_date }}">
<div class="start-date-picker"></div>
</td>
</tr>
<tr>
<td>
{{ _('End date') }}
</td>
<td>
<input type="date" class="date-range-end" value="{{ row.end_date }}">
<div class="end-date-picker"></div>
</td>
</tr>
</table>
</td>
<td><input type="hidden"><a class="remove" data-id="{{ row.app.id }}">×</a></td>

Просмотреть файл

@ -1,3 +1,5 @@
from datetime import date
from nose.tools import eq_
from pyquery import PyQuery as pq
@ -114,7 +116,7 @@ class TestFeaturedApps(amo.tests.TestCase):
def test_set_region(self):
f = FeaturedApp.objects.create(app=self.a1, category=None)
FeaturedAppRegion.objects.create(featured_app=f, region=1)
r = self.client.post(reverse('zadmin.set_region_ajax'),
r = self.client.post(reverse('zadmin.set_attrs_ajax'),
data={'app': f.pk, 'region[]': (3, 2)})
eq_(r.status_code, 200)
eq_(list(FeaturedApp.objects.get(pk=f.pk).regions.values_list(
@ -122,6 +124,23 @@ class TestFeaturedApps(amo.tests.TestCase):
[2, 3])
def test_set_startdate(self):
f = FeaturedApp.objects.create(app=self.a1, category=None)
FeaturedAppRegion.objects.create(featured_app=f, region=1)
r = self.client.post(reverse('zadmin.set_attrs_ajax'),
data={'app': f.pk, 'startdate': '2012-08-01'})
eq_(r.status_code, 200)
eq_(FeaturedApp.objects.get(pk=f.pk).startdate, date(2012, 8, 1))
def test_set_enddate(self):
f = FeaturedApp.objects.create(app=self.a1, category=None)
FeaturedAppRegion.objects.create(featured_app=f, region=1)
r = self.client.post(reverse('zadmin.set_attrs_ajax'),
data={'app': f.pk, 'enddate': '2012-08-31'})
eq_(r.status_code, 200)
eq_(FeaturedApp.objects.get(pk=f.pk).enddate, date(2012, 8, 31))
class TestAddonSearch(amo.tests.ESTestCase):
fixtures = ['base/users', 'webapps/337141-steamcube', 'base/addon_3615']

Просмотреть файл

@ -12,6 +12,6 @@ urlpatterns = patterns(
name='zadmin.featured_apps_ajax'),
url('^apps/featured_categories_ajax$', views.featured_categories_ajax,
name='zadmin.featured_categories_ajax'),
url('^apps/set_region_ajax$', views.set_region_ajax,
name='zadmin.set_region_ajax'),
url('^apps/set_attrs_ajax$', views.set_attrs_ajax,
name='zadmin.set_attrs_ajax'),
)

Просмотреть файл

@ -1,3 +1,5 @@
import datetime
import jingo
from django.contrib import admin
@ -71,8 +73,11 @@ def featured_apps_ajax(request):
@admin_required
def set_region_ajax(request):
def set_attrs_ajax(request):
regions = request.POST.getlist('region[]')
startdate = request.POST.get('startdate', None)
enddate = request.POST.get('enddate', None)
app = request.POST.get('app', None)
if regions and app:
fa = FeaturedApp.objects.get(pk=app)
@ -83,6 +88,14 @@ def set_region_ajax(request):
for i in to_create:
FeaturedAppRegion.objects.create(featured_app=fa, region=i)
if startdate and app:
FeaturedApp.objects.update(
start_date=datetime.datetime.strptime(startdate,
'%Y-%m-%d'))
if enddate and app:
FeaturedApp.objects.update(
end_date=datetime.datetime.strptime(enddate,
'%Y-%m-%d'))
return HttpResponse()