From 14dcbfa60ad137e22ff633716f10d9da39c748fd Mon Sep 17 00:00:00 2001 From: Yogesh Upadhyay Date: Sat, 4 Jan 2025 09:23:44 +0000 Subject: [PATCH] update slug generation --- apps/accounts/models.py | 16 ++++++++-------- apps/applicants/views.py | 9 ++++++++- apps/jobs/models.py | 16 ++++++++-------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/apps/accounts/models.py b/apps/accounts/models.py index 15ef796..044b7fc 100644 --- a/apps/accounts/models.py +++ b/apps/accounts/models.py @@ -1,7 +1,8 @@ import uuid -from django.utils.text import slugify from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.db import models +import string +import random # from Jobapp.models import User as JobUser # Create your models here. @@ -38,6 +39,9 @@ def create_superuser(self, email, name, password=None): user.save(using=self._db) return user +def generate_unique_slug(length=8): + characters = string.ascii_uppercase + string.digits # Uppercase letters and digits + return ''.join(random.choices(characters, k=length)) class User(AbstractBaseUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) @@ -73,13 +77,9 @@ class User(AbstractBaseUser): 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 + unique_slug = generate_unique_slug() + while User.objects.filter(slug=unique_slug).exists(): # Ensure uniqueness + unique_slug = generate_unique_slug() self.slug = unique_slug super().save(*args, **kwargs) diff --git a/apps/applicants/views.py b/apps/applicants/views.py index 2a426eb..ad7e561 100644 --- a/apps/applicants/views.py +++ b/apps/applicants/views.py @@ -29,7 +29,14 @@ class AllApplicantsOfCompany(APIView): ) def get(self, request): """List all users that belong to company""" - applicants = Applicants.objects.filter(job__employer=request.user) + filters = { + "job__employer": request.user + } + slug = request.query_params.get('slug', None) + if slug: + filters["slug"] = slug + + applicants = Applicants.objects.filter(**filters) return Response( ApplicantModelSerializer(applicants, many=True).data, diff --git a/apps/jobs/models.py b/apps/jobs/models.py index fd40a7a..a64face 100644 --- a/apps/jobs/models.py +++ b/apps/jobs/models.py @@ -4,7 +4,8 @@ from apps.accounts.models import User from apps.jobs.constants import values -from django.utils.text import slugify +import string +import random from apps.jobs.constants.values import GENDER, HIRING_STATUS, JOB_TYPE, STATUS_CHOICES @@ -52,6 +53,9 @@ class Meta: # policy is_deleted = models.BooleanField(default=False, editable=False) +def generate_unique_slug(length=8): + characters = string.ascii_uppercase + string.digits # Uppercase letters and digits + return ''.join(random.choices(characters, k=length)) class Job(models.Model): """ @@ -105,13 +109,9 @@ class Meta: 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 + unique_slug = generate_unique_slug() + while User.objects.filter(slug=unique_slug).exists(): # Ensure uniqueness + unique_slug = generate_unique_slug() self.slug = unique_slug super().save(*args, **kwargs)