def create_theme(name, **extra_kwargs): """ 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(), } kwargs.update(extra_kwargs) # 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_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 = 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.user.username, 'display_username': self.request.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.user.username, 'display_username': self.request.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 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_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( ugettext('This url is already in use by another collection')) return slug
def create_collection(application, **kwargs): """Create a Collection for the given `application`.""" data = { 'name': 'Collection %s' % abs(hash(datetime.now())), 'addon_count': random.randint(200, 2000), 'listed': True, } data.update(kwargs) c = Collection(**data) c.slug = slugify(data['name']) c.created = c.modified = datetime(2014, 10, 27, random.randint(0, 23), random.randint(0, 59)) c.save() return c
def create_addon(name, icon_type, application, **extra_kwargs): """Create an addon with the given `name` and his version.""" kwargs = { 'status': STATUS_PUBLIC, 'name': name, 'slug': slugify(name), 'guid': '@%s' % 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, 'type': ADDON_EXTENSION, } kwargs.update(extra_kwargs) addon = Addon.objects.create(**kwargs) generate_version(addon=addon, app=application) addon.update_version() addon.status = STATUS_PUBLIC addon.save() return addon
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) denied = (Tag.objects.values_list('tag_text', flat=True) .filter(tag_text__in=target, denied=True)) if denied: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ungettext('Invalid tag: {0}', 'Invalid tags: {0}', len(denied)).format(', '.join(denied)) 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, amo.permissions.ADDONS_EDIT): if restricted: # L10n: {0} is a single tag or a comma-separated list of tags. msg = ungettext('"{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 = ungettext('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( ugettext( '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 = ungettext('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 create_collection(application, **kwargs): """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), 'listed': True, } data.update(kwargs) c = Collection(**data) c.slug = slugify(data['name']) c.created = c.modified = datetime(2014, 10, 27, random.randint(0, 23), random.randint(0, 59)) c.save() return c
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 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 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 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 create_collection(application, **kwargs): """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, } data.update(kwargs) 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 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
def test_slugify_spaces(): """We want slugify to preserve spaces, but not at either end.""" assert utils.slugify(' b ar ') == 'b-ar' assert utils.slugify(' b ar ', spaces=True) == 'b ar' assert utils.slugify(' b ar ', spaces=True) == 'b ar'
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): assert slugify(x) == y slug_validator(slugify(x))
def check(x, y): eq_(slugify(x), y) slug_validator(slugify(x))
def test_slugify(test_input, expected): assert slugify(test_input) == expected slug_validator(slugify(test_input))