Esempio n. 1
0
def getsetting(user, name):
    # this makes sure it exists
    setting = register.get(name)
    if user.is_anonymous():
        return setting.default()
    try:
        obj = UserSetting.objects.get(name=name, user=user)
    except UserSetting.DoesNotExist:
        return setting.default()
    else:
        return setting.decode(obj.value)
Esempio n. 2
0
def setsetting(user, name, value):
    # this makes sure it exists
    setting = register.get(name)
    encoded = setting.encode(value)
    if value == setting.default():
        # setting is default
        # using filter to avoid problems when setting does not exist
        # TODO: check if 'filter' instead of 'get' is faster
        UserSetting.objects.filter(name=name, user=user).delete()
        return True  # succesfully saved (by deleting)
    try:
        # TODO: use update here instead
        obj = UserSetting.objects.get(name=name, user=user)
    except UserSetting.DoesNotExist:
        obj = UserSetting(name=name, user=user, value=encoded)
        obj.save()
    else:
        obj.value = encoded
        obj.save()
    return True  # succesfully saved new value
Esempio n. 3
0
def get_settings_form(settings):
    setting_objects = []
    for s in settings:
        if isinstance(s, Setting):
            setting_objects.append(s)
        elif isinstance(s, basestring):
            setting_objects.append(register.get(s))
        else:
            raise TypeError

    fields = {}
    for setting in settings:
        field = setting.formfield()
        if not field.help_text and setting.description:
            field.help_text = setting.description
        fields[setting.name] = field

    fields['settings'] = settings

    return type(
        'CustomSettingsForm',
        (SettingsForm,),
        fields,
    )