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

Commit

Permalink
move to explicit signals (bug 821773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy McKay committed Apr 1, 2013
1 parent e19c94d commit 22ded22
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 16 deletions.
16 changes: 14 additions & 2 deletions apps/addons/models.py
Expand Up @@ -41,8 +41,8 @@
from reviews.models import Review from reviews.models import Review
import sharing.utils as sharing import sharing.utils as sharing
from stats.models import AddonShareCountTotal from stats.models import AddonShareCountTotal
from translations.fields import (TranslatedField, PurifiedField, from translations.fields import (LinkifiedField, PurifiedField, save_signal,
LinkifiedField, Translation) TranslatedField, Translation)
from translations.query import order_by_translation from translations.query import order_by_translation
from users.models import UserProfile, UserForeignKey from users.models import UserProfile, UserForeignKey
from users.utils import find_users from users.utils import find_users
Expand Down Expand Up @@ -1512,6 +1512,9 @@ def app_type(self):
# Not implemented for non-webapps. # Not implemented for non-webapps.
return '' return ''


dbsignals.pre_save.connect(save_signal, sender=Addon,
dispatch_uid='addon_translations')



class AddonDeviceType(amo.models.ModelBase): class AddonDeviceType(amo.models.ModelBase):
addon = models.ForeignKey(Addon) addon = models.ForeignKey(Addon)
Expand Down Expand Up @@ -1801,6 +1804,9 @@ def get_url_path(self):
return None return None
return reverse('browse.%s' % type) return reverse('browse.%s' % type)


dbsignals.pre_save.connect(save_signal, sender=AddonType,
dispatch_uid='addontype_translations')



class AddonUser(caching.CachingMixin, models.Model): class AddonUser(caching.CachingMixin, models.Model):
addon = models.ForeignKey(Addon) addon = models.ForeignKey(Addon)
Expand Down Expand Up @@ -1894,6 +1900,9 @@ def transformer(addons):
for addon in addons: for addon in addons:
addon.all_categories = cats.get(addon.id, []) addon.all_categories = cats.get(addon.id, [])


dbsignals.pre_save.connect(save_signal, sender=Category,
dispatch_uid='category_translations')



class CategorySupervisor(amo.models.ModelBase): class CategorySupervisor(amo.models.ModelBase):
category = models.ForeignKey(Category) category = models.ForeignKey(Category)
Expand Down Expand Up @@ -2007,6 +2016,9 @@ def thumbnail_size(self):
def image_size(self): def image_size(self):
return self.sizes.get('image', []) if self.sizes else [] return self.sizes.get('image', []) if self.sizes else []


dbsignals.pre_save.connect(save_signal, sender=Preview,
dispatch_uid='preview_translations')



class AppSupport(amo.models.ModelBase): class AppSupport(amo.models.ModelBase):
"""Cache to tell us if an add-on's current version supports an app.""" """Cache to tell us if an add-on's current version supports an app."""
Expand Down
8 changes: 6 additions & 2 deletions apps/bandwagon/models.py
Expand Up @@ -22,7 +22,7 @@
from addons.models import Addon, AddonRecommendation from addons.models import Addon, AddonRecommendation
from applications.models import Application from applications.models import Application
from stats.models import CollectionShareCountTotal from stats.models import CollectionShareCountTotal
from translations.fields import TranslatedField, LinkifiedField from translations.fields import LinkifiedField, save_signal, TranslatedField
from users.models import UserProfile from users.models import UserProfile
from versions import compare from versions import compare


Expand Down Expand Up @@ -376,10 +376,11 @@ def post_delete(sender, instance, **kwargs):


models.signals.post_save.connect(Collection.post_save, sender=Collection, models.signals.post_save.connect(Collection.post_save, sender=Collection,
dispatch_uid='coll.post_save') dispatch_uid='coll.post_save')
models.signals.pre_save.connect(save_signal, sender=Collection,
dispatch_uid='coll_translations')
models.signals.post_delete.connect(Collection.post_delete, sender=Collection, models.signals.post_delete.connect(Collection.post_delete, sender=Collection,
dispatch_uid='coll.post_delete') dispatch_uid='coll.post_delete')



class CollectionAddon(amo.models.ModelBase): class CollectionAddon(amo.models.ModelBase):
addon = models.ForeignKey(Addon) addon = models.ForeignKey(Addon)
collection = models.ForeignKey(Collection) collection = models.ForeignKey(Collection)
Expand All @@ -404,6 +405,9 @@ class CollectionFeature(amo.models.ModelBase):
class Meta(amo.models.ModelBase.Meta): class Meta(amo.models.ModelBase.Meta):
db_table = 'collection_features' db_table = 'collection_features'


models.signals.pre_save.connect(save_signal, sender=CollectionFeature,
dispatch_uid='collectionfeature_translations')



class CollectionPromo(amo.models.ModelBase): class CollectionPromo(amo.models.ModelBase):
collection = models.ForeignKey(Collection, null=True) collection = models.ForeignKey(Collection, null=True)
Expand Down
5 changes: 4 additions & 1 deletion apps/devhub/models.py
Expand Up @@ -23,7 +23,7 @@
from mkt.webapps.models import Webapp from mkt.webapps.models import Webapp
from reviews.models import Review from reviews.models import Review
from tags.models import Tag from tags.models import Tag
from translations.fields import TranslatedField from translations.fields import save_signal, TranslatedField
from users.helpers import user_link from users.helpers import user_link
from users.models import UserProfile from users.models import UserProfile
from versions.models import Version from versions.models import Version
Expand Down Expand Up @@ -74,6 +74,9 @@ def __unicode__(self):
def flush_urls(self): def flush_urls(self):
return ['*/developers*'] return ['*/developers*']


models.signals.pre_save.connect(save_signal, sender=HubPromo,
dispatch_uid='hubpromo_translations')



class HubEvent(amo.models.ModelBase): class HubEvent(amo.models.ModelBase):
name = models.CharField(max_length=255, default='') name = models.CharField(max_length=255, default='')
Expand Down
5 changes: 4 additions & 1 deletion apps/editors/models.py
Expand Up @@ -17,7 +17,7 @@
from amo.utils import cache_ns_key, send_mail from amo.utils import cache_ns_key, send_mail
from addons.models import Addon from addons.models import Addon
from editors.sql_model import RawSQLModel from editors.sql_model import RawSQLModel
from translations.fields import TranslatedField from translations.fields import save_signal, TranslatedField
from users.models import UserProfile from users.models import UserProfile
from versions.models import version_uploaded from versions.models import version_uploaded


Expand All @@ -41,6 +41,9 @@ class Meta:
def __unicode__(self): def __unicode__(self):
return unicode(self.name) return unicode(self.name)


models.signals.pre_save.connect(save_signal, sender=CannedResponse,
dispatch_uid='cannedresponses_translations')



class AddonCannedResponseManager(amo.models.ManagerBase): class AddonCannedResponseManager(amo.models.ManagerBase):
def get_query_set(self): def get_query_set(self):
Expand Down
6 changes: 5 additions & 1 deletion apps/localizers/models.py
Expand Up @@ -3,7 +3,7 @@
import caching.base import caching.base


import amo.models import amo.models
from translations.fields import PurifiedField from translations.fields import PurifiedField, save_signal




class L10nEventlog(caching.base.CachingMixin, models.Model): class L10nEventlog(caching.base.CachingMixin, models.Model):
Expand Down Expand Up @@ -35,3 +35,7 @@ class L10nSettings(amo.models.ModelBase):


class Meta: class Meta:
db_table = 'l10n_settings' db_table = 'l10n_settings'


models.signals.pre_save.connect(save_signal, sender=L10nSettings,
dispatch_uid='l10n_translations')
6 changes: 5 additions & 1 deletion apps/market/models.py
Expand Up @@ -4,7 +4,7 @@
from django.dispatch import receiver from django.dispatch import receiver
from django.utils import translation from django.utils import translation


from translations.fields import TranslatedField from translations.fields import save_signal, TranslatedField


import amo import amo
import amo.models import amo.models
Expand Down Expand Up @@ -110,6 +110,10 @@ def prices(self, provider=None):
return [({'currency': o.currency, 'amount': o.price}) return [({'currency': o.currency, 'amount': o.price})
for c, o in self.currencies()] for c, o in self.currencies()]


models.signals.pre_save.connect(save_signal, sender=Price,
dispatch_uid='price_translations')




class PriceCurrency(amo.models.ModelBase): class PriceCurrency(amo.models.ModelBase):
currency = models.CharField(max_length=10, currency = models.CharField(max_length=10,
Expand Down
10 changes: 7 additions & 3 deletions apps/reviews/models.py
Expand Up @@ -12,7 +12,7 @@
import amo.models import amo.models
from amo.helpers import shared_url from amo.helpers import shared_url
from amo.urlresolvers import reverse from amo.urlresolvers import reverse
from translations.fields import TranslatedField from translations.fields import save_signal, TranslatedField
from users.models import UserProfile from users.models import UserProfile


log = logging.getLogger('z.review') log = logging.getLogger('z.review')
Expand Down Expand Up @@ -115,8 +115,12 @@ def transformer(reviews):
user_ids[user.id].user = user user_ids[user.id].user = user




models.signals.post_save.connect(Review.post_save, sender=Review) models.signals.post_save.connect(Review.post_save, sender=Review,
models.signals.post_delete.connect(Review.post_delete, sender=Review) dispatch_uid='review_post_save')
models.signals.post_delete.connect(Review.post_delete, sender=Review,
dispatch_uid='review_post_delete')
models.signals.pre_save.connect(save_signal, sender=Review,
dispatch_uid='review_translations')




# TODO: translate old flags. # TODO: translate old flags.
Expand Down
17 changes: 16 additions & 1 deletion apps/translations/fields.py
Expand Up @@ -5,6 +5,7 @@
from django.utils import translation as translation_utils from django.utils import translation as translation_utils
from django.utils.translation.trans_real import to_language from django.utils.translation.trans_real import to_language


from .hold import add_translation, make_key, save_translations
from .models import Translation, PurifiedTranslation, LinkifiedTranslation from .models import Translation, PurifiedTranslation, LinkifiedTranslation
from .widgets import TransInput, TransTextarea from .widgets import TransInput, TransTextarea


Expand Down Expand Up @@ -155,7 +156,11 @@ def translation_from_string(self, instance, lang, string):
except AttributeError: except AttributeError:
# Create a brand new translation. # Create a brand new translation.
translation = self.model.new(string, lang) translation = self.model.new(string, lang)
save_on_signal(instance, translation)
# A new translation has been created and it might need to be saved.
# This adds the translation to the queue of translation that need
# to be saved for this instance.
add_translation(make_key(instance), translation)
return translation return translation


def translation_from_dict(self, instance, lang, dict_): def translation_from_dict(self, instance, lang, dict_):
Expand Down Expand Up @@ -276,3 +281,13 @@ def __contains__(self, item):


def zip(self): def zip(self):
return zip(self.locales, self.seq) return zip(self.locales, self.seq)


def save_signal(sender, instance, **kw):
"""
Use this signal on a model to iterate through all the translations added
to the hold queue and save them all. Hook this up to the pre_save signal
on the model.
"""
if not kw.get('raw'):
save_translations(make_key(instance))
51 changes: 51 additions & 0 deletions apps/translations/hold.py
@@ -0,0 +1,51 @@
from threading import local

from django.core.signals import request_finished

_to_save = local()


def add_translation(key, translation):
"""
Queue a translation that needs to be saved for a particular object. To
generate the key, call make_key.
"""
if not hasattr(_to_save, 'translations'):
_to_save.translations = {}

_to_save.translations.setdefault(key, [])
_to_save.translations[key].append(translation)


def clean_translations(sender, **kwargs):
"""
Removes all translations in the queue.
"""
if hasattr(_to_save, 'translations'):
_to_save.translations = {}


def make_key(obj):
"""Returns a key for this object."""
return id(obj)


def save_translations(key):
"""
For a given key, save all the translations. The key is used to ensure that
we only save the translations for the given object (and not all of them).
Once saved, they will be deleted.
"""
if not hasattr(_to_save, 'translations'):
return

for trans in _to_save.translations.get(key, []):
is_new = trans.autoid is None
trans.save(force_insert=is_new, force_update=not is_new)

if key in _to_save.translations:
del _to_save.translations[key]


# Ensure that on request completion, we flush out any unsaved translations.
request_finished.connect(clean_translations, dispatch_uid='clean_translations')
10 changes: 9 additions & 1 deletion apps/translations/tests/testapp/models.py
@@ -1,7 +1,8 @@
from django.db import models from django.db import models


import amo.models import amo.models
from translations.fields import TranslatedField, PurifiedField, LinkifiedField from translations.fields import (LinkifiedField, PurifiedField, save_signal,
TranslatedField)




class TranslatedModel(amo.models.ModelBase): class TranslatedModel(amo.models.ModelBase):
Expand All @@ -10,6 +11,9 @@ class TranslatedModel(amo.models.ModelBase):
default_locale = models.CharField(max_length=10) default_locale = models.CharField(max_length=10)
no_locale = TranslatedField(require_locale=False) no_locale = TranslatedField(require_locale=False)


models.signals.pre_save.connect(save_signal, sender=TranslatedModel,
dispatch_uid='testapp_translatedmodel')



class UntranslatedModel(amo.models.ModelBase): class UntranslatedModel(amo.models.ModelBase):
"""Make sure nothing is broken when a model doesn't have translations.""" """Make sure nothing is broken when a model doesn't have translations."""
Expand All @@ -20,3 +24,7 @@ class FancyModel(amo.models.ModelBase):
"""Mix it up with purified and linkified fields.""" """Mix it up with purified and linkified fields."""
purified = PurifiedField() purified = PurifiedField()
linkified = LinkifiedField() linkified = LinkifiedField()


models.signals.pre_save.connect(save_signal, sender=FancyModel,
dispatch_uid='testapp_fancymodel')
5 changes: 4 additions & 1 deletion apps/users/models.py
Expand Up @@ -25,7 +25,7 @@
import amo.models import amo.models
from access.models import Group, GroupUser from access.models import Group, GroupUser
from amo.urlresolvers import reverse from amo.urlresolvers import reverse
from translations.fields import PurifiedField from translations.fields import save_signal, PurifiedField
from translations.query import order_by_translation from translations.query import order_by_translation


log = commonware.log.getLogger('z.users') log = commonware.log.getLogger('z.users')
Expand Down Expand Up @@ -443,6 +443,9 @@ def has_preapproval_key(self):
""" """
return bool(getattr(self.get_preapproval(), 'paypal_key', '')) return bool(getattr(self.get_preapproval(), 'paypal_key', ''))


models.signals.pre_save.connect(save_signal, sender=UserProfile,
dispatch_uid='userprofile_translations')



@dispatch.receiver(models.signals.post_save, sender=UserProfile, @dispatch.receiver(models.signals.post_save, sender=UserProfile,
dispatch_uid='user.post_save') dispatch_uid='user.post_save')
Expand Down
9 changes: 7 additions & 2 deletions apps/versions/models.py
Expand Up @@ -22,8 +22,8 @@
from files import utils from files import utils
from files.models import File, Platform, cleanup_file from files.models import File, Platform, cleanup_file
from tower import ugettext as _ from tower import ugettext as _
from translations.fields import (TranslatedField, PurifiedField, from translations.fields import (LinkifiedField, PurifiedField, save_signal,
LinkifiedField) TranslatedField)
from users.models import UserProfile from users.models import UserProfile


from .compare import version_dict, version_int from .compare import version_dict, version_int
Expand Down Expand Up @@ -526,6 +526,8 @@ def clear_compatversion_cache_on_delete(sender, instance, **kw):




version_uploaded = django.dispatch.Signal() version_uploaded = django.dispatch.Signal()
models.signals.pre_save.connect(
save_signal, sender=Version, dispatch_uid='version_translations')
models.signals.post_save.connect( models.signals.post_save.connect(
update_status, sender=Version, dispatch_uid='version_update_status') update_status, sender=Version, dispatch_uid='version_update_status')
models.signals.post_save.connect( models.signals.post_save.connect(
Expand Down Expand Up @@ -577,6 +579,9 @@ class Meta:
def __unicode__(self): def __unicode__(self):
return unicode(self.name) return unicode(self.name)


models.signals.pre_save.connect(
save_signal, sender=License, dispatch_uid='version_translations')



class VersionComment(amo.models.ModelBase): class VersionComment(amo.models.ModelBase):
"""Editor comments for version discussion threads.""" """Editor comments for version discussion threads."""
Expand Down
3 changes: 3 additions & 0 deletions mkt/webapps/models.py
Expand Up @@ -34,6 +34,7 @@
from files.models import File, nfd_str, Platform from files.models import File, nfd_str, Platform
from files.utils import parse_addon, WebAppParser from files.utils import parse_addon, WebAppParser
from lib.crypto import packaged from lib.crypto import packaged
from translations.fields import save_signal
from versions.models import Version from versions.models import Version
from mkt.zadmin.models import FeaturedApp from mkt.zadmin.models import FeaturedApp


Expand Down Expand Up @@ -757,6 +758,8 @@ def app_type(self):


models.signals.post_save.connect(update_search_index, sender=Webapp, models.signals.post_save.connect(update_search_index, sender=Webapp,
dispatch_uid='mkt.webapps.index') dispatch_uid='mkt.webapps.index')
models.signals.pre_save.connect(save_signal, sender=Webapp,
dispatch_uid='webapp_translations')




@receiver(version_changed, dispatch_uid='update_cached_manifests') @receiver(version_changed, dispatch_uid='update_cached_manifests')
Expand Down

0 comments on commit 22ded22

Please sign in to comment.