def test_config_object_util(no_config_env): project = Project.objects.first() conf = ObjectConfig(project) assert conf.items() == conf.values() == conf.keys() == [] # keys are returned order by key other_dict = OrderedDict() other_dict["foo.a"] = "bar" other_dict["foo.b"] = dict(bar=23) other_dict["foo.c"] = [1, 2, 3] conf["foo.a"] = "bar" conf["foo.b"] = dict(bar=23) conf["foo.c"] = [1, 2, 3] assert conf.items() == other_dict.items() assert conf.values() == other_dict.values() assert conf.keys() == other_dict.keys() assert [x for x in conf] == other_dict.keys() assert all(x in conf for x in other_dict) assert all(conf[k] == v for k, v in other_dict.items()) assert all(conf.get(k) == v for k, v in other_dict.items()) assert conf.get("DOESNOTEXIST") is None assert conf.get("DOESNOTEXIST", "foo") == "foo" with pytest.raises(KeyError): conf["DOESNOTEXIST"] assert ObjectConfig(project).items() == other_dict.items() assert ObjectConfig(project).values() == other_dict.values() assert ObjectConfig(project).keys() == other_dict.keys() assert [x for x in ObjectConfig(project)] == other_dict.keys() assert all(x in ObjectConfig(project) for x in other_dict) assert all(ObjectConfig(project)[k] == v for k, v in other_dict.items()) assert all( ObjectConfig(project).get(k) == v for k, v in other_dict.items()) assert ObjectConfig(project).get("DOESNOTEXIST") is None assert ObjectConfig(project).get("DOESNOTEXIST", "foo") == "foo" with pytest.raises(KeyError): ObjectConfig(project)["DOESNOTEXIST"]
def test_config_object_util(): project = Project.objects.first() conf = ObjectConfig(project) assert conf.items() == conf.values() == conf.keys() == [] # keys are returned order by key other_dict = OrderedDict() other_dict["foo.a"] = "bar" other_dict["foo.b"] = dict(bar=23) other_dict["foo.c"] = [1, 2, 3] conf["foo.a"] = "bar" conf["foo.b"] = dict(bar=23) conf["foo.c"] = [1, 2, 3] assert conf.items() == other_dict.items() assert conf.values() == other_dict.values() assert conf.keys() == other_dict.keys() assert [x for x in conf] == other_dict.keys() assert all(x in conf for x in other_dict) assert all(conf[k] == v for k, v in other_dict.items()) assert all(conf.get(k) == v for k, v in other_dict.items()) assert conf.get("DOESNOTEXIST") is None assert conf.get("DOESNOTEXIST", "foo") == "foo" with pytest.raises(KeyError): conf["DOESNOTEXIST"] assert ObjectConfig(project).items() == other_dict.items() assert ObjectConfig(project).values() == other_dict.values() assert ObjectConfig(project).keys() == other_dict.keys() assert [x for x in ObjectConfig(project)] == other_dict.keys() assert all(x in ObjectConfig(project) for x in other_dict) assert all(ObjectConfig(project)[k] == v for k, v in other_dict.items()) assert all(ObjectConfig(project).get(k) == v for k, v in other_dict.items()) assert ObjectConfig(project).get("DOESNOTEXIST") is None assert ObjectConfig(project).get("DOESNOTEXIST", "foo") == "foo" with pytest.raises(KeyError): ObjectConfig(project)["DOESNOTEXIST"]
def serialize_config(self, info, item): config = ObjectConfig(item) for k, v in self.config: if k == "fs_mapping": mapping = config.get(v) or {} info[k] = mapping.get("default") else: info[k] = config.get(v) info["template_name"] = ( item.lang_mapper.get_upstream_code("templates"))
def save(self, response_url=None, commit=True): tp = self.instance initialize_from_templates = False if tp.id is None: initialize_from_templates = tp.can_be_inited_from_templates() tp = super(TranslationProjectForm, self).save(commit) project = tp.project config = ObjectConfig(project) mappings = config.get("pootle.core.lang_mapping", {}) mappings = dict((v, k) for k, v in mappings.iteritems()) if not self.cleaned_data["fs_code"]: if tp.language.code in mappings: del mappings[tp.language.code] else: mappings[tp.language.code] = self.cleaned_data["fs_code"] config["pootle.core.lang_mapping"] = dict( (v, k) for k, v in mappings.iteritems()) if initialize_from_templates: def _enqueue_job(): queue = get_queue('default') queue.enqueue(update_translation_project, tp, response_url) connection.on_commit(_enqueue_job) return tp
def save(self, response_url=None, commit=True): tp = self.instance initialize_from_templates = False if tp.id is None: initialize_from_templates = tp.can_be_inited_from_templates() tp = super(TranslationProjectForm, self).save(commit) project = tp.project config = ObjectConfig(project) mappings = config.get("pootle.core.lang_mapping", {}) mappings = dict((v, k) for k, v in mappings.iteritems()) if not self.cleaned_data["fs_code"]: if tp.language.code in mappings: del mappings[tp.language.code] else: mappings[tp.language.code] = self.cleaned_data["fs_code"] config["pootle.core.lang_mapping"] = dict( (v, k) for k, v in mappings.iteritems()) if initialize_from_templates: def _enqueue_job(): queue = get_queue('default') queue.enqueue( update_translation_project, tp, response_url) connection.on_commit(_enqueue_job) return tp
def delete_existing(self, tp, commit=True): config = ObjectConfig(tp.project) mapping = config.get("pootle.core.lang_mapping", {}) if tp.language.code in mapping: del mapping[tp.language.code] config["pootle.core.lang_mapping"] = mapping super(TranslationProjectFormSet, self).delete_existing( tp, commit=commit)
def delete_existing(self, tp, commit=True): config = ObjectConfig(tp.project) mapping = config.get("pootle.core.lang_mapping", {}) if tp.language.code in mapping: del mapping[tp.language.code] config["pootle.core.lang_mapping"] = mapping super(TranslationProjectFormSet, self).delete_existing(tp, commit=commit)
def serialize_config(self, info, item): config = ObjectConfig(item) for k, v in self.config: info[k] = config.get(v)