当前位置: 首页>>代码示例>>Python>>正文


Python EditThemeForm.is_valid方法代码示例

本文整理汇总了Python中olympia.addons.forms.EditThemeForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python EditThemeForm.is_valid方法的具体用法?Python EditThemeForm.is_valid怎么用?Python EditThemeForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在olympia.addons.forms.EditThemeForm的用法示例。


在下文中一共展示了EditThemeForm.is_valid方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestEditThemeForm

# 需要导入模块: from olympia.addons.forms import EditThemeForm [as 别名]
# 或者: from olympia.addons.forms.EditThemeForm import is_valid [as 别名]
class TestEditThemeForm(TestCase):
    fixtures = ['base/user_2519']

    def setUp(self):
        super(TestEditThemeForm, self).setUp()
        self.populate()
        self.request = mock.Mock()
        self.request.user = mock.Mock()
        self.request.user.groups_list = []
        self.request.user.username = 'swagyismymiddlename'
        self.request.user.name = 'Sir Swag A Lot'
        self.request.user.is_authenticated.return_value = True

    def populate(self):
        self.instance = Addon.objects.create(
            type=amo.ADDON_PERSONA, status=amo.STATUS_PUBLIC,
            slug='swag-overload', name='Bands Make Me Dance',
            description='tha description')
        self.cat = Category.objects.create(
            type=amo.ADDON_PERSONA, name='xxxx')
        self.instance.addoncategory_set.create(category=self.cat)
        self.license = amo.LICENSE_CC_BY.id
        self.theme = Persona.objects.create(
            persona_id=0, addon_id=self.instance.id, license=self.license,
            accentcolor='C0FFEE', textcolor='EFFFFF')
        Tag(tag_text='sw').save_tag(self.instance)
        Tag(tag_text='ag').save_tag(self.instance)

    def get_dict(self, **kw):
        data = {
            'accentcolor': '#C0FFEE',
            'category': self.cat.id,
            'license': self.license,
            'slug': self.instance.slug,
            'tags': 'ag, sw',
            'textcolor': '#EFFFFF',

            'name_en-us': unicode(self.instance.name),
            'description_en-us': unicode(self.instance.description),
        }
        data.update(**kw)
        return data

    def test_initial(self):
        self.form = EditThemeForm(None, request=self.request,
                                  instance=self.instance)

        # Compare form initial data with post data.
        eq_data = self.get_dict()
        for k in [k for k in self.form.initial.keys()
                  if k not in ['name', 'description']]:
            assert self.form.initial[k] == eq_data[k]

    def save_success(self):
        other_cat = Category.objects.create(type=amo.ADDON_PERSONA)
        self.data = {
            'accentcolor': '#EFF0FF',
            'category': other_cat.id,
            'license': amo.LICENSE_CC_BY_NC_SA.id,
            'slug': 'swag-lifestyle',
            'tags': 'ag',
            'textcolor': '#CACACA',

            'name_en-us': 'All Day I Dream About Swag',
            'description_en-us': 'ADIDAS',
        }
        self.form = EditThemeForm(self.data, request=self.request,
                                  instance=self.instance)

        # Compare form initial data with post data.
        eq_data = self.get_dict()
        for k in [k for k in self.form.initial.keys()
                  if k not in ['name', 'description']]:
            assert self.form.initial[k] == eq_data[k]

        assert self.form.data == self.data
        assert self.form.is_valid(), self.form.errors
        self.form.save()

    def test_success(self):
        self.save_success()
        self.instance = self.instance.reload()
        assert unicode(self.instance.persona.accentcolor) == (
            self.data['accentcolor'].lstrip('#'))
        assert self.instance.categories.all()[0].id == self.data['category']
        assert self.instance.persona.license == self.data['license']
        assert unicode(self.instance.name) == self.data['name_en-us']
        assert unicode(self.instance.description) == (
            self.data['description_en-us'])
        self.assertSetEqual(
            set(self.instance.tags.values_list('tag_text', flat=True)),
            {self.data['tags']})
        assert unicode(self.instance.persona.textcolor) == (
            self.data['textcolor'].lstrip('#'))

    def test_success_twice(self):
        """Form should be just fine when POSTing twice."""
        self.save_success()
        self.form.save()

#.........这里部分代码省略.........
开发者ID:tsl143,项目名称:addons-server,代码行数:103,代码来源:test_forms.py

示例2: TestEditThemeForm

# 需要导入模块: from olympia.addons.forms import EditThemeForm [as 别名]
# 或者: from olympia.addons.forms.EditThemeForm import is_valid [as 别名]
class TestEditThemeForm(TestCase):
    fixtures = ["base/user_2519"]

    def setUp(self):
        super(TestEditThemeForm, self).setUp()
        self.populate()
        self.request = mock.Mock()
        self.request.user = mock.Mock()
        self.request.user.groups_list = []
        self.request.user.username = "swagyismymiddlename"
        self.request.user.name = "Sir Swag A Lot"
        self.request.user.is_authenticated.return_value = True

    def populate(self):
        self.instance = Addon.objects.create(
            type=amo.ADDON_PERSONA,
            status=amo.STATUS_PUBLIC,
            slug="swag-overload",
            name="Bands Make Me Dance",
            description="tha description",
        )
        self.cat = Category.objects.create(type=amo.ADDON_PERSONA, name="xxxx")
        self.instance.addoncategory_set.create(category=self.cat)
        self.license = amo.LICENSE_CC_BY.id
        self.theme = Persona.objects.create(
            persona_id=0, addon_id=self.instance.id, license=self.license, accentcolor="C0FFEE", textcolor="EFFFFF"
        )
        Tag(tag_text="sw").save_tag(self.instance)
        Tag(tag_text="ag").save_tag(self.instance)

    def get_dict(self, **kw):
        data = {
            "accentcolor": "#C0FFEE",
            "category": self.cat.id,
            "license": self.license,
            "slug": self.instance.slug,
            "tags": "ag, sw",
            "textcolor": "#EFFFFF",
            "name_en-us": unicode(self.instance.name),
            "description_en-us": unicode(self.instance.description),
        }
        data.update(**kw)
        return data

    def test_initial(self):
        self.form = EditThemeForm(None, request=self.request, instance=self.instance)

        # Compare form initial data with post data.
        eq_data = self.get_dict()
        for k in [k for k in self.form.initial.keys() if k not in ["name", "description"]]:
            assert self.form.initial[k] == eq_data[k]

    def save_success(self):
        other_cat = Category.objects.create(type=amo.ADDON_PERSONA)
        self.data = {
            "accentcolor": "#EFF0FF",
            "category": other_cat.id,
            "license": amo.LICENSE_CC_BY_NC_SA.id,
            "slug": "swag-lifestyle",
            "tags": "ag",
            "textcolor": "#CACACA",
            "name_en-us": "All Day I Dream About Swag",
            "description_en-us": "ADIDAS",
        }
        self.form = EditThemeForm(self.data, request=self.request, instance=self.instance)

        # Compare form initial data with post data.
        eq_data = self.get_dict()
        for k in [k for k in self.form.initial.keys() if k not in ["name", "description"]]:
            assert self.form.initial[k] == eq_data[k]

        assert self.form.data == self.data
        assert self.form.is_valid(), self.form.errors
        self.form.save()

    def test_success(self):
        self.save_success()
        self.instance = self.instance.reload()
        assert unicode(self.instance.persona.accentcolor) == (self.data["accentcolor"].lstrip("#"))
        assert self.instance.categories.all()[0].id == self.data["category"]
        assert self.instance.persona.license == self.data["license"]
        assert unicode(self.instance.name) == self.data["name_en-us"]
        assert unicode(self.instance.description) == (self.data["description_en-us"])
        self.assertSetEqual(self.instance.tags.values_list("tag_text", flat=True), [self.data["tags"]])
        assert unicode(self.instance.persona.textcolor) == (self.data["textcolor"].lstrip("#"))

    def test_success_twice(self):
        """Form should be just fine when POSTing twice."""
        self.save_success()
        self.form.save()

    def test_name_unique(self):
        data = self.get_dict(**{"name_en-us": "Bands Make You Dance"})
        Addon.objects.create(type=amo.ADDON_PERSONA, status=amo.STATUS_PUBLIC, name=data["name_en-us"])
        self.form = EditThemeForm(data, request=self.request, instance=self.instance)
        assert not self.form.is_valid()
        assert self.form.errors == {"name": [("en-us", "This name is already in use. Please choose another.")]}

    def test_localize_name_description(self):
        data = self.get_dict(name_de="name_de", description_de="description_de")
#.........这里部分代码省略.........
开发者ID:lavish205,项目名称:olympia,代码行数:103,代码来源:test_forms.py


注:本文中的olympia.addons.forms.EditThemeForm.is_valid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。