class SeparatedValuesFieldTestCase(TestCase): def setUp(self): self.field = SeparatedValuesField(forms.EmailField) def test_email_field(self): eq_(self.field.clean(u'[email protected], [email protected]'), u'[email protected], [email protected]') def test_email_field_w_empties(self): eq_(self.field.clean(u'[email protected],, \n,[email protected]'), u'[email protected], [email protected]') def test_email_validation_error(self): with self.assertRaises(exceptions.ValidationError): self.field.clean(u'e') with self.assertRaises(exceptions.ValidationError): self.field.clean(u'[email protected], [email protected], e') def test_url_field(self): field = SeparatedValuesField(forms.URLField) eq_(field.clean(u'http://hy.fr/,,http://yo.lo'), u'http://hy.fr/, http://yo.lo') def test_alt_separator(self): self.field = SeparatedValuesField(forms.EmailField, separator='#') eq_(self.field.clean(u'[email protected]#[email protected]'), u'[email protected], [email protected]')
class SeparatedValuesFieldTestCase(amo.tests.TestCase): def setUp(self): self.field = SeparatedValuesField(forms.EmailField) def test_email_field(self): eq_(self.field.clean(u'[email protected], [email protected]'), u'[email protected], [email protected]') def test_email_field_w_empties(self): eq_(self.field.clean(u'[email protected],, \n,[email protected]'), u'[email protected], [email protected]') def test_email_validation_error(self): with self.assertRaises(exceptions.ValidationError): self.field.clean(u'e') with self.assertRaises(exceptions.ValidationError): self.field.clean(u'[email protected], [email protected], e') def test_url_field(self): field = SeparatedValuesField(forms.URLField) eq_(field.clean(u'http://hy.fr/,,http://yo.lo'), u'http://hy.fr/, http://yo.lo/') def test_alt_separator(self): self.field = SeparatedValuesField(forms.EmailField, separator='#') eq_(self.field.clean(u'[email protected]#[email protected]'), u'[email protected], [email protected]')
def test_alt_separator(self): self.field = SeparatedValuesField(forms.EmailField, separator='#') eq_(self.field.clean(u'[email protected]#[email protected]'), u'[email protected], [email protected]')
def test_url_field(self): field = SeparatedValuesField(forms.URLField) eq_(field.clean(u'http://hy.fr/,,http://yo.lo'), u'http://hy.fr/, http://yo.lo/')
def setUp(self): self.field = SeparatedValuesField(forms.EmailField)
class AdminSettingsForm(PreviewForm): DELETE = forms.BooleanField(required=False) mozilla_contact = SeparatedValuesField(forms.EmailField, separator=',', required=False) vip_app = forms.BooleanField(required=False) priority_review = forms.BooleanField(required=False) class Meta: model = Preview fields = ('file_upload', 'upload_hash', 'position') def __init__(self, *args, **kw): # Note that this form is not inheriting from AddonFormBase, so we have # to get rid of 'version' ourselves instead of letting the parent class # do it. kw.pop('version', None) # Get the object for the app's promo `Preview` and pass it to the form. if kw.get('instance'): addon = kw.pop('instance') self.instance = addon self.promo = addon.get_promo() self.request = kw.pop('request', None) # Note: After calling `super`, `self.instance` becomes the `Preview` # object. super(AdminSettingsForm, self).__init__(*args, **kw) self.initial['vip_app'] = addon.vip_app self.initial['priority_review'] = addon.priority_review if self.instance: self.initial['mozilla_contact'] = addon.mozilla_contact @property def regions_by_id(self): return mkt.regions.REGIONS_CHOICES_ID_DICT def clean_position(self): return -1 def clean_mozilla_contact(self): contact = self.cleaned_data.get('mozilla_contact') if self.cleaned_data.get('mozilla_contact') is None: return u'' return contact def save(self, addon, commit=True): if (self.cleaned_data.get('DELETE') and 'upload_hash' not in self.changed_data and self.promo.id): self.promo.delete() elif self.promo and 'upload_hash' in self.changed_data: self.promo.delete() elif self.cleaned_data.get('upload_hash'): super(AdminSettingsForm, self).save(addon, True) updates = { 'vip_app': self.cleaned_data.get('vip_app'), } contact = self.cleaned_data.get('mozilla_contact') if contact is not None: updates['mozilla_contact'] = contact if (self.cleaned_data.get('priority_review') and not addon.priority_review): # addon.priority_review gets updated within prioritize_app(). prioritize_app(addon, self.request.user) else: updates['priority_review'] = self.cleaned_data.get( 'priority_review') addon.update(**updates) index_webapps.delay([addon.id]) return addon
def test_url_field(self): field = SeparatedValuesField(forms.URLField) eq_(field.clean(u'http://hy.fr/,,http://yo.lo'), u'http://hy.fr/, http://yo.lo')
class AdminSettingsForm(PreviewForm): DELETE = forms.BooleanField(required=False) mozilla_contact = SeparatedValuesField(forms.EmailField, separator=',', required=False) vip_app = forms.BooleanField(required=False) priority_review = forms.BooleanField(required=False) tags = forms.CharField(required=False) banner_regions = JSONMultipleChoiceField( required=False, choices=mkt.regions.REGIONS_CHOICES_NAME) banner_message = TransField(required=False) class Meta: model = Preview fields = ('file_upload', 'upload_hash', 'position') def __init__(self, *args, **kw): # Note that this form is not inheriting from AddonFormBase, so we have # to get rid of 'version' ourselves instead of letting the parent class # do it. kw.pop('version', None) # Get the object for the app's promo `Preview` and pass it to the form. if kw.get('instance'): addon = kw.pop('instance') self.instance = addon self.promo = addon.get_promo() self.request = kw.pop('request', None) # Note: After calling `super`, `self.instance` becomes the `Preview` # object. super(AdminSettingsForm, self).__init__(*args, **kw) self.initial['vip_app'] = addon.vip_app self.initial['priority_review'] = addon.priority_review if self.instance: self.initial['mozilla_contact'] = addon.mozilla_contact self.initial['tags'] = ', '.join(self.get_tags(addon)) self.initial['banner_regions'] = addon.geodata.banner_regions or [] self.initial['banner_message'] = addon.geodata.banner_message_id @property def regions_by_id(self): return mkt.regions.REGIONS_CHOICES_ID_DICT def clean_position(self): return -1 def clean_banner_regions(self): try: regions = map(int, self.cleaned_data.get('banner_regions')) except (TypeError, ValueError): # input data is not a list or data contains non-integers. raise forms.ValidationError(_('Invalid region(s) selected.')) return list(regions) def get_tags(self, addon): if acl.action_allowed(self.request, 'Apps', 'Edit'): return list(addon.tags.values_list('tag_text', flat=True)) else: return list( addon.tags.filter(restricted=False).values_list('tag_text', flat=True)) def clean_tags(self): return clean_tags(self.request, self.cleaned_data['tags']) def clean_mozilla_contact(self): contact = self.cleaned_data.get('mozilla_contact') if self.cleaned_data.get('mozilla_contact') is None: return u'' return contact def save(self, addon, commit=True): if (self.cleaned_data.get('DELETE') and 'upload_hash' not in self.changed_data and self.promo.id): self.promo.delete() elif self.promo and 'upload_hash' in self.changed_data: self.promo.delete() elif self.cleaned_data.get('upload_hash'): super(AdminSettingsForm, self).save(addon, True) updates = { 'vip_app': self.cleaned_data.get('vip_app'), 'priority_review': self.cleaned_data.get('priority_review'), } contact = self.cleaned_data.get('mozilla_contact') if contact is not None: updates['mozilla_contact'] = contact addon.update(**updates) tags_new = self.cleaned_data['tags'] tags_old = [slugify(t, spaces=True) for t in self.get_tags(addon)] add_tags = set(tags_new) - set(tags_old) del_tags = set(tags_old) - set(tags_new) # Add new tags. for t in add_tags: Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in del_tags: Tag(tag_text=t).remove_tag(addon) geodata = addon.geodata geodata.banner_regions = self.cleaned_data.get('banner_regions') geodata.banner_message = self.cleaned_data.get('banner_message') geodata.save() uses_flash = self.cleaned_data.get('flash') af = addon.get_latest_file() if af is not None: af.update(uses_flash=bool(uses_flash)) index_webapps.delay([addon.id]) return addon