Пример #1
0
    def test_custom_warning(self):
        d = ImmutableList(range(10), warning="Object is immutable!")

        self.assertEqual(d[1], 1)

        # AttributeError: Object is immutable!
        with self.assertRaisesMessage(AttributeError, 'Object is immutable!'):
            d.__setitem__(1, 'test')
Пример #2
0
    def test_custom_warning(self):
        d = ImmutableList(range(10), warning="Object is immutable!")

        self.assertEqual(d[1], 1)

        # AttributeError: Object is immutable!
        with self.assertRaisesMessage(AttributeError, 'Object is immutable!'):
            d.__setitem__(1, 'test')
Пример #3
0
    def test_sort(self):
        d = ImmutableList(range(10))

        # AttributeError: ImmutableList object is immutable.
        with self.assertRaisesMessage(AttributeError, 'ImmutableList object is immutable.'):
            d.sort()

        self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)')
Пример #4
0
    def test_sort(self):
        d = ImmutableList(range(10))

        # AttributeError: ImmutableList object is immutable.
        with self.assertRaisesMessage(AttributeError, 'ImmutableList object is immutable.'):
            d.sort()

        self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)')
Пример #5
0
 def __init__(cls, class_name, bases, namespace):
     disused_field_names = ('is_staff', 'is_superuser')
     disused_fields = [
         f for f in cls._meta.fields if f.name in disused_field_names
     ]
     local_fields = list(cls._meta.local_fields)
     fields = list(cls._meta.fields)
     for field in disused_fields:
         # remove disused fields from `local_fields` and `fields`
         local_fields.remove(field)
         fields.remove(field)
     cls._meta.local_fields = ImmutableList(local_fields)
     cls._meta.fields = ImmutableList(fields)
Пример #6
0
 def __init__(
         self,
         name,
         description="",
         moderated=False,
         aliases=ImmutableList([]),
         addresses=ImmutableList([]),
 ):
     super().__init__()
     self.moderated = moderated
     self.name = name
     self.description = description
     self.aliases = aliases
     self.addresses = sorted(set(addresses))
Пример #7
0
 def parse_file_upload(self, META, post_data):
     """Return a tuple of (POST QueryDict, FILES MultiValueDict)."""
     self.upload_handlers = ImmutableList(
         self.upload_handlers,
         warning="You cannot alter upload handlers after the upload has been processed."
     )
     parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
     return parser.parse()
Пример #8
0
    def __call__(self, decorated):
        if os.environ.get("DJANGO_REMOVE_FIELD_DISABLED") is not None:
            return decorated
        decorated__init__ = decorated.__init__
        decorated__setattr__ = decorated.__setattr__

        def __setattr__(inner_self, name, value):
            if name in self.fieldnames:
                raise AttributeError(
                    f"Field '{decorated.__name__}.{name}' is deprecated and should"
                    " not be called anymore")
            return decorated__setattr__(inner_self, name, value)

        def __init__(innerself, *args, **kwargs):
            for fieldname, value in kwargs.items():
                if fieldname in self.fieldnames:
                    raise AttributeError(
                        f"Field '{decorated.__name__}.{fieldname}' is deprecated"
                        " and should not be set anymore")

            decorated__init__(innerself, *args, **kwargs)

        decorated.__init__ = __init__

        for field in decorated._meta.fields:
            if field.name in self.fieldnames and (not field.null
                                                  or not field.blank):
                raise ValueError(
                    f"Field '{decorated.__name__}.{field.name}' must set 'null' and"
                    " 'blank' to True")

        decorated._meta.concrete_fields = [
            field for field in decorated._meta.concrete_fields
            if field.name not in self.fieldnames
        ]
        decorated._meta.local_fields = [
            field for field in decorated._meta.local_fields
            if field.name not in self.fieldnames
        ]
        decorated._meta.fields = ImmutableList([
            field for field in decorated._meta.fields
            if field.name not in self.fieldnames
        ])
        decorated.__setattr__ = __setattr__
        return decorated
Пример #9
0
    def parse_file_upload(self, META, post_data):
        """Return a tuple of (POST QueryDict, FILES MultiValueDict)."""
        # self 是「请求对象」
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning=
            "You cannot alter upload handlers after the upload has been processed."
        )
        print('【django.http.request.HttpRequest.parse_file_upload】'
              '「请求对象」创建「请求表单解析对象」并调用其 parse 方法')

        # 此类定义在 django.http.multipartparser 模块中,其实例被称为「请求表单解析对象」
        # META 是包含请求头信息的字典对象
        # post_data 是 self ,也就是「请求对象」
        # self.upload_handlers 是元组,里面是一些文件上传相关的对象
        # self.encoding 是字符串或 None TODO
        parser = MultiPartParser(META, post_data, self.upload_handlers,
                                 self.encoding)
        # 调用「请求表单解析对象」的 parse 方法
        # 处理请求体中的表单数据和文件数据,生成两个类字典对象并返回
        return parser.parse()
Пример #10
0
def make_immutable_fields_list(name, data):
    return ImmutableList(data, warning=IMMUTABLE_WARNING % name)
Пример #11
0
def set_primary_key(model, field_name):
    pk = model._meta.get_field(field_name)
    model._meta.pk = pk
    model._meta.auto_field = pk
    model._meta.fields = ImmutableList(
        [f for f in model._meta.fields if not isinstance(f, AutoField)])