def create_or_update(self, instance, timestamp=None, tags="", source="INTERACTIVE", source_id=""): """ Create or update an Item from some instace. """ # If the instance hasn't already been saved, save it first. This # requires disconnecting the post-save signal that might be sent to # this function (otherwise we could get an infinite loop). if instance._get_pk_val() is None: try: dispatcher.disconnect(self.create_or_update, signal=signals.post_save, sender=type(instance)) except dispatcher.errors.DispatcherError: reconnect = False else: reconnect = True instance.save() if reconnect: dispatcher.connect(self.create_or_update, signal=signals.post_save, sender=type(instance)) # Make sure the item "should" be registered. if not getattr(instance, "jellyrollable", True): return # Check to see if the timestamp is being updated, possibly pulling # the timestamp from the instance. if hasattr(instance, "timestamp"): timestamp = instance.timestamp if timestamp is None: update_timestamp = False timestamp = datetime.datetime.now() else: update_timestamp = True # Ditto for tags. if not tags: for f in instance._meta.fields: if isinstance(f, TagField): tags = getattr(instance, f.attname) break # Create the Item object. ctype = ContentType.objects.get_for_model(instance) item, created = self.get_or_create( content_type = ctype, object_id = instance._get_pk_val(), defaults = dict( timestamp = timestamp, source = source, source_id = source_id, tags = tags, ) ) item.tags = tags item.source = source item.source_id = source_id if update_timestamp: item.timestamp = timestamp # Save and return the item. item.save() return item
def _save_new(self, instance): if not self.auto_rename: return try: image = getattr(instance, self.attname) if self.name_field: image = rename_by_field(image, '%s-%s-%s' \ % (instance.__class__.__name__, self.name, getattr(instance, self.name_field) ) ) else: # XXX this needs testing, maybe it can generate too long image names (max is 100) image = rename_by_field(image, '%s-%s-%s' \ % (instance.__class__.__name__, self.name, instance._get_pk_val() ) ) setattr(instance, self.attname, image) instance.save() finally: # cleanup dispatcher.disconnect(self._save_new, signals.post_save, sender=instance)
def _save_new(self, instance): if not self.auto_rename: return try: image = getattr(instance, self.attname) if self.name_field: image = rename_by_field(image, '%s-%s-%s' \ % (instance.__class__.__name__, self.name, getattr(instance, self.name_field) ) ) else: # XXX this needs testing, maybe it can generate too long image names (max is 100) image = rename_by_field(image, '%s-%s-%s' \ % (instance.__class__.__name__, self.name, instance._get_pk_val() ) ) setattr(instance, self.attname, image) instance.save() finally: # cleanup dispatcher.disconnect(self._save_new, signals.post_save, sender=instance)
# -*- coding: utf-8 -*- from django.dispatch import dispatcher from django.contrib.auth.management import create_superuser from django.contrib.auth import models as auth_app from django.db.models import signals # Para que no pregunte la creacion de usuario admin, # se crea automaticamente dispatcher.disconnect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
# -*- coding: utf-8 -*- from django.dispatch import dispatcher from django.contrib.auth.management import create_superuser from django.contrib.auth import models as auth_app from django.db.models import signals # Para que no pregunte la creacion de usuario admin, # se crea automaticamente dispatcher.disconnect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
super(MultilingualAddManipulator, self).do_html2python(new_data) class MultilingualChangeManipulator(django_manipulators.AutomaticChangeManipulator, MultilingualManipulatorMixin): def do_html2python(self, new_data): self.fix_translation_data(new_data) super(MultilingualChangeManipulator, self).do_html2python(new_data) def add_multilingual_manipulators(sender): """ A replacement for django.db.models.manipulators that installs multilingual manipulators for translatable models. It is supposed to be called from Translation.finish_multilingual_class. """ cls = sender if hasattr(cls, "is_translation_model"): cls.add_to_class("AddManipulator", MultilingualAddManipulator) cls.add_to_class("ChangeManipulator", MultilingualChangeManipulator) else: django_manipulators.add_manipulators(sender) from django.dispatch import dispatcher from django.db.models import signals dispatcher.disconnect(django_manipulators.add_manipulators, signal=signals.class_prepared) dispatcher.connect(add_multilingual_manipulators, signal=signals.class_prepared)
# from it. sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) # Must set this env var *before* importing any part of Django os.environ['DJANGO_SETTINGS_MODULE'] = 'taskhood.settings' import django.core.handlers.wsgi import django.core.signals import django.db import django.dispatch.dispatcher def log_exception(*args, **kwds): logging.exception('Exception in request:') # Log errors. dispatcher = django.dispatch.dispatcher.Signal() dispatcher.connect(log_exception, django.core.signals.got_request_exception) # Unregister the rollback event handler. dispatcher.disconnect(django.db._rollback_on_exception, django.core.signals.got_request_exception) def main(): # Create a Django application for WSGI. application = django.core.handlers.wsgi.WSGIHandler() # Run the WSGI CGI handler with that application. util.run_wsgi_app(application) if __name__ == '__main__': main()