create task to reparse get_satisfaction_company and correct (bug 652133)

This commit is contained in:
Andy McKay 2011-05-30 12:00:35 -07:00
Родитель 9772e59d39
Коммит 38b069f3a6
4 изменённых файлов: 73 добавлений и 20 удалений

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

@ -314,6 +314,18 @@ class AddonFormDetails(AddonFormBase):
return data
def get_satisfaction(url):
"""
If there's a GetSatisfaction URL entered, we'll extract the product
and company name..
"""
gs_regex = "getsatisfaction\.com/(\w*)(?:/products/(\w*))?"
match = re.search(gs_regex, url)
if match:
return match.groups()
return None, None
class AddonFormSupport(AddonFormBase):
support_url = TransField.adapt(forms.URLField)(required=False,
verify_exists=False)
@ -325,20 +337,9 @@ class AddonFormSupport(AddonFormBase):
def save(self, addon, commit=True):
instance = self.instance
# If there's a GetSatisfaction URL entered, we'll extract the product
# and company name and save it to the DB.
gs_regex = "getsatisfaction\.com/(\w*)(?:/products/(\w*))?"
match = re.search(gs_regex, instance.support_url.localized_string)
company = product = None
if match:
company, product = match.groups()
instance.get_satisfaction_company = company
instance.get_satisfaction_product = product
url = instance.support_url.localized_string
(instance.get_satisfaction_company,
instance.get_satisfaction_product) = get_satisfaction(url)
return super(AddonFormSupport, self).save(commit)

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

@ -1,33 +1,42 @@
from optparse import make_option
from django.core.management.base import BaseCommand
from django.db.models import Q
from addons.models import Addon
from addons.tasks import fix_get_satisfaction
from amo.utils import chunked
from devhub.tasks import flag_binary
from celery.messaging import establish_connection
tasks = {
'flag_binary': flag_binary,
'flag_binary': {'method': flag_binary, 'qs': []},
'fix_get_satisfaction': {
'method': fix_get_satisfaction,
'qs': [Q(get_satisfaction_company__startswith='http')],
}
}
class Command(BaseCommand):
"""
A generic command to run a task on *all* addons.
Add tasks to the reg dictionary.
A generic command to run a task on addons.
Add tasks to the tasks dictionary, providing a list of Q objects if you'd
like to filter the list down.
"""
option_list = BaseCommand.option_list + (
make_option('--task', action='store', type='string',
dest='task', help='Run task on all addons.'),
dest='task', help='Run task on the addons.'),
)
def handle(self, *args, **options):
pks = Addon.objects.values_list('pk', flat=True).order_by('id')
task = tasks.get(options.get('task'))
if not task:
raise ValueError('Unknown task: %s' % ', '.join(tasks.keys()))
pks = (Addon.objects.filter(*task['qs'])
.values_list('pk', flat=True)
.order_by('id'))
with establish_connection():
for chunk in chunked(pks, 100):
task.delay(chunk)
task['method'].delay(chunk)

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

@ -9,6 +9,7 @@ import elasticutils
import amo
from amo.decorators import write
from . import cron, search # Pull in tasks from cron.
from .forms import get_satisfaction
from .models import Addon, Preview
log = logging.getLogger('z.task')
@ -68,6 +69,21 @@ def update_appsupport(ids):
Addon.objects.invalidate(*addons)
@task
def fix_get_satisfaction(ids, **kw):
log.info('[%s@None] Fixing get satisfaction starting with id: %s...' %
(len(ids), ids[0]))
for addon in Addon.objects.filter(pk__in=ids):
url = addon.support_url
if url is None:
url = ''
else:
url = url.localized_string
(c, p) = get_satisfaction(url)
addon.update(get_satisfaction_company=c, get_satisfaction_product=p)
log.info('Updated get satisfaction for: %s' % addon.pk)
@task
def delete_preview_files(id, **kw):
log.info('[1@None] Removing preview with id of %s.' % id)

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

@ -7,6 +7,7 @@ import amo.tests
from addons import cron
from addons.models import Addon, AppSupport
from addons.utils import ReverseNameLookup
from addons.tasks import fix_get_satisfaction
from files.models import File, Platform
from versions.models import Version
@ -114,6 +115,32 @@ class TestLastUpdated(test_utils.TestCase):
app=amo.SEAMONKEY.id).count(), 1)
class TestGetSatisfactionFix(test_utils.TestCase):
def setUp(self):
self.addon = (Addon.objects.create(type=amo.ADDON_EXTENSION,
get_satisfaction_company='foo'))
def test_good_url(self):
self.addon.support_url = 'http://getsatisfaction.com/mozilla'
self.addon.save()
fix_get_satisfaction([self.addon.pk])
eq_(Addon.objects.get(pk=self.addon.pk).get_satisfaction_company,
'mozilla')
def test_bad_url(self):
self.addon.support_url = 'http://something.else.com/foo'
self.addon.save()
fix_get_satisfaction([self.addon.pk])
eq_(Addon.objects.get(pk=self.addon.pk).get_satisfaction_company, None)
def test_no_url(self):
self.addon.support_url = ''
self.addon.save()
fix_get_satisfaction([self.addon.pk])
eq_(Addon.objects.get(pk=self.addon.pk).get_satisfaction_company, None)
class TestHideDisabledFiles(test_utils.TestCase):
msg = 'Moving disabled file: %s => %s'