Exemple #1
0
    def __new__(cls, verbose_name=None, copy=True, app_label=None):
        # If not otherwise provided, set the module to where it was executed
        if '__module__' in cls.__dict__:
            module_name = cls.__dict__['__module__']
        else:
            module_name = sys._getframe(1).f_globals['__name__']

        attrs = [(k, v) for (k, v) in cls.__dict__.items()
                 if isinstance(v, Value)]
        if copy:
            attrs = [(k, v.copy()) for (k, v) in attrs]
        attrs.sort(key=lambda a: a[1])

        for _, attr in attrs:
            attr.creation_counter = Value.creation_counter
            Value.creation_counter += 1
            if not hasattr(attr, 'verbose_name'):
                attr.verbose_name = verbose_name
            if app_label:
                attr._app = app_label
            register_setting(attr)

        attr_dict = dict(attrs + [('__module__', module_name)])

        # A new class is created so descriptors work properly
        # object.__new__ is necessary here to avoid recursion
        group = object.__new__(type('Group', (cls, ), attr_dict))
        group._settings = attrs

        return group
Exemple #2
0
    def contribute_to_class(self, cls, name):
        # Override module_name and class_name of all registered settings
        for attr in self.__class__.__dict__.values():
            if isinstance(attr, Value):
                unregister_setting(attr)
                attr.module_name = cls.__module__
                attr.class_name = cls.__name__
                attr._app = cls._meta.app_label
                register_setting(attr)

        # Create permission for editing settings on the model
        permission = (
            'can_edit_%s_settings' % cls.__name__.lower(),
            'Can edit %s settings' % cls._meta.verbose_name_raw,
        )
        if permission not in cls._meta.permissions:
            # Add a permission for the setting editor
            try:
                cls._meta.permissions.append(permission)
            except AttributeError:
                # Permissions were supplied as a tuple, so preserve that
                cls._meta.permissions = tuple(cls._meta.permissions +
                                              (permission, ))
        # Django migrations runner cache properties
        if hasattr(cls._meta, 'original_attrs'):
            cls._meta.original_attrs['permissions'] = cls._meta.permissions

        # Finally, place the attribute on the class
        setattr(cls, name, GroupDescriptor(self, name))
Exemple #3
0
    def __new__(cls, copy=True):
        # If not otherwise provided, set the module to where it was executed
        if "__module__" in cls.__dict__:
            module_name = cls.__dict__["__module__"]
        else:
            module_name = sys._getframe(1).f_globals["__name__"]

        attrs = [(k, v) for (k, v) in cls.__dict__.items() if isinstance(v, Value)]
        if copy:
            attrs = [(k, v.copy()) for (k, v) in attrs]
        attrs.sort(lambda a, b: cmp(a[1], b[1]))

        for key, attr in attrs:
            attr.creation_counter = Value.creation_counter
            Value.creation_counter += 1
            register_setting(attr)

        attr_dict = dict(attrs + [("__module__", module_name)])

        # A new class is created so descriptors work properly
        # object.__new__ is necessary here to avoid recursion
        group = object.__new__(type("Group", (cls,), attr_dict))
        group._settings = attrs

        from django.contrib.auth.models import Permission

        return group
    def contribute_to_class(self, cls, name):
        # Override module_name and class_name of all registered settings
        for attr in self.__class__.__dict__.values():
            if isinstance(attr, Value):
                unregister_setting(attr)
                attr.module_name = cls.__module__
                attr.class_name = cls.__name__
                attr._app = cls._meta.app_label
                register_setting(attr)

        # Create permission for editing settings on the model
        permission = (
            'can_edit_%s_settings' % cls.__name__.lower(),
            'Can edit %s settings' % cls._meta.verbose_name_raw,
        )
        if permission not in cls._meta.permissions:
            # Add a permission for the setting editor
            try:
                cls._meta.permissions.append(permission)
            except AttributeError:
                # Permissions were supplied as a tuple, so preserve that
                cls._meta.permissions = tuple(cls._meta.permissions + (permission,))
        # Django migrations runner cache properties
        if hasattr(cls._meta, 'original_attrs'):
            cls._meta.original_attrs['permissions'] = cls._meta.permissions

        # Finally, place the attribute on the class
        setattr(cls, name, GroupDescriptor(self, name))
    def __new__(cls, verbose_name=None, copy=True, app_label=None):
        # If not otherwise provided, set the module to where it was executed
        if '__module__' in cls.__dict__:
            module_name = cls.__dict__['__module__']
        else:
            module_name = sys._getframe(1).f_globals['__name__']

        attrs = [(k, v) for (k, v) in cls.__dict__.items() if isinstance(v, Value)]
        if copy:
            attrs = [(k, v.copy()) for (k, v) in attrs]
        attrs.sort(key=lambda a: a[1])

        for _, attr in attrs:
            attr.creation_counter = Value.creation_counter
            Value.creation_counter += 1
            if not hasattr(attr, 'verbose_name'):
                attr.verbose_name = verbose_name
            if app_label:
                attr._app = app_label
            register_setting(attr)

        attr_dict = dict(attrs + [('__module__', module_name)])

        # A new class is created so descriptors work properly
        # object.__new__ is necessary here to avoid recursion
        group = object.__new__(type('Group', (cls,), attr_dict))
        group._settings = attrs

        return group
Exemple #6
0
    def __new__(cls, copy=True):
        # If not otherwise provided, set the module to where it was executed
        if '__module__' in cls.__dict__:
            module_name = cls.__dict__['__module__']
        else:
            module_name = sys._getframe(1).f_globals['__name__']

        attrs = [(k, v) for (k, v) in cls.__dict__.items() if isinstance(v, Value)]
        if copy:
            attrs = [(k, v.copy()) for (k, v) in attrs]
        attrs.sort(lambda a, b: cmp(a[1], b[1]))

        for key, attr in attrs:
            attr.creation_counter = Value.creation_counter
            Value.creation_counter += 1
            register_setting(attr)

        attr_dict = dict(attrs + [('__module__', module_name)])

        # A new class is created so descriptors work properly
        # object.__new__ is necessary here to avoid recursion
        group = object.__new__(type('Group', (cls,), attr_dict))
        group._settings = attrs

        from django.contrib.auth.models import Permission

        return group
Exemple #7
0
    def contribute_to_class(self, cls, name):
        # Override module_name and class_name of all registered settings
        for attr in self.__class__.__dict__.values():
            if isinstance(attr, Value):
                attr.module_name = cls.__module__
                attr.class_name = cls.__name__
                register_setting(attr)

        # Create permission for editing settings on the model
        permission = (
            "can_edit_%s_settings" % cls.__name__.lower(),
            "Can edit %s settings" % cls._meta.verbose_name_raw,
        )
        if permission not in cls._meta.permissions:
            # Add a permission for the setting editor
            try:
                cls._meta.permissions.append(permission)
            except AttributeError:
                # Permissions were supplied as a tuple, so preserve that
                cls._meta.permissions = tuple(cls._meta.permissions + (permission,))

        # Finally, place the attribute on the class
        setattr(cls, name, GroupDescriptor(self, name))