From c45268945b893c9c7d3150ebf256f3e02c6e0847 Mon Sep 17 00:00:00 2001 From: Yogesh Upadhyay Date: Sat, 4 Jan 2025 08:39:05 +0000 Subject: [PATCH] add slug --- apps/accounts/models.py | 15 ++++++++++++++- apps/accounts/serializers.py | 3 ++- apps/jobs/models.py | 14 ++++++++++++++ apps/jobs/serializers.py | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/apps/accounts/models.py b/apps/accounts/models.py index ea54f77..15ef796 100644 --- a/apps/accounts/models.py +++ b/apps/accounts/models.py @@ -1,5 +1,5 @@ import uuid - +from django.utils.text import slugify from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.db import models @@ -41,6 +41,7 @@ def create_superuser(self, email, name, password=None): class User(AbstractBaseUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + slug = models.SlugField(unique=True, blank=True, editable=False) email = models.EmailField(max_length=255, unique=True) name = models.CharField(max_length=200) @@ -70,5 +71,17 @@ class User(AbstractBaseUser): USERNAME_FIELD = "email" # by default required REQUIRED_FIELDS = ["name"] + def save(self, *args, **kwargs): + if not self.slug: # Generate slug only if it doesn't exist + self.slug = slugify(self.name) + # Ensure uniqueness by appending a counter if needed + unique_slug = self.slug + counter = 1 + while User.objects.filter(slug=unique_slug).exists(): + unique_slug = f"{self.slug}{counter}" + counter += 1 + self.slug = unique_slug + super().save(*args, **kwargs) + class Meta: db_table = "tbl_user_auth" diff --git a/apps/accounts/serializers.py b/apps/accounts/serializers.py index f706232..98d42ca 100644 --- a/apps/accounts/serializers.py +++ b/apps/accounts/serializers.py @@ -98,7 +98,8 @@ class Meta: "is_verified", "is_profile_completed", "is_active", - "user_type" + "user_type", + "slug" ] diff --git a/apps/jobs/models.py b/apps/jobs/models.py index 6eed03d..fd40a7a 100644 --- a/apps/jobs/models.py +++ b/apps/jobs/models.py @@ -4,6 +4,7 @@ from apps.accounts.models import User from apps.jobs.constants import values +from django.utils.text import slugify from apps.jobs.constants.values import GENDER, HIRING_STATUS, JOB_TYPE, STATUS_CHOICES @@ -66,6 +67,7 @@ class Meta: job_id = models.UUIDField( primary_key=True, default=uuid.uuid4, editable=False, null=False ) + slug = models.SlugField(unique=True, blank=True) # relations to the user and the company as the jobs are posted by # a particular user and the company is more of a employer profile mapping @@ -101,6 +103,18 @@ class Meta: education_or_certifications = models.TextField(default="No Education details provided") about = models.TextField(default="No description provided") + def save(self, *args, **kwargs): + if not self.slug: # Generate slug only if it doesn't exist + self.slug = slugify(self.title) + # Ensure uniqueness by appending a counter if needed + unique_slug = self.slug + counter = 1 + while Job.objects.filter(slug=unique_slug).exists(): + unique_slug = f"{self.slug}{counter}" + counter += 1 + self.slug = unique_slug + super().save(*args, **kwargs) + class ContactMessage(models.Model): """Represents contact_us model. diff --git a/apps/jobs/serializers.py b/apps/jobs/serializers.py index 85252be..0fbf9f1 100644 --- a/apps/jobs/serializers.py +++ b/apps/jobs/serializers.py @@ -22,7 +22,7 @@ class Meta: model = Job fields = "__all__" - read_only_fields = ["employer_id", "company"] + read_only_fields = ["employer_id", "company", "slug"] def to_representation(self, instance): """