def make_observable( cls, exclude=['modified'], auto_subscriber_fields=['parent', 'owner', 'author', 'created_by']): """Adds Observable mix-in to the given class. Should be placed before every other signal connection for the given class. @param cls The object class which needs to be observed. @param exclude The list of fields to not track in changes. @param auto_subscriber_fields The list of fields which should be automatically added as subscribers. """ cls = get_model(cls) if not issubclass(cls, Observable): class _Observable(Observable): __change_exclude = exclude __subscriber_fields = auto_subscriber_fields _Observable.__name__ = cls.__name__ + str("Observable") setattr(sys.modules[__name__], _Observable.__name__, _Observable) cls.__bases__ += (_Observable, ) models.signals.pre_delete.connect(_cache_followers, sender=cls, dispatch_uid="%s_cache_followers" % cls.__name__) models.signals.post_save.connect(_notify_changes, sender=cls, dispatch_uid="%s_notify_changes" % cls.__name__)
def make_default_notifier( cls, exclude=['modified'], auto_subscriber_fields=['parent', 'owner', 'author', 'created_by']): """Makes the class observable and notify creation, changing and deletion. This implicitly adds Observable to "cls". @param cls The object class which should automatically post notifies. @param exclude The list of fields that should not be notified on changes. @param auto_subscriber_fields The list of fields which should be automatically added as subscribers. """ cls = get_model(cls) make_observable(cls, exclude, auto_subscriber_fields) models.signals.post_save.connect(notify_object_created, sender=cls, dispatch_uid="%s_created" % cls.__name__) post_change.connect(notify_object_changed, sender=cls, dispatch_uid="%s_changed" % cls.__name__) models.signals.m2m_changed.connect(notify_m2m_changed, sender=cls, dispatch_uid="%s_m2m_changed" % cls.__name__) models.signals.post_delete.connect(notify_object_deleted, sender=cls, dispatch_uid="%s_deleted" % cls.__name__)
def make_observable(cls, exclude=['modified'], auto_subscriber_fields=['parent', 'owner', 'author', 'created_by']): """Adds Observable mix-in to the given class. Should be placed before every other signal connection for the given class. @param cls The object class which needs to be observed. @param exclude The list of fields to not track in changes. @param auto_subscriber_fields The list of fields which should be automatically added as subscribers. """ cls = get_model(cls) if not issubclass(cls, Observable): class _Observable(Observable): __change_exclude = exclude __subscriber_fields = auto_subscriber_fields _Observable.__name__ = cls.__name__ + str("Observable") setattr(sys.modules[__name__], _Observable.__name__, _Observable) cls.__bases__ += (_Observable,) models.signals.pre_delete.connect(_cache_followers, sender=cls, dispatch_uid="%s_cache_followers" % cls.__name__) models.signals.post_save.connect(_notify_changes, sender=cls, dispatch_uid="%s_notify_changes" % cls.__name__)
def manage_bookmarks(cls, enabled=True): """Connects handlers for bookmarks management. This handler could be used to automatically create a related bookmark list on given model class instance creation. i.e.: >> manage_bookmarks(User) It will auto generate a bookmark list associated to each new User's instance. To disconnect: >> manage_bookmarks(User, False) """ cls = get_model(cls) cls_name = cls.__name__.lower() create_dispatch_uid = "create_%s_bookmarks" % cls_name delete_dispatch_uid = "delete_%s_bookmarks" % cls_name if enabled: post_save.connect(_create_bookmarks, cls, dispatch_uid=create_dispatch_uid) pre_delete.connect(_delete_bookmarks, cls, dispatch_uid=delete_dispatch_uid) else: post_save.disconnect(_create_bookmarks, cls, dispatch_uid=create_dispatch_uid) pre_delete.disconnect(_delete_bookmarks, cls, dispatch_uid=delete_dispatch_uid)
def make_notification_target(cls): """Adds NotificationTarget mix-in to the given class. @param cls The object class. """ cls = get_model(cls) if not issubclass(cls, NotificationTarget): cls.__bases__ += (NotificationTarget, )
def make_notification_target(cls): """Adds NotificationTarget mix-in to the given class. @param cls The object class. """ cls = get_model(cls) if not issubclass(cls, NotificationTarget): cls.__bases__ += (NotificationTarget,)
def raw_model_name(obj): """Returns the raw model name for the given instance. Example usage: {{ object|raw_model_name }} """ try: mk = get_model(obj) return mk.__name__.lower() except: pass return ""
def model_name_plural(obj): """Returns the pluralized model name for the given instance. Example usage: {{ object|model_name_plural }} """ try: mk = get_model(obj) return force_text(mk._meta.verbose_name_plural) except: pass return ""
def raw_model_name(obj): """Returns the raw model name for the given instance. Example usage: {{ object|raw_model_name }} """ try: mk = get_model(obj) return mk.__name__.lower() except: pass return ""
def model_name_plural(obj): """Returns the pluralized model name for the given instance. Example usage: {{ object|model_name_plural }} """ try: mk = get_model(obj) return force_text(mk._meta.verbose_name_plural) except: pass return ""
def manage_dashboard(cls, default_title=_("Dashboard")): """Connects handlers for dashboard management. This handler could be used to automatically create a related dashboard on given model class instance creation. i.e.: >> manage_dashboard(Project, _("Project's dashboard")) It will auto generate a dashboard associated to each new Project's instance with title "Project's dashboard". If no title is passed, default title will be used ("Dashboard"). """ cls = get_model(cls) global _dashboard_registry _dashboard_registry.manage_dashboard(cls, default_title)
def manage_dashboard(cls, default_title=_("Dashboard")): """Connects handlers for dashboard management. This handler could be used to automatically create a related dashboard on given model class instance creation. i.e.: >> manage_dashboard(Project, _("Project's dashboard")) It will auto generate a dashboard associated to each new Project's instance with title "Project's dashboard". If no title is passed, default title will be used ("Dashboard"). """ cls = get_model(cls) global _dashboard_registry _dashboard_registry.manage_dashboard(cls, default_title)
def make_default_notifier(cls, exclude=['modified'], auto_subscriber_fields=['parent', 'owner', 'author', 'created_by']): """Makes the class observable and notify creation, changing and deletion. This implicitly adds Observable to "cls". @param cls The object class which should automatically post notifies. @param exclude The list of fields that should not be notified on changes. @param auto_subscriber_fields The list of fields which should be automatically added as subscribers. """ cls = get_model(cls) make_observable(cls, exclude, auto_subscriber_fields) models.signals.post_save.connect(notify_object_created, sender=cls, dispatch_uid="%s_created" % cls.__name__) post_change.connect(notify_object_changed, sender=cls, dispatch_uid="%s_changed" % cls.__name__) models.signals.m2m_changed.connect(notify_m2m_changed, sender=cls, dispatch_uid="%s_m2m_changed" % cls.__name__) models.signals.post_delete.connect(notify_object_deleted, sender=cls, dispatch_uid="%s_deleted" % cls.__name__)