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)
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)
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 - askbot.deps.livesettings db is disabled',self.group.key, self.key) return False
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)
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) 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) elif 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)
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) 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) elif 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)
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() settings = mgr[group] if settings is None: raise Http404() 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: request.user.message_set.create(message=e.message) 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, 'form': form, 'use_db': use_db })
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 = apply(self.update_callback, (current_value, new_value)) db_value = self.get_db_prep_save(new_value) try: s = self.get_setting(language_code) s.value = db_value except SettingNotSet: s = self.make_setting(db_value, language_code=language_code) 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() if self.localized: try: unlocalized_setting = find_setting( self.group.key, self.key) unlocalized_setting.delete() except SettingNotSet: pass signals.configuration_value_changed.send( self, old_value=current_value, new_value=new_value, setting=self, language_code=language_code) if self.clear_cache: cache.clear() return True else: log.debug( 'not updating setting %s.%s - askbot.deps.livesettings db is disabled', self.group.key, self.key) return False
def spaces_settings(request, template='livesettings/space_settings.html'): use_db, overrides = get_overrides(); print request.GET, request.POST mgr = ConfigurationSettings() settings = mgr['SPACE'] if use_db: if request.method=='GET' and request.GET.get('Create_or_Edit_Space'): try : instance=Space.objects.get(pk=request.GET['Create_or_Edit_Space']) selectform=SelectForm(initial = {'Create_or_Edit_Space': instance.pk }) thisform=SpaceForm(instance=instance) except KeyError: selectform=SelectForm(initial = {'Create_or_Edit_Space': 0 }) thisform=SpaceForm() elif request.method=='POST': try: # request.GET['Create_or_Edit_Space'] instance=Space.objects.get(pk=request.GET['Create_or_Edit_Space']) selectform=SelectForm(initial={'Create_or_Edit_Space': instance.pk }) if request.POST['Delete']: instance.delete() instance=None selectform=SelectForm(initial={'Create_or_Edit_Space': 0 }) thisform=SpaceForm(instance=instance) else: selectform=SelectForm(initial={'Create_or_Edit_Space': instance.pk }) thisform=SpaceForm(request.POST,request.FILES,instance=instance) except : instance=None selectform=SelectForm(initial = {'Create_or_Edit_Space': 0 }) thisform=SpaceForm(request.POST,request.FILES,instance=instance) if thisform.is_valid(): thisform.save() else: thisform=SpaceForm() selectform=SelectForm() else: form = None return render_to_response(template, { 'all_super_groups': mgr.get_super_groups(), 'group' : settings, 'select':selectform, 'form': thisform, 'use_db' : use_db }, context_instance=RequestContext(request))
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() settings = mgr[group] if settings is None: raise Http404() 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: request.user.message_set.create(message=e.message) 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, 'form': form, 'use_db': use_db })
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() 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 = 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 if cfg.update(value): # Give user feedback as to which settings were changed message = 'Updated %s on %s' % (cfg.key, cfg.group.key) request.user.message_set.create(message=message) 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, { 'all_super_groups': mgr.get_super_groups(), 'title': title, 'group': settings, 'form': form, 'use_db': use_db }, context_instance=RequestContext(request))
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() 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 = 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 if cfg.update(value): # Give user feedback as to which settings were changed message='Updated %s on %s' % (cfg.key, cfg.group.key) request.user.message_set.create(message = message) 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, { 'all_super_groups': mgr.get_super_groups(), 'title': title, 'group' : settings, 'form': form, 'use_db' : use_db }, context_instance=RequestContext(request))
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, sns: 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 grp.has_key(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", key, self.setting) raise (ae)
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 = apply(self.update_callback, (current_value, new_value)) db_value = self.get_db_prep_save(new_value) try: s = self.get_setting(language_code) s.value = db_value except SettingNotSet: s = self.make_setting(db_value, language_code=language_code) 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() if self.localized: try: unlocalized_setting = find_setting(self.group.key, self.key) unlocalized_setting.delete() except SettingNotSet: pass signals.configuration_value_changed.send( self, old_value=current_value, new_value=new_value, setting=self, language_code=language_code ) if self.clear_cache: cache.clear() return True else: log.debug("not updating setting %s.%s - askbot.deps.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() 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 = 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: if cfg.update(value): 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, e: messages.error(request, e.message) return HttpResponseRedirect(request.path) else: # Leave the form populated with current setting values #form = editor() form = forms.SettingsEditor(settings=settings)
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, sns: 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 grp.has_key(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", key, self.setting) raise(ae)
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() 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 = 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: if cfg.update(value): 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, e: messages.error(request, e.message) return HttpResponseRedirect(request.path) else: # Leave the form populated with current setting values #form = editor() form = forms.SettingsEditor(settings=settings)
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 as sns: 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 grp.has_key(key): val = grp[self.key] else: val = NOTSET except AttributeError as ae: log.error("Attribute error: %s", ae) log.error("%s: Could not get _value of %s", key, self.setting) raise (ae) except Exception as e: global _WARN log.error(e) if str(e).find("configuration_setting") > -1: if not _WARN.has_key('configuration_setting'): log.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: import traceback traceback.print_exc() log.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
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 as sns: 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 grp.has_key(key): val = grp[self.key] else: val = NOTSET except AttributeError as ae: log.error("Attribute error: %s", ae) log.error("%s: Could not get _value of %s", key, self.setting) raise(ae) except Exception as e: global _WARN log.error(e) if str(e).find("configuration_setting") > -1: if not _WARN.has_key('configuration_setting'): log.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: import traceback traceback.print_exc() log.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