class SeparatedValuesFieldTestCase(amo.tests.TestCase): def setUp(self): super(SeparatedValuesFieldTestCase, self).setUp() 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]')
class AdminSettingsForm(PreviewForm): DELETE = forms.BooleanField(required=False) mozilla_contact = SeparatedValuesField(forms.EmailField, separator=',', required=False) tags = forms.CharField(required=False) app_ratings = forms.MultipleChoiceField(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): # 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) self.base_fields['app_ratings'].choices = RATINGS_BY_NAME() self.disabled_regions = sorted(addon.get_excluded_region_ids()) # Note: After calling `super`, `self.instance` becomes the `Preview` # object. super(AdminSettingsForm, self).__init__(*args, **kw) if self.instance: self.initial['mozilla_contact'] = addon.mozilla_contact self.initial['tags'] = ', '.join(self.get_tags(addon)) app_ratings = [] for acr in addon.content_ratings.all(): rating = RATINGS_BODIES[acr.ratings_body].ratings[acr.rating] try: app_ratings.append(ALL_RATINGS().index(rating)) except ValueError: pass # Due to waffled ratings bodies. self.initial['app_ratings'] = app_ratings 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_app_ratings(self): ratings_ids = self.cleaned_data.get('app_ratings') ratings = [ALL_RATINGS()[int(i)] for i in ratings_ids] ratingsbodies = set([r.ratingsbody for r in ratings]) if len(ratingsbodies) != len(ratings): raise forms.ValidationError( _('Only one rating from each ratings ' 'body may be selected.')) return ratings_ids 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.')) if set(regions).intersection(self.disabled_regions): raise forms.ValidationError( _('Only regions the app is already ' 'available in may be 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 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) contact = self.cleaned_data.get('mozilla_contact') if contact: addon.update(mozilla_contact=contact) tags = self.cleaned_data.get('tags') if tags: 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) # Content ratings. ratings = self.cleaned_data.get('app_ratings') if ratings: ratings = [ALL_RATINGS()[int(i)] for i in ratings] # Delete content ratings with ratings body not in new set. r_bodies = set([rating.ratingsbody.id for rating in ratings]) addon.content_ratings.exclude(ratings_body__in=r_bodies).delete() # Set content ratings, takes {<ratingsbody class>: <rating class>}. addon.set_content_ratings( dict((rating.ratingsbody, rating) for rating in ratings)) else: addon.content_ratings.all().delete() geodata = addon.geodata geodata.banner_regions = self.cleaned_data.get('banner_regions') geodata.banner_message = self.cleaned_data.get('banner_message') geodata.save() toggle_game(addon) 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
class AdminSettingsForm(PreviewForm): DELETE = forms.BooleanField(required=False) mozilla_contact = SeparatedValuesField(forms.EmailField, separator=',', required=False) vip_app = 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): # 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 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) contact = self.cleaned_data.get('mozilla_contact') if contact is not None: addon.update(mozilla_contact=contact) vip = self.cleaned_data.get('vip_app') addon.update(vip_app=bool(vip)) tags = self.cleaned_data.get('tags') if tags: 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
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): super(SeparatedValuesFieldTestCase, self).setUp() self.field = SeparatedValuesField(forms.EmailField)
def setUp(self): self.field = SeparatedValuesField(forms.EmailField)
class AdminSettingsForm(PreviewForm): DELETE = forms.BooleanField(required=False) mozilla_contact = SeparatedValuesField(forms.EmailField, separator=',', required=False) tags = forms.CharField(required=False) app_ratings = forms.MultipleChoiceField(required=False, choices=RATINGS_BY_NAME) class Meta: model = Preview fields = ('caption', 'file_upload', 'upload_hash', 'position') def __init__(self, *args, **kw): # 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) if self.instance: self.initial['mozilla_contact'] = addon.mozilla_contact self.initial['tags'] = ', '.join(self.get_tags(addon)) rs = [] for r in addon.content_ratings.all(): rating = RATINGS_BODIES[r.ratings_body].ratings[r.rating] rs.append(ALL_RATINGS.index(rating)) self.initial['app_ratings'] = rs def clean_caption(self): return '__promo__' def clean_position(self): return -1 def clean_app_ratings(self): ratings_ids = self.cleaned_data.get('app_ratings') ratings = [ALL_RATINGS[int(i)] for i in ratings_ids] ratingsbodies = set([r.ratingsbody for r in ratings]) if len(ratingsbodies) != len(ratings): raise forms.ValidationError(_('Only one rating from each ratings ' 'body may be selected.')) return ratings_ids 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 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) contact = self.cleaned_data.get('mozilla_contact') if contact: addon.update(mozilla_contact=contact) tags = self.cleaned_data.get('tags') if tags: 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) if add_tags or del_tags: index_webapps.delay([addon.id]) ratings = self.cleaned_data.get('app_ratings') if ratings: before = set(addon.content_ratings.filter(rating__in=ratings) .values_list('rating', flat=True)) after = set(int(r) for r in ratings) addon.content_ratings.exclude(rating__in=after).delete() for i in after - before: r = ALL_RATINGS[i] ContentRating.objects.create(addon=addon, rating=r.id, ratings_body=r.ratingsbody.id) else: addon.content_ratings.all().delete() ban_game_in_brazil(addon) uses_flash = self.cleaned_data.get('flash') af = addon.get_latest_file() if af is not None: af.update(uses_flash=bool(uses_flash)) return addon
class AdminSettingsForm(PreviewForm): DELETE = forms.BooleanField(required=False) mozilla_contact = SeparatedValuesField(forms.EmailField, separator=',', required=False) app_ratings = forms.MultipleChoiceField(required=False, choices=RATINGS_BY_NAME) class Meta: model = Preview fields = ('caption', 'file_upload', 'upload_hash', 'position') def __init__(self, *args, **kw): # 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() # Just consume the request - we don't care. kw.pop('request', None) super(AdminSettingsForm, self).__init__(*args, **kw) if self.instance: self.initial['mozilla_contact'] = addon.mozilla_contact rs = [] for r in addon.content_ratings.all(): rating = RATINGS_BODIES[r.ratings_body].ratings[r.rating] rs.append(ALL_RATINGS.index(rating)) self.initial['app_ratings'] = rs def clean_caption(self): return '__promo__' def clean_position(self): return -1 def clean_app_ratings(self): ratings_ids = self.cleaned_data.get('app_ratings') ratings = [ALL_RATINGS[int(i)] for i in ratings_ids] ratingsbodies = set([r.ratingsbody for r in ratings]) if len(ratingsbodies) != len(ratings): raise forms.ValidationError( _('Only one rating from each ratings ' 'body may be selected.')) return ratings_ids 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) contact = self.cleaned_data.get('mozilla_contact') if contact: addon.update(mozilla_contact=contact) ratings = self.cleaned_data.get('app_ratings') if ratings: before = set( addon.content_ratings.filter(rating__in=ratings).values_list( 'rating', flat=True)) after = set(int(r) for r in ratings) addon.content_ratings.exclude(rating__in=after).delete() for i in after - before: r = ALL_RATINGS[i] ContentRating.objects.create(addon=addon, rating=r.id, ratings_body=r.ratingsbody.id) else: addon.content_ratings.all().delete() ban_game_in_brazil(addon) uses_flash = self.cleaned_data.get('flash') af = addon.get_latest_file() if af is not None: af.update(uses_flash=bool(uses_flash)) return addon