Skip to content

Commit

Permalink
Merge pull request #145 from YogeshUpdhyay/main
Browse files Browse the repository at this point in the history
update slug generation
  • Loading branch information
YogeshUpdhyay authored Jan 4, 2025
2 parents 833e3fa + 14dcbfa commit 98cb589
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
16 changes: 8 additions & 8 deletions apps/accounts/models.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion apps/applicants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions apps/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 98cb589

Please sign in to comment.