def group_settings(request, group, template='livesettings/group_settings.html'):
    # Determine what set of settings this editor is used for

    use_db, overrides = get_overrides();

    mgr = ConfigurationSettings()
    if group is None:
        settings = mgr
        title = 'Site settings'
    else:
        settings = mgr[group]
        title = settings.name
        log.debug('title: %s', title)

    if use_db:
        # Create an editor customized for the current user
        # editor = forms.customized_editor(settings)

        if request.method == 'POST':
            # Populate the form with user-submitted data
            data = request.POST.copy()
            form = forms.SettingsEditor(data, settings=settings)
            if form.is_valid():
                form.full_clean()
                for name, value in list(form.cleaned_data.items()):
                    group, key = name.split('__')
                    cfg = mgr.get_config(group, key)
                    from livesettings.values import ImageValue
                    if isinstance(cfg, ImageValue):
                        if request.FILES and name in request.FILES:
                            value = request.FILES[name]
                        else:
                            continue

                    try:
                        if cfg.update(value):
                            # Give user feedback as to which settings were changed
                            messages.add_message(request, messages.INFO,
                                                 'Updated %s on %s' % (cfg.key, cfg.group.key))
                    except Exception as e:
                        log.exception(f'failed to save setting {name}:={value}')
                        request.user.message_set.create(message=str(e))

                return HttpResponseRedirect(request.path)
        else:
            # Leave the form populated with current setting values
            # form = editor()
            form = forms.SettingsEditor(settings=settings)
    else:
        form = None

    return render(request, template, {
        'all_super_groups': mgr.get_super_groups(),
        'page_class': 'settings',
        'title': title,
        'settings_group': settings,
        'group': group,
        'form': form,
        'use_db': use_db,
    })
Ejemplo n.º 2
0
def group_settings(request,
                   group,
                   template='livesettings/group_settings.jinja'):
    # Determine what set of settings this editor is used for

    use_db, overrides = get_overrides()

    mgr = ConfigurationSettings()

    settings = mgr[group]
    title = settings.name
    log.debug('title: %s', title)

    if use_db:
        # Create an editor customized for the current user
        # editor = forms.customized_editor(settings)
        if request.method == 'POST':
            # Populate the form with user-submitted data
            data = request.POST.copy()
            form = forms.SettingsEditor(data, request.FILES, settings=settings)
            if form.is_valid():
                for name, value in form.cleaned_data.items():
                    group, key, lang = name.split('__')
                    cfg = mgr.get_config(group, key)

                    if isinstance(cfg, ImageValue):
                        if request.FILES and name in request.FILES:
                            value = request.FILES[name]
                        else:
                            continue

                    try:
                        cfg.update(value, lang)
                        # message='Updated %s on %s' % (cfg.key, cfg.group.key)
                        # messages.success(request, message)
                        # the else if for the settings that are not updated.
                    except Exception as e:
                        traceback.print_exc()
                        request.user.message_set.create(
                            message=six.text_type(e))

                return redirect(request.path)
        else:
            # Leave the form populated with current setting values
            # form = editor()
            form = forms.SettingsEditor(settings=settings)
    else:
        form = None

    return render(
        request, template, {
            'all_super_groups': mgr.get_super_groups(),
            'title': title,
            'settings_group': settings,
            'form': form,
            'use_db': use_db
        })
Ejemplo n.º 3
0
def group_settings(request,
                   group,
                   template='livesettings/group_settings.html'):
    # Determine what set of settings this editor is used for

    use_db, overrides = get_overrides()

    mgr = ConfigurationSettings()
    if group is None:
        settings = mgr
        title = 'Site settings'
    else:
        settings = mgr[group]
        title = settings.name
        log.debug('title: %s', title)

    if use_db:
        # Create an editor customized for the current user
        #editor = forms.customized_editor(settings)

        if request.method == 'POST':
            # Populate the form with user-submitted data
            data = request.POST.copy()
            form = forms.SettingsEditor(data, settings=settings)
            if form.is_valid():
                form.full_clean()
                for name, value in form.cleaned_data.items():
                    group, key = name.split('__')
                    cfg = mgr.get_config(group, key)
                    if cfg.update(value):

                        # Give user feedback as to which settings were changed
                        messages.add_message(
                            request, messages.INFO,
                            'Updated %s on %s' % (cfg.key, cfg.group.key))

                return HttpResponseRedirect(request.path)
        else:
            # Leave the form populated with current setting values
            #form = editor()
            form = forms.SettingsEditor(settings=settings)
    else:
        form = None

    return render_to_response(template, {
        'title': title,
        'group': group,
        'form': form,
        'use_db': use_db,
        'DJANGO_PRE_12': _pre_12()
    },
                              context_instance=RequestContext(request))
Ejemplo n.º 4
0
def group_settings(request, group, template='livesettings/group_settings.html'):
    # Determine what set of settings this editor is used for

    use_db, overrides = get_overrides();

    mgr = ConfigurationSettings()
    if group is None:
        settings = mgr
        title = 'Site settings'
    else:
        settings = mgr[group]
        title = settings.name
        log.debug('title: %s', title)

    try:
        setting_version = Setting.objects.latest('version').version
    except Setting.DoesNotExist:
        setting_version = 1
    try:
        long_setting_version = LongSetting.objects.latest('version').version
    except LongSetting.DoesNotExist:
        long_setting_version = 1

    current_version = max(setting_version, long_setting_version)

    if use_db:
        # Create an editor customized for the current user
        # editor = forms.customized_editor(settings)

        if request.method == 'POST':
            # Populate the form with user-submitted data
            data = request.POST.copy()
            version = int(data.pop('version')[0])
            form = forms.SettingsEditor(data, settings=settings)
            if version == current_version:
                if form.is_valid():
                    form.full_clean()
                    with transaction.atomic():
                        for name, value in form.cleaned_data.items():
                            group, key = name.split('__')
                            cfg = mgr.get_config(group, key)
                            if cfg.update(value):

                                # Give user feedback as to which settings were changed
                                messages.add_message(request, messages.INFO, 'Updated %s on %s' % (cfg.key, cfg.group.key))
                        Setting.objects.update(version=current_version + 1)
                        LongSetting.objects.update(version=current_version + 1)

                    return HttpResponseRedirect(request.path)
            else:
                messages.add_message(
                    request, messages.ERROR,
                    'You are on incorrect version of settings. Please refresh the page before updating settings.'
                )
                current_version = version
        else:
            # Leave the form populated with current setting values
            # form = editor()
            form = forms.SettingsEditor(settings=settings)
    else:
        form = None

    context = {
        'title': title,
        'group' : group,
        'form': form,
        'use_db' : use_db,
        'version': current_version,
    }
    return render(request, template, context=context)