在模型django中实现每个字段隐私的最佳方式

时间:2015-05-28 06:44:45

标签: python django django-permissions

我正在尝试在模型的字段级别为用户提供隐私设置。因此,用户可以决定他想要显示哪些数据以及他想隐藏哪些数据。

示例:

class Foo(models.Model):
    user = models.OneToOneField("auth.User")
    telephone_number = models.CharField(blank=True, null=True, max_length=10)
    image = models.ImageField(upload_to=get_photo_storage_path, null=True, blank=True)

我想为用户提供一个选项,以选择他想要显示的字段以及他不显示的字段。 考虑用户不想显示telephone_number,所以他应该有选择。

哪种方法最好?

3 个答案:

答案 0 :(得分:2)

您可以在模型中创建 CommaSeparatedIntegerField 字段,并使用它来存储用户想要隐藏的field_names(表示field_name的整数)列表。

您可以在models.py中创建field_names和整数之间的映射作为常量。并检查用户已检查过的那些field_names。

示例映射:

FIELD_NAME_CHOICES = (
    (1, Foo._meta.get_field('telephone_number')),
    (2, Foo._meta.get_field('name')),
    .
    .
)

请查看以下链接以获取参考https://docs.djangoproject.com/en/1.8/ref/models/fields/#commaseparatedintegerfield

答案 1 :(得分:0)

非常明显的简单愚蠢的解决方案是为每个字段添加一个布尔“show_xxx”,即:

class Foo(models.Model):
    user = models.OneToOneField("auth.User")
    telephone_number = models.CharField(blank=True, null=True, max_length=10)
    show_telephone_number = models.BooleanField(default=True)
    image = models.ImageField(upload_to=get_photo_storage_path, null=True, blank=True)
    show_image = models.BooleanField(default=True)

然后在模板中检查show_xxx字段的值:

{% if foo.telephone_number and foo.show_telephone_number %}
  <p>Telephone number: {{ foo.telephone_number }}</p>
{% endif %}

等...

当然你也可以使用一个整数字段和旧的位掩码技巧:

class Foo(models.Model):
    user = models.OneToOneField("auth.User")
    telephone_number = models.CharField(blank=True, null=True, max_length=10)
    image = models.ImageField(upload_to=get_photo_storage_path, null=True, blank=True)

    foo = models.TextField(blank=True)

    perms = models.IntegerField(default=0)
    SHOW_TEL = 1
    SHOW_IMG = 2
    SHOW_FOO = 4

    def _show(self, flag):
        return (self.perms & flag) == flag

    def show_telephone_number(self):
        return self._show(self.SHOW_TEL)

    def show_image(self):
        return self._show(self.SHOW_IMG)

    def show_foo(self):
        return self._show(self.SHOW_FOO)

但我不确定这真的是“优化”......你必须手动处理编辑表格中的复选框等。

答案 2 :(得分:0)

您也可以使用MultiSelectField ...下面的代码来自Django Snippets,所以请不要归功于我,因为我只是分享别人的作品!

class MultiSelectField(models.CharField):
""" Choice values can not contain commas. """

def __init__(self, *args, **kwargs):
    self.max_choices = kwargs.pop('max_choices', None)
    super(MultiSelectField, self).__init__(*args, **kwargs)
    self.max_length = get_max_length(self.choices, self.max_length)
    self.validators[0] = MaxValueMultiFieldValidator(self.max_length)
    if self.max_choices is not None:
        self.validators.append(MaxChoicesValidator(self.max_choices))

@property
def flatchoices(self):
    return None

def get_choices_default(self):
    return self.get_choices(include_blank=False)

def get_choices_selected(self, arr_choices):
    choices_selected = []
    for choice_selected in arr_choices:
        choices_selected.append(string_type(choice_selected[0]))
    return choices_selected

def value_to_string(self, obj):
    value = self._get_val_from_obj(obj)
    return self.get_prep_value(value)

def validate(self, value, model_instance):
    arr_choices = self.get_choices_selected(self.get_choices_default())
    for opt_select in value:
        if (opt_select not in arr_choices):
            if django.VERSION[0] == 1 and django.VERSION[1] >= 6:
                raise ValidationError(self.error_messages['invalid_choice'] % {"value": value})
            else:
                raise ValidationError(self.error_messages['invalid_choice'] % value)

def get_default(self):
    default = super(MultiSelectField, self).get_default()
    if isinstance(default, (int, long)):
        default = string_type(default)
    return default

def formfield(self, **kwargs):
    defaults = {'required': not self.blank,
                'label': capfirst(self.verbose_name),
                'help_text': self.help_text,
                'choices': self.choices,
                'max_length': self.max_length,
                'max_choices': self.max_choices}
    if self.has_default():
        defaults['initial'] = self.get_default()
    defaults.update(kwargs)
    return MultiSelectFormField(**defaults)

def get_prep_value(self, value):
    return '' if value is None else ",".join(value)

def to_python(self, value):
    if value:
        return value if isinstance(value, list) else value.split(',')

def contribute_to_class(self, cls, name):
    super(MultiSelectField, self).contribute_to_class(cls, name)
    if self.choices:
        def get_list(obj):
            fieldname = name
            choicedict = dict(self.choices)
            display = []
            if getattr(obj, fieldname):
                for value in getattr(obj, fieldname):
                    item_display = choicedict.get(value, None)
                    if item_display is None:
                        try:
                            item_display = choicedict.get(int(value), value)
                        except (ValueError, TypeError):
                            item_display = value
                    display.append(string_type(item_display))
            return display

        def get_display(obj):
            return ", ".join(get_list(obj))

        setattr(cls, 'get_%s_list' % self.name, get_list)
        setattr(cls, 'get_%s_display' % self.name, get_display)

MultiSelectField = add_metaclass(models.SubfieldBase)(MultiSelectField)