def clean_fields(self, **kwargs): if not self.id: self.slug = normalize_slug(self.name, self.slug, max_length=SLUG_MAX_LENGTH) self.slug = normalize_slug(slug=self.slug, max_length=SLUG_MAX_LENGTH) super().clean_fields(**kwargs)
def forwards(apps, *args): org_model = apps.get_model("organizations", "Organization") for org in org_model.objects.all(): new_slug = normalize_slug(name=org.name) if org_model.objects.filter(slug=new_slug).exists(): org.slug = new_slug + "-duplicate" else: org.slug = new_slug org.save()
class OrganizationFactory(DjangoModelFactory): class Meta: model = models.Organization django_get_or_create = ("name", ) name = factory.Sequence(lambda n: f"{fake.company()}-{str(n)}") stripe_account_id = fake.uuid4() address = factory.SubFactory(AddressFactory) stripe_verified = True slug = factory.lazy_attribute(lambda o: normalize_slug(name=o.name)) plan = factory.SubFactory("apps.organizations.tests.factories.PlanFactory")
def make_page_from_template(self, page_data={}): """Create DonationPage() instance from self. Expects template_data as dict, and optional page_data (eg. for creating a template page via org admin). We also clean up template and page data here, so that we only copy the fields we want. """ template_data = self.__dict__ template_data["name"] = f"New Page From Template ({template_data['name']})" template_data["slug"] = normalize_slug(name=template_data["name"]) unwanted_keys = ["_state", "id", "modified", "created", "published_date", "_history_user"] template = cleanup_keys(template_data, unwanted_keys) page = cleanup_keys(page_data, unwanted_keys) merged_page = {**template, **page} return DonationPage.objects.create(**merged_page)
class RevenueProgramFactory(DjangoModelFactory): class Meta: model = models.RevenueProgram django_get_or_create = ("name", ) name = factory.Sequence(lambda n: f"{' '.join(fake.words(nb=4))}-{str(n)}") slug = factory.lazy_attribute(lambda o: normalize_slug(name=o.name)) contact_email = fake.email() class Params: org = None @factory.lazy_attribute def organization(self): if self.org: return self.org return OrganizationFactory()
def clean_fields(self, **kwargs): self.slug = normalize_slug(self.name, self.slug) super().clean_fields(**kwargs)
def test_custom_length_enforced(): assert len(normalize_slug(f"{'x' * 80}", max_length=70)) == 70
def test_custom_length_allowed(): assert len(normalize_slug(f"{'x' * 60}", max_length=70)) == 60
def test_slug_with_name(): assert (normalize_slug(name="A name not slug")) == "a-name-not-slug"
def test_slug_with_supplied_slug(): assert normalize_slug(name="No Name", slug="A Name") == "a-name"
def test_normalize_slug_name_only(): assert len(normalize_slug("A name")) == 6 assert len(normalize_slug(f"{'x' * 60}")) == DEFAULT_MAX_SLUG_LENGTH