def clean_slug(self): if not self.slug: if not self.name: try: name = Translation.objects.filter(id=self.name_id)[0] except IndexError: name = str(self.id) else: name = self.name self.slug = slugify(name)[:27] if self.slug.isdigit(): self.slug += "~" qs = Addon.objects.values_list('slug', 'id') match = qs.filter(slug=self.slug) if match and match[0][1] != self.id: if self.id: prefix = '%s-%s' % (self.slug[:-len(str(self.id))], self.id) else: prefix = self.slug slugs = dict(qs.filter(slug__startswith='%s-' % prefix)) slugs.update(match) for idx in range(len(slugs)): new = ('%s-%s' % (prefix, idx + 1))[:30] if new not in slugs: self.slug = new return
def create_theme(name): """ Create a theme with the given `name`, his version and Persona instance. """ kwargs = { 'status': STATUS_PUBLIC, 'name': name, 'slug': slugify(name), 'bayesian_rating': random.uniform(1, 5), 'average_daily_users': random.randint(200, 2000), 'weekly_downloads': random.randint(200, 2000), 'created': datetime.now(), 'last_updated': datetime.now(), } # Themes need to start life as an extension for versioning. theme = Addon.objects.create(type=ADDON_EXTENSION, **kwargs) generate_version(addon=theme) theme.update_version() theme.status = STATUS_PUBLIC theme.type = ADDON_PERSONA Persona.objects.create(addon=theme, popularity=theme.weekly_downloads, persona_id=0) theme.save() return theme
def clean_tags(self): target = [slugify(t, spaces=True) for t in self.cleaned_data['tags'].split(',')] target = filter(None, target) min_len = amo.MIN_TAG_LENGTH max_tags = amo.MAX_TAGS total = len(target) blacklisted = [] for tag in Tag.objects.filter(tag_text__in=target): if len(tag.tag_text) > 0 and tag.blacklisted: blacklisted.append(tag.tag_text) if blacklisted: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ngettext('Invalid tag: {0}', 'Invalid tags: {0}', len(blacklisted)).format(', '.join(blacklisted)) raise forms.ValidationError(msg) if total > max_tags: num = total - max_tags msg = ngettext('You have {0} too many tags.', 'You have {0} too many tags.', num).format(num) raise forms.ValidationError(msg) if any(t for t in target if len(t) < amo.MIN_TAG_LENGTH): msg = ngettext("All tags must be at least {0} character.", "All tags must be at least {0} characters.", min_len).format(min_len) raise forms.ValidationError(msg) return target
def clean_tag(pk, **kw): task_log.info("[1@%s] Cleaning tag %s" % (clean_tag.rate_limit, pk)) try: # It could be that a previous run of this has deleted our # tag, if so we just leave. tag = Tag.objects.no_cache().get(pk=pk) except Tag.DoesNotExist: return old = tag.tag_text new = slugify(old, spaces=True, lower=True) if old != new: # Find out if there's any existing tags with this tag. existing = (Tag.objects.no_cache().filter( tag_text=new).select_related().exclude(pk=tag.pk).order_by("pk")) blacklisted = tag.blacklisted if existing: # Before deleting them, see if any AddonTags need to # be moved over. for existing_tag in existing: for addon_tag in existing_tag.addon_tags.all(): if not (AddonTag.objects.no_cache().filter( addon=addon_tag.addon, tag=tag).exists()): # If there are no tags for this addon, but there is # for an existing and about to be deleted addon tag, # move just one addon tag over. addon_tag.update(tag=tag) # If there's a tag in all this that's blacklisted, keep that # around. if existing_tag.blacklisted: blacklisted = True Tag.objects.filter(pk__in=[e.pk for e in existing]).delete() tag.update(tag_text=new, blacklisted=blacklisted)
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 = self.instance persona = addon.persona data = self.cleaned_data # Update Persona-specific data. persona_data = { 'license': int(data['license']), 'accentcolor': data['accentcolor'].lstrip('#'), 'textcolor': data['textcolor'].lstrip('#'), 'author': self.request.amo_user.username, 'display_username': self.request.amo_user.name } changed = False for k, v in persona_data.iteritems(): if v != getattr(persona, k): changed = True setattr(persona, k, v) if changed: persona.save() if self.changed_data: amo.log(amo.LOG.EDIT_PROPERTIES, addon) self.instance.modified = datetime.now() # Update Addon-specific data. changed = ( set(self.old_tags) != data['tags'] or # Check if tags changed. self.initial['slug'] != data['slug'] or # Check if slug changed. transfield_changed('description', self.initial, data) or transfield_changed('name', self.initial, data)) if changed: # Only save if addon data changed. super(EditThemeForm, self).save() # Update tags. tags_new = data['tags'] tags_old = [slugify(t, spaces=True) for t in self.old_tags] # Add new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in set(tags_old) - set(tags_new): Tag(tag_text=t).remove_tag(addon) # Update category. if data['category'].id != self.initial['category']: addon_cat = addon.addoncategory_set.all()[0] addon_cat.category = data['category'] addon_cat.save() # Theme reupload. if not addon.is_pending(): if data['header_hash'] or data['footer_hash']: save_theme_reupload.delay(data['header_hash'], data['footer_hash'], addon) return data
def save(self): addon = self.instance persona = addon.persona data = self.cleaned_data # Update Persona-specific data. persona_data = { 'license': int(data['license']), 'accentcolor': data['accentcolor'].lstrip('#'), 'textcolor': data['textcolor'].lstrip('#'), 'author': self.request.amo_user.username, 'display_username': self.request.amo_user.name } changed = False for k, v in persona_data.iteritems(): if v != getattr(persona, k): changed = True setattr(persona, k, v) if changed: persona.save() if self.changed_data: amo.log(amo.LOG.EDIT_PROPERTIES, addon) self.instance.modified = datetime.now() # Update Addon-specific data. changed = ( set(self.old_tags) != data['tags'] or # Check if tags changed. self.initial['slug'] != data['slug'] or # Check if slug changed. transfield_changed('description', self.initial, data) or transfield_changed('name', self.initial, data)) if changed: # Only save if addon data changed. super(EditThemeForm, self).save() # Update tags. tags_new = data['tags'] tags_old = [slugify(t, spaces=True) for t in self.old_tags] # Add new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in set(tags_old) - set(tags_new): Tag(tag_text=t).remove_tag(addon) # Update category. if data['category'].id != self.initial['category']: addon_cat = addon.addoncategory_set.all()[0] addon_cat.category = data['category'] addon_cat.save() # Theme reupload. if not addon.is_pending(): if data['header_hash'] or data['footer_hash']: save_theme_reupload.delay( data['header_hash'], data['footer_hash'], addon) return data
def save(self): addon = self.instance persona = addon.persona data = self.cleaned_data # Update Persona-specific data. persona_data = { 'license': int(data['license']), 'accentcolor': data['accentcolor'].lstrip('#'), 'textcolor': data['textcolor'].lstrip('#'), 'author': self.request.amo_user.username, 'display_username': self.request.amo_user.name } changed = False for k, v in persona_data.iteritems(): if v != getattr(persona, k): changed = True setattr(persona, k, v) if changed: persona.save() # Update name, slug, and summary. if self.changed_data: amo.log(amo.LOG.EDIT_PROPERTIES, addon) self.instance.modified = datetime.now() # Save the Addon object. super(EditThemeForm, self).save() tags_new = data['tags'] tags_old = [slugify(t, spaces=True) for t in self.get_tags(addon)] # Add new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in set(tags_old) - set(tags_new): Tag(tag_text=t).remove_tag(addon) # Update category. try: old_cat = (addon.addoncategory_set .exclude(category_id=data['category'].id))[0] except IndexError: # The category has remained unchanged. pass else: old_cat.category = data['category'] old_cat.save() # Theme reupload. save_theme_reupload.delay(data['header_hash'], data['footer_hash'], addon) return data
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() 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 = self.instance persona = addon.persona data = self.cleaned_data # Update Persona-specific data. persona_data = { 'license': int(data['license']), 'accentcolor': data['accentcolor'].lstrip('#'), 'textcolor': data['textcolor'].lstrip('#'), 'author': self.request.amo_user.username, 'display_username': self.request.amo_user.name } changed = False for k, v in persona_data.iteritems(): if v != getattr(persona, k): changed = True setattr(persona, k, v) if changed: persona.save() if self.changed_data: amo.log(amo.LOG.EDIT_PROPERTIES, addon) self.instance.modified = datetime.now() # Save the Addon object. super(EditThemeForm, self).save() tags_new = data['tags'] tags_old = [slugify(t, spaces=True) for t in self.get_tags(addon)] # Add new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in set(tags_old) - set(tags_new): Tag(tag_text=t).remove_tag(addon) # Update category. try: old_cat = (addon.addoncategory_set .exclude(category_id=data['category'].id))[0] except IndexError: # The category has remained unchanged. pass else: old_cat.category = data['category'] old_cat.save() # Theme reupload. save_theme_reupload.delay(data['header_hash'], data['footer_hash'], addon) return data
def clean_slug(self): slug = slugify(self.cleaned_data["slug"]) slug_validator(slug) if self.instance and self.instance.slug == slug: return slug author = self.initial["author"] if author.collections.filter(slug=slug).count(): raise forms.ValidationError(_("This url is already in use by another collection")) return slug
def clean_package_name(self): slug = self.cleaned_data["package_name"] if slugify(slug, ok="_", lower=False, delimiter="_") != slug: raise forms.ValidationError( _("Enter a valid package name consisting of letters, numbers, " "or underscores.") ) if Addon.objects.filter(slug=slug).exists(): raise forms.ValidationError(_("This package name is already in use.")) if BlacklistedSlug.blocked(slug): raise forms.ValidationError(_(u"The package name cannot be: %s." % slug)) return slug
def clean_slug(self): slug = slugify(self.cleaned_data['slug']) slug_validator(slug) if self.instance and self.instance.slug == slug: return slug author = self.initial['author'] if author.collections.filter(slug=slug).count(): raise forms.ValidationError( _('This url is already in use by another collection')) return slug
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
def clean_package_name(self): slug = self.cleaned_data['package_name'] if slugify(slug, ok='_', lower=False, delimiter='_') != slug: raise forms.ValidationError( _('Enter a valid package name consisting of letters, numbers, ' 'or underscores.')) if Addon.objects.filter(slug=slug).exists(): raise forms.ValidationError( _('This package name is already in use.')) if BlacklistedSlug.blocked(slug): raise forms.ValidationError( _(u'The package name cannot be: %s.' % slug)) return slug
def clean_tags(self): target = [ slugify(t, spaces=True, lower=True) for t in self.cleaned_data['tags'].split(',') ] target = set(filter(None, target)) min_len = amo.MIN_TAG_LENGTH max_len = Tag._meta.get_field('tag_text').max_length max_tags = amo.MAX_TAGS total = len(target) blacklisted = (Tag.objects.values_list('tag_text', flat=True).filter( tag_text__in=target, blacklisted=True)) if blacklisted: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ngettext('Invalid tag: {0}', 'Invalid tags: {0}', len(blacklisted)).format(', '.join(blacklisted)) raise forms.ValidationError(msg) restricted = (Tag.objects.values_list('tag_text', flat=True).filter( tag_text__in=target, restricted=True)) if not acl.action_allowed(self.request, 'Admin', 'EditAnyAddon'): if restricted: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ngettext('"{0}" is a reserved tag and cannot be used.', '"{0}" are reserved tags and cannot be used.', len(restricted)).format('", "'.join(restricted)) raise forms.ValidationError(msg) else: # Admin's restricted tags don't count towards the limit. total = len(target - set(restricted)) if total > max_tags: num = total - max_tags msg = ngettext('You have {0} too many tags.', 'You have {0} too many tags.', num).format(num) raise forms.ValidationError(msg) if any(t for t in target if len(t) > max_len): raise forms.ValidationError( _('All tags must be %s characters ' 'or less after invalid characters are removed.' % max_len)) if any(t for t in target if len(t) < min_len): msg = ngettext("All tags must be at least {0} character.", "All tags must be at least {0} characters.", min_len).format(min_len) raise forms.ValidationError(msg) return target
def generate_manifest(app): data = { "name": unicode(app.name), "description": "This app has been automatically generated", "version": "1.0", "icons": { "16": "http://testmanifest.com/icon-16.png", "48": "http://testmanifest.com/icon-48.png", "128": "http://testmanifest.com/icon-128.png", }, "installs_allowed_from": ["*"], "developer": {"name": "Marketplace Team", "url": "https://marketplace.firefox.com/credits"}, } AppManifest.objects.create(version=app.latest_version, manifest=json.dumps(data)) app.update(manifest_url="http://%s.testmanifest.com/manifest.webapp" % (slugify(unicode(app.name)),))
def clean_tags(request, tags): target = [slugify(t, spaces=True, lower=True) for t in tags.split(',')] target = set(filter(None, target)) min_len = amo.MIN_TAG_LENGTH max_len = Tag._meta.get_field('tag_text').max_length max_tags = amo.MAX_TAGS total = len(target) blacklisted = (Tag.objects.values_list('tag_text', flat=True) .filter(tag_text__in=target, blacklisted=True)) if blacklisted: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ngettext('Invalid tag: {0}', 'Invalid tags: {0}', len(blacklisted)).format(', '.join(blacklisted)) raise forms.ValidationError(msg) restricted = (Tag.objects.values_list('tag_text', flat=True) .filter(tag_text__in=target, restricted=True)) if not acl.action_allowed(request, 'Addons', 'Edit'): if restricted: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ngettext('"{0}" is a reserved tag and cannot be used.', '"{0}" are reserved tags and cannot be used.', len(restricted)).format('", "'.join(restricted)) raise forms.ValidationError(msg) else: # Admin's restricted tags don't count towards the limit. total = len(target - set(restricted)) if total > max_tags: num = total - max_tags msg = ngettext('You have {0} too many tags.', 'You have {0} too many tags.', num).format(num) raise forms.ValidationError(msg) if any(t for t in target if len(t) > max_len): raise forms.ValidationError( _('All tags must be %s characters or less after invalid characters' ' are removed.' % max_len)) if any(t for t in target if len(t) < min_len): msg = ngettext("All tags must be at least {0} character.", "All tags must be at least {0} characters.", min_len).format(min_len) raise forms.ValidationError(msg) return target
def collections_add_slugs(): """Give slugs to any slugless collections.""" # Don't touch the modified date. Collection._meta.get_field('modified').auto_now = False q = Collection.objects.filter(slug=None) ids = q.values_list('id', flat=True) task_log.info('%s collections without names' % len(ids)) max_length = Collection._meta.get_field('slug').max_length cnt = itertools.count() # Chunk it so we don't do huge queries. for chunk in chunked(ids, 300): for c in q.no_cache().filter(id__in=chunk): c.slug = c.nickname or slugify(c.name)[:max_length] if not c.slug: c.slug = 'collection' c.save(force_update=True) task_log.info(u'%s. %s => %s' % (next(cnt), c.name, c.slug))
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
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 save(self, addon, commit=False): tags_new = self.cleaned_data['tags'] tags_old = [slugify(t, spaces=True) for t in self.get_tags(addon)] # Add new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in set(tags_old) - set(tags_new): Tag(tag_text=t).remove_tag(addon) # We ignore `commit`, since we need it to be `False` so we can save # the ManyToMany fields on our own. addonform = super(AddonFormBasic, self).save(commit=False) addonform.save() return addonform
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 new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon) # Remove old tags. for t in set(tags_old) - set(tags_new): 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() 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
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
def create_addon(name, icon_type, application): """Create an addon with the given `name` and his version.""" kwargs = { 'status': STATUS_PUBLIC, 'name': name, 'slug': slugify(name), 'bayesian_rating': random.uniform(1, 5), 'average_daily_users': random.randint(200, 2000), 'weekly_downloads': random.randint(200, 2000), 'created': datetime.now(), 'last_updated': datetime.now(), 'icon_type': icon_type, } addon = Addon.objects.create(type=ADDON_EXTENSION, **kwargs) generate_version(addon=addon, app=application) addon.update_version() addon.status = STATUS_PUBLIC addon.save() return addon
def generate_manifest(app): data = { "name": unicode(app.name), "description": "This app has been automatically generated", "version": "1.0", "icons": { "16": "http://testmanifest.com/icon-16.png", "48": "http://testmanifest.com/icon-48.png", "128": "http://testmanifest.com/icon-128.png" }, "installs_allowed_from": ["*"], "developer": { "name": "Marketplace Team", "url": "https://marketplace.firefox.com/credits" } } AppManifest.objects.create( version=app.latest_version, manifest=json.dumps(data)) app.update(manifest_url="http://%s.testmanifest.com/manifest.webapp" % (slugify(unicode(app.name)),))
def addons_add_slugs(): """Give slugs to any slugless addons.""" Addon._meta.get_field('modified').auto_now = False q = Addon.objects.filter(slug=None).order_by('id') ids = q.values_list('id', flat=True) cnt = 0 total = len(ids) task_log.info('%s addons without slugs' % total) max_length = Addon._meta.get_field('slug').max_length # Chunk it so we don't do huge queries. for chunk in chunked(ids, 300): for c in q.no_cache().filter(id__in=chunk): slug = slugify(c.name)[:max_length] try: c.update(slug=slug) except IntegrityError: tail = '-%s' % c.id slug = "%s%s" % (slug[:max_length - len(tail)], tail) c.update(slug=slug) cnt += 300 task_log.info('Slugs added to %s/%s add-ons.' % (cnt, total))
def packager(data, feature_set, **kw): """Build an add-on based on input data.""" log.info('[1@None] Packaging add-on') # "Lock" the file by putting .lock in its name. from devhub.views import packager_path xpi_path = packager_path('%s.lock' % data['uuid']) log.info('Saving package to: %s' % xpi_path) from packager.main import packager data['slug'] = slugify(data['name']).replace('-', '_') features = set([k for k, v in feature_set.items() if v]) packager(data, xpi_path, features) # Unlock the file and make it available. try: shutil.move(xpi_path, packager_path(data['uuid'])) except IOError: log.error('Error unlocking add-on: %s' % xpi_path)
def create_collection(application): """Create a Collection for the given `application`.""" data = { 'type': amo.COLLECTION_NORMAL, 'application': application, 'name': 'Collection %s' % abs(hash(datetime.now())), 'addon_count': random.randint(200, 2000), 'subscribers': random.randint(1000, 5000), 'monthly_subscribers': random.randint(100, 500), 'weekly_subscribers': random.randint(10, 50), 'upvotes': random.randint(100, 500), 'downvotes': random.randint(100, 500), 'listed': True, } c = Collection(**data) c.slug = slugify(data['name']) c.rating = (c.upvotes - c.downvotes) * math.log(c.upvotes + c.downvotes) c.created = c.modified = datetime(2014, 10, 27, random.randint(0, 23), random.randint(0, 59)) c.save() return c
def clean_tag(pk, **kw): task_log.info("[1@%s] Cleaning tag %s" % (clean_tag.rate_limit, pk)) try: # It could be that a previous run of this has deleted our # tag, if so we just leave. tag = Tag.objects.no_cache().get(pk=pk) except Tag.DoesNotExist: return old = tag.tag_text new = slugify(old, spaces=True, lower=True) if old != new: # Find out if there's any existing tags with this tag. existing = (Tag.objects.no_cache().filter(tag_text=new) .select_related() .exclude(pk=tag.pk).order_by("pk")) blacklisted = tag.blacklisted if existing: # Before deleting them, see if any AddonTags need to # be moved over. for existing_tag in existing: for addon_tag in existing_tag.addon_tags.all(): if not (AddonTag.objects.no_cache() .filter(addon=addon_tag.addon, tag=tag) .exists()): # If there are no tags for this addon, but there is # for an existing and about to be deleted addon tag, # move just one addon tag over. addon_tag.update(tag=tag) # If there's a tag in all this that's blacklisted, keep that # around. if existing_tag.blacklisted: blacklisted = True Tag.objects.filter(pk__in=[e.pk for e in existing]).delete() tag.update(tag_text=new, blacklisted=blacklisted)
def collections_add_slugs(): """Give slugs to any slugless collections.""" q = Collection.objects.filter(slug=None) ids = q.values_list('id', flat=True) task_log.info('%s collections without names' % len(ids)) max_length = Collection._meta.get_field('slug').max_length cnt = itertools.count() # Chunk it so we don't do huge queries. for chunk in chunked(ids, 300): for c in q.no_cache().filter(id__in=chunk): c.slug = c.nickname or slugify(c.name)[:max_length] if not c.slug: c.slug = 'collection' c.save(force_update=True) task_log.info(u'%s. %s => %s' % (next(cnt), c.name, c.slug)) # Uniquify slug names by user. cursor = connections[multidb.get_slave()].cursor() dupes = cursor.execute(""" SELECT user_id, slug FROM ( SELECT user_id, slug, COUNT(1) AS cnt FROM collections c INNER JOIN collections_users cu ON c.id = cu.collection_id GROUP BY user_id, slug) j WHERE j.cnt > 1""") task_log.info('Uniquifying %s (user, slug) pairs' % dupes) cnt = itertools.count() for user, slug in cursor.fetchall(): q = Collection.objects.filter(slug=slug, collectionuser__user=user) # Skip the first one since it's unique without any appendage. for idx, c in enumerate(q[1:]): # Give enough space for appending a two-digit number. slug = c.slug[:max_length - 3] c.slug = u'%s-%s' % (slug, idx + 1) c.save(force_update=True) task_log.info(u'%s. %s => %s' % (next(cnt), slug, c.slug))
def test_slugify_spaces(): """We want slugify to preserve spaces, but not at either end.""" eq_(utils.slugify(' b ar '), 'b-ar') eq_(utils.slugify(' b ar ', spaces=True), 'b ar') eq_(utils.slugify(' b ar ', spaces=True), 'b ar')
def check(x, y): eq_(slugify(x), y) slug_validator(slugify(x))
def test_create_has_perms_no_slug(self): self.make_publisher() self.collection_data.pop('slug') res, data = self.create(self.client) eq_(res.status_code, 201) eq_(data['slug'], slugify(self.collection_data['name']['en-US']))
def test_slugify_spaces(): """We want slugify to preserve spaces, but not at either end.""" eq_(utils.slugify(" b ar "), "b-ar") eq_(utils.slugify(" b ar ", spaces=True), "b ar") eq_(utils.slugify(" b ar ", spaces=True), "b ar")
def test_slugify(): check = lambda x, y: eq_(slugify(x), y) s = [('xx x - "#$@ x', 'xx-x-x'), (u'Bän...g (bang)', u'bäng-bang')] for val, expected in s: yield check, val, expected
def generate_user(email): email = email or '*****@*****.**' username = slugify(email) user, _ = UserProfile.objects.get_or_create( email=email, defaults={'username': username}) return user
def generate_user(email): """Generate a UserProfile given the `email` provided.""" username = slugify(email) user, _ = UserProfile.objects.get_or_create( email=email, defaults={'username': username}) return user