Esempio n. 1
0
    def update(self, value):
        use_db, overrides = get_overrides()

        if use_db:
            current_value = self.value

            new_value = self.to_python(value)
            if current_value != new_value:
                if self.update_callback:
                    new_value = apply(self.update_callback, (current_value, new_value))

                db_value = self.get_db_prep_save(new_value)

                try:
                    s = self.setting
                    s.value = db_value

                except SettingNotSet:
                    s = self.make_setting(db_value)

                if self.use_default and self.default == new_value:
                    if s.id:
                        log.info("Deleted setting %s.%s", self.group.key, self.key)
                        s.delete()
                else:
                    log.info("Updated setting %s.%s = %s", self.group.key, self.key, value)
                    s.save()

                signals.configuration_value_changed.send(self, old_value=current_value, new_value=new_value, setting=self)

                return True
        else:
            log.debug('not updating setting %s.%s - livesettings db is disabled',self.group.key, self.key)

        return False
Esempio n. 2
0
    def update(self, value):
        use_db, overrides = get_overrides()

        if use_db:
            current_value = self.value

            new_value = self.to_python(value)
            if current_value != new_value:
                if self.update_callback:
                    new_value = apply(self.update_callback, (current_value, new_value))

                db_value = self.get_db_prep_save(new_value)

                try:
                    s = self.setting
                    s.value = db_value

                except SettingNotSet:
                    s = self.make_setting(db_value)

                if self.use_default and self.default == new_value:
                    if s.id:
                        log.info("Deleted setting %s.%s", self.group.key, self.key)
                        s.delete()
                else:
                    log.info("Updated setting %s.%s = %s", self.group.key, self.key, value)
                    s.save()

                signals.configuration_value_changed.send(self, old_value=current_value, new_value=new_value, setting=self)

                return True
        else:
            log.debug('not updating setting %s.%s - livesettings db is disabled',self.group.key, self.key)

        return False
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,
    })
Esempio n. 4
0
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""
       
    siteid = _safe_get_siteid(site)
    setting = None
    
    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)
    
    if use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError, nce:
            if loading.app_cache_ready():
                try:
                    setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)
           
                    except LongSetting.DoesNotExist:
                        pass
            
                cache_set(ck, value=setting)
Esempio n. 5
0
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""
       
    siteid = _safe_get_siteid(site)
    setting = None
    
    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)
    
    if use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError, nce:
            if loading.app_cache_ready():
                try:
                    setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)
           
                    except LongSetting.DoesNotExist:
                        pass
            
                cache_set(ck, value=setting)
Esempio n. 6
0
    def _value(self):
        use_db, overrides = get_overrides()

        if not use_db:
            try:
                val = overrides[self.group.key][self.key]
            except KeyError:
                if self.use_default:
                    val = self.default
                else:
                    raise SettingNotSet('%s.%s is not in your LIVESETTINGS_OPTIONS' % (self.group.key, self.key))

        else:
            try:
                val = self.setting.value

            except SettingNotSet, sns:
                if self.use_default:
                    val = self.default
                    if overrides:
                        # maybe override the default
                        grp = overrides.get(self.group.key, {})
                        if grp.has_key(self.key):
                            val = grp[self.key]
                else:
                    val = NOTSET

            except AttributeError, ae:
                log.error("Attribute error: %s", ae)
                log.error("%s: Could not get _value of %s", self.key, self.setting)
                raise(ae)
Esempio n. 7
0
    def _value(self):
        global is_setting_initializing
        use_db, overrides = get_overrides()

        if not use_db:
            try:
                val = overrides[self.group.key][self.key]
            except KeyError:
                if self.use_default:
                    val = self.default
                else:
                    raise SettingNotSet('%s.%s is not in your LIVESETTINGS_OPTIONS' % (self.group.key, self.key))

        else:
            try:
                val = self.setting.value

            except SettingNotSet as sns:
                is_setting_initializing = False
                if self.use_default:
                    val = self.default
                    if overrides:
                        # maybe override the default
                        grp = overrides.get(self.group.key, {})
                        if self.key in grp:
                            val = grp[self.key]
                else:
                    val = NOTSET

            except AttributeError as ae:
                is_setting_initializing = False
                log.error("Attribute error: %s", ae)
                log.error("%s: Could not get _value of %s", self.key, self.setting)
                raise (ae)

            except Exception as e:
                global _WARN
                if is_setting_initializing and isinstance(e, DatabaseError) and str(e).find(
                        "livesettings_setting") > -1:
                    if 'livesettings_setting' not in _WARN:
                        log.warn(str(e).strip())
                        _WARN['livesettings_setting'] = True
                    log.warn('Error loading livesettings from table, OK if you are in syncdb or before it. ROLLBACK')
                    connection._rollback()

                    if self.use_default:
                        val = self.default
                    else:
                        raise ImproperlyConfigured("All settings used in startup must have defaults, %s.%s does not",
                                                   self.group.key, self.key)
                else:
                    is_setting_initializing = False
                    import traceback
                    traceback.print_exc()
                    log.error("Problem finding settings %s.%s, %s", self.group.key, self.key, e)
                    raise SettingNotSet("Startup error, couldn't load %s.%s" % (self.group.key, self.key))
            else:
                is_setting_initializing = False
        return val
Esempio n. 8
0
    def _value(self):
        global is_setting_initializing
        use_db, overrides = get_overrides()

        if not use_db:
            try:
                val = overrides[self.group.key][self.key]
            except KeyError:
                if self.use_default:
                    val = self.default
                else:
                    raise SettingNotSet('%s.%s is not in your LIVESETTINGS_OPTIONS' % (self.group.key, self.key))

        else:
            try:
                val = self.setting.value

            except SettingNotSet:
                is_setting_initializing = False
                if self.use_default:
                    val = self.default
                    if overrides:
                        # maybe override the default
                        grp = overrides.get(self.group.key, {})
                        if self.key in grp:
                            val = grp[self.key]
                else:
                    val = NOTSET

            except AttributeError as ae:
                is_setting_initializing = False
                log.error("Attribute error: %s", ae)
                log.error("%s: Could not get _value of %s", self.key, self.setting)
                raise(ae)

            except Exception as e:
                global _WARN
                if is_setting_initializing and isinstance(e, DatabaseError) and str(e).find("livesettings_setting") > -1:
                    if not 'livesettings_setting' in _WARN:
                        log.warn(str(e).strip())
                        _WARN['livesettings_setting'] = True
                    log.warn('Error loading livesettings from table, OK if you are in syncdb or before it. ROLLBACK')
                    connection._rollback()

                    if self.use_default:
                        val = self.default
                    else:
                        raise ImproperlyConfigured("All settings used in startup must have defaults, %s.%s does not", self.group.key, self.key)
                else:
                    is_setting_initializing = False
                    import traceback
                    traceback.print_exc()
                    log.error("Problem finding settings %s.%s, %s", self.group.key, self.key, e)
                    raise SettingNotSet("Startup error, couldn't load %s.%s" % (self.group.key, self.key))
            else:
                is_setting_initializing = False
        return val
Esempio n. 9
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
        })
Esempio n. 10
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
    })
Esempio n. 11
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

    if django.VERSION < (1, 4):
        ADMIN_MEDIA_PREFIX = djangosettings.ADMIN_MEDIA_PREFIX
    else:
        ADMIN_MEDIA_PREFIX = djangosettings.STATIC_URL + 'admin/'

    return render_to_response(template, {
        'title': title,
        'group' : group,
        'form': form,
        'use_db' : use_db,
        'ADMIN_MEDIA_PREFIX' : ADMIN_MEDIA_PREFIX,
        'DJANGO_PRE_12' : _pre_12()
    }, context_instance=RequestContext(request))
Esempio n. 12
0
def group_settings(request, group, template='livesettings/group_settings.html'):
    # Determine what set of settings this editor is used for
    try:
        message = request.session['message']
        del request.session['message']
    except:
        message = None
    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):
                        request.session['message'] = 'Contents Has Been Updated'
                        pass
                        # Give user feedback as to which settings were changed
                        #request.user.message_set.create(message='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,
        'message':message,
    }, context_instance=RequestContext(request))
Esempio n. 13
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))
Esempio n. 14
0
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""

    siteid = _safe_get_siteid(site)
    setting = None

    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)

    if use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError as nce:
            if hasattr(apps, 'ready'):
                app_cache_ready = apps.ready
            else:
                app_cache_ready = apps.app_cache_ready()

            if app_cache_ready:
                try:
                    setting = Setting.objects.get(site__id__exact=siteid,
                                                  key__exact=key,
                                                  group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(
                            site__id__exact=siteid,
                            key__exact=key,
                            group__exact=group)

                    except LongSetting.DoesNotExist:
                        pass

                cache_set(ck, value=setting)

    else:
        grp = overrides.get(group, None)
        if grp and key in grp:
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            log.debug('Returning overridden: %s', setting)

    if not setting:
        raise SettingNotSet(key, cachekey=ck)

    return setting
Esempio n. 15
0
    def update(self, value, language_code=None):
        use_db, overrides = get_overrides()

        if use_db:
            current_value = self.value

            new_value = self.to_python(value)
            if current_value != new_value:
                if self.update_callback:
                    new_value = self.update_callback(current_value, new_value)

                db_value = self.get_db_prep_save(new_value)

                try:
                    settings_ = self.get_setting(language_code)
                    settings_.value = db_value
                except SettingNotSet:
                    settings_ = self.make_setting(db_value, language_code=language_code)

                if self.use_default and self.default == new_value:
                    if settings_.id:
                        logger.info("Deleted setting %s.%s", self.group.key, self.key)
                        settings_.delete()
                else:
                    logger.info("Updated setting(id=%s) %s.%s = %s", settings_.id, self.group.key, self.key, value)
                    settings_.save()

                if self.localized:
                    try:
                        unlocalized_setting = find_setting(self.group.key, self.key)
                        unlocalized_setting.delete()
                    except SettingNotSet:
                        pass

                configuration_value_changed.send(
                    None, old_value=current_value, new_value=new_value, setting=self, language_code=language_code
                )

                if self.clear_cache:
                    cache.clear()

                return True
        else:
            logger.debug("not updating setting %s.%s - livesettings db is disabled", self.group.key, self.key)

        return False
Esempio n. 16
0
    def update(self, value, language_code=None):
        use_db, overrides = get_overrides()

        if use_db:
            current_value = self.value

            new_value = self.to_python(value)
            if current_value != new_value:
                if self.update_callback:
                    new_value = self.update_callback(current_value, new_value)

                db_value = self.get_db_prep_save(new_value)

                try:
                    settings_ = self.get_setting(language_code)
                    settings_.value = db_value
                except SettingNotSet:
                    settings_ = self.make_setting(db_value, language_code=language_code)

                if self.use_default and self.default == new_value:
                    if settings_.id:
                        logger.info("Deleted setting %s.%s", self.group.key, self.key)
                        settings_.delete()
                else:
                    logger.info("Updated setting(id=%s) %s.%s = %s", settings_.id, self.group.key, self.key, value)
                    settings_.save()

                if self.localized:
                    try:
                        unlocalized_setting = find_setting(self.group.key, self.key)
                        unlocalized_setting.delete()
                    except SettingNotSet:
                        pass

                configuration_value_changed.send(None, old_value=current_value, new_value=new_value,
                                                 setting=self, language_code=language_code)

                if self.clear_cache:
                    cache.clear()

                return True
        else:
            logger.debug('not updating setting %s.%s - livesettings db is disabled', self.group.key, self.key)

        return False
Esempio n. 17
0
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""

    siteid = _safe_get_siteid(site)
    setting = None

    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)

    if use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError as nce:
            if hasattr(apps, 'ready'):
                app_cache_ready = apps.ready
            else:
                app_cache_ready = apps.app_cache_ready()

            if app_cache_ready:
                try:
                    setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                    except LongSetting.DoesNotExist:
                        pass

                cache_set(ck, value=setting)

    else:
        grp = overrides.get(group, None)
        if grp and key in grp:
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            log.debug('Returning overridden: %s', setting)

    if not setting:
        raise SettingNotSet(key, cachekey=ck)

    return setting
Esempio n. 18
0
def find_setting(group, key, site=None):
    'Get a setting or longsetting by group and key, cache and return it.'

    site_id = _safe_get_site_id(site)
    setting = None

    use_db, overrides = get_overrides(site_id)
    ck = cache_key_get('Setting', site_id, group, key)

    grp = overrides.get(group, None)

    if grp and (key in grp):
        val = grp[key]
        setting = ImmutableSetting(key=key, group=group, value=val)
        logger.debug('Returning overridden: %s', setting)

    elif use_db:
        try:
            setting = cache_get(ck)
        except NotCachedError:
            model_classes = (Setting, LongSetting)
            for model_cls in model_classes:
                try:
                    setting = model_cls.objects.get(site__id=site_id, key=key, group=group)
                    break
                except model_cls.DoesNotExist:
                    pass

            cache_set(ck, value=setting)
    else:
        grp = overrides.get(group, None)
        if grp and key in grp:
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            logger.debug('Returning overridden: %s', setting)

    if setting is None:
        raise SettingNotSet(key, cachekey=ck)

    return setting
Esempio n. 19
0
def find_setting(group, key, site=None):
    "Get a setting or longsetting by group and key, cache and return it."

    site_id = _safe_get_site_id(site)
    setting = None

    use_db, overrides = get_overrides(site_id)
    ck = cache_key_get("Setting", site_id, group, key)

    grp = overrides.get(group, None)

    if grp and (key in grp):
        val = grp[key]
        setting = ImmutableSetting(key=key, group=group, value=val)
        logger.debug("Returning overridden: %s", setting)

    elif use_db:
        try:
            setting = cache_get(ck)
        except NotCachedError:
            model_classes = (Setting, LongSetting)
            for model_cls in model_classes:
                try:
                    setting = model_cls.objects.get(site__id=site_id, key=key, group=group)
                    break
                except model_cls.DoesNotExist:
                    pass

            cache_set(ck, value=setting)
    else:
        grp = overrides.get(group, None)
        if grp and key in grp:
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            logger.debug("Returning overridden: %s", setting)

    if setting is None:
        raise SettingNotSet(key, cachekey=ck)

    return setting
Esempio n. 20
0
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""

    siteid = _safe_get_siteid(site)
    setting = None

    backend = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)

    if backend.is_editable:
        setting = backend.get_value(group, key)
    else:
        grp = overrides.get(group, None)
        if grp and grp.has_key(key):
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            log.debug('Returning overridden: %s', setting)

    if not setting:
        raise SettingNotSet(key, cachekey=ck)

    return setting
Esempio n. 21
0
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""
       
    siteid = _safe_get_siteid(site)
    setting = None
    
    backend = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)
    
    if backend.is_editable:
        setting = backend.get_value(group, key)
    else:
        grp = overrides.get(group, None)
        if grp and grp.has_key(key):
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            log.debug('Returning overridden: %s', setting)
                
    if not setting:
        raise SettingNotSet(key, cachekey=ck)

    return setting
Esempio n. 22
0
    def _value(self):
        use_db, overrides = get_overrides()

        lang = get_language()

        key = self.key
        if self.localized:
            key += '_' + format_setting_name(lang)

        if not use_db:
            try:
                val = overrides[self.group.key][key]
            except KeyError:
                if self.use_default:
                    val = self.default
                else:
                    raise SettingNotSet('%s.%s is not in your LIVESETTINGS_OPTIONS' % (self.group.key, key))
        else:
            try:
                val = self.setting.value
            except SettingNotSet:
                if self.localized and lang == django_settings.LANGUAGE_CODE:
                    try:
                        unlocalized_setting = find_setting(self.group.key, self.key)
                        return unlocalized_setting.value
                    except SettingNotSet:
                        pass

                if self.use_default:
                    val = self.default
                    if overrides:
                        # maybe override the default
                        grp = overrides.get(self.group.key, {})
                        if key in grp:
                            val = grp[self.key]
                else:
                    val = NOTSET

            except AttributeError as ae:
                logger.error("Attribute error: %s", ae)
                logger.error("%s: Could not get _value of %s", key, self.setting)
                raise(ae)

            except Exception as e:
                global _WARN
                logger.error(e)
                if str(e).find("configuration_setting") > -1:
                    if 'configuration_setting' not in _WARN:
                        logger.warn('Error loading setting %s.%s from table, OK if you are in syncdb', self.group.key, key)
                        _WARN['configuration_setting'] = True

                    if self.use_default:
                        val = self.default
                    else:
                        raise ImproperlyConfigured("All settings used in startup must have defaults, %s.%s does not",
                                                   self.group.key, key)
                else:
                    traceback.print_exc()
                    logger.warn("Problem finding settings %s.%s, %s", self.group.key, key, e)
                    raise SettingNotSet("Startup error, couldn't load %s.%s" % (self.group.key, key))
        return val
Esempio n. 23
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)
Esempio n. 24
0
    def _value(self):
        use_db, overrides = get_overrides()

        lang = get_language()

        key = self.key
        if self.localized:
            key += "_" + format_setting_name(lang)

        if not use_db:
            try:
                val = overrides[self.group.key][key]
            except KeyError:
                if self.use_default:
                    val = self.default
                else:
                    raise SettingNotSet("%s.%s is not in your LIVESETTINGS_OPTIONS" % (self.group.key, key))
        else:
            try:
                val = self.setting.value
            except SettingNotSet:
                if self.localized and lang == django_settings.LANGUAGE_CODE:
                    try:
                        unlocalized_setting = find_setting(self.group.key, self.key)
                        return unlocalized_setting.value
                    except SettingNotSet:
                        pass

                if self.use_default:
                    val = self.default
                    if overrides:
                        # maybe override the default
                        grp = overrides.get(self.group.key, {})
                        if key in grp:
                            val = grp[self.key]
                else:
                    val = NOTSET

            except AttributeError as ae:
                logger.error("Attribute error: %s", ae)
                logger.error("%s: Could not get _value of %s", key, self.setting)
                raise (ae)

            except Exception as e:
                global _WARN
                logger.error(e)
                if str(e).find("configuration_setting") > -1:
                    if "configuration_setting" not in _WARN:
                        logger.warn(
                            "Error loading setting %s.%s from table, OK if you are in syncdb", self.group.key, key
                        )
                        _WARN["configuration_setting"] = True

                    if self.use_default:
                        val = self.default
                    else:
                        raise ImproperlyConfigured(
                            "All settings used in startup must have defaults, %s.%s does not", self.group.key, key
                        )
                else:
                    traceback.print_exc()
                    logger.warn("Problem finding settings %s.%s, %s", self.group.key, key, e)
                    raise SettingNotSet("Startup error, couldn't load %s.%s" % (self.group.key, key))
        return val