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
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) 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
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) 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() new_ratings = after - before for i in new_ratings: rb = ALL_RATINGS()[i] addon.content_ratings.create(rating=rb.id, ratings_body=rb.ratingsbody.id) else: addon.content_ratings.all().delete() 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
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 __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 __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 self.initial['adult_content'] = addon.has_flag('adult_content') self.initial['child_content'] = addon.has_flag('child_content') 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 __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