Example #1
0
    def contribute_to_class(self, cls, name):
        """
        Add each of the names and fields in the ``fields`` attribute
        to the model the relationship field is applied to, and set up
        the related item save and delete signals for calling
        ``related_items_changed``.
        """
        for field in cls._meta.many_to_many:
            if isinstance(field, self.__class__):
                e = "Multiple %s fields are not supported (%s.%s, %s.%s)" % (
                    self.__class__.__name__, cls.__name__, cls.__name__, name,
                    field.name)
                raise ImproperlyConfigured(e)
        self.related_field_name = name
        super(BaseGenericRelation, self).contribute_to_class(cls, name)
        # Not applicable to abstract classes, and in fact will break.
        if not cls._meta.abstract:
            for (name_string, field) in self.fields.items():
                if "%s" in name_string:
                    name_string = name_string % name
                # In Django 1.6, add_to_class will be called on a
                # parent model's field more than once, so
                # contribute_to_class needs to be idempotent. We
                # don't call get_all_field_names() which fill the app
                # cache get_fields_with_model() is safe.
                try:
                    # Django >= 1.8
                    extant_fields = cls._meta._forward_fields_map
                except AttributeError:
                    # Django <= 1.7
                    extant_fields = (i.name for i in cls._meta.fields)
                if name_string in extant_fields:
                    continue
                if field.verbose_name is None:
                    field.verbose_name = self.verbose_name
                cls.add_to_class(name_string, copy(field))
            # Add a getter function to the model we can use to retrieve
            # the field/manager by name.
            getter_name = "get_%s_name" % self.__class__.__name__.lower()
            cls.add_to_class(getter_name, lambda self: name)

            def connect_save(sender):
                post_save.connect(self._related_items_changed, sender=sender)

            def connect_delete(sender):
                post_delete.connect(self._related_items_changed, sender=sender)

            lazy_model_ops.add(connect_save, self.rel.to)
            lazy_model_ops.add(connect_delete, self.rel.to)
Example #2
0
    def contribute_to_class(self, cls, name):
        """
        Add each of the names and fields in the ``fields`` attribute
        to the model the relationship field is applied to, and set up
        the related item save and delete signals for calling
        ``related_items_changed``.
        """
        for field in cls._meta.many_to_many:
            if isinstance(field, self.__class__):
                e = "Multiple %s fields are not supported (%s.%s, %s.%s)" % (
                    self.__class__.__name__, cls.__name__, cls.__name__,
                    name, field.name)
                raise ImproperlyConfigured(e)
        self.related_field_name = name
        super(BaseGenericRelation, self).contribute_to_class(cls, name)
        # Not applicable to abstract classes, and in fact will break.
        if not cls._meta.abstract:
            for (name_string, field) in self.fields.items():
                if "%s" in name_string:
                    name_string = name_string % name
                # In Django 1.6, add_to_class will be called on a
                # parent model's field more than once, so
                # contribute_to_class needs to be idempotent. We
                # don't call get_all_field_names() which fill the app
                # cache get_fields_with_model() is safe.
                try:
                    # Django >= 1.8
                    extant_fields = cls._meta._forward_fields_map
                except AttributeError:
                    # Django <= 1.7
                    extant_fields = (i.name for i in cls._meta.fields)
                if name_string in extant_fields:
                    continue
                if field.verbose_name is None:
                    field.verbose_name = self.verbose_name
                cls.add_to_class(name_string, copy(field))
            # Add a getter function to the model we can use to retrieve
            # the field/manager by name.
            getter_name = "get_%s_name" % self.__class__.__name__.lower()
            cls.add_to_class(getter_name, lambda self: name)

            def connect_save(sender):
                post_save.connect(self._related_items_changed, sender=sender)

            def connect_delete(sender):
                post_delete.connect(self._related_items_changed, sender=sender)

            lazy_model_ops.add(connect_save, self.rel.to)
            lazy_model_ops.add(connect_delete, self.rel.to)
Example #3
0
from django.db.models.signals import post_save
from mezzanine.accounts import get_profile_for_user
from mezzanine.conf import settings
from mezzanine.utils.models import lazy_model_ops


__all__ = ()


if getattr(settings, "AUTH_PROFILE_MODULE", None):

    # This will be called when class_prepared signal has been sent
    # for both the profile and user model.
    def wait_for_models(profile_model, user_model):

        def create_profile(sender, instance, created, **_):
            if created:
                try:
                    get_profile_for_user(instance)
                except DatabaseError:
                    # User creation in initial syncdb may have been triggered,
                    # while profile model is under migration management and
                    # doesn't exist yet. We close the connection so that it
                    # gets re-opened, allowing syncdb to continue and complete.
                    connection.close()

        post_save.connect(create_profile, sender=user_model, weak=False)

    lazy_model_ops.add(wait_for_models,
                       settings.AUTH_PROFILE_MODULE, settings.AUTH_USER_MODEL)
Example #4
0
from django.db import DatabaseError, connection
from django.db.models.signals import post_save
from mezzanine.accounts import get_profile_for_user
from mezzanine.conf import settings
from mezzanine.utils.models import lazy_model_ops

__all__ = ()

if getattr(settings, "AUTH_PROFILE_MODULE", None):

    # This will be called when class_prepared signal has been sent
    # for both the profile and user model.
    def wait_for_models(profile_model, user_model):
        def create_profile(sender, instance, created, **_):
            if created:
                try:
                    get_profile_for_user(instance)
                except DatabaseError:
                    # User creation in initial syncdb may have been triggered,
                    # while profile model is under migration management and
                    # doesn't exist yet. We close the connection so that it
                    # gets re-opened, allowing syncdb to continue and complete.
                    connection.close()

        post_save.connect(create_profile, sender=user_model, weak=False)

    lazy_model_ops.add(wait_for_models, settings.AUTH_PROFILE_MODULE,
                       settings.AUTH_USER_MODEL)