Skip to content

Commit

Permalink
Merge pull request #144 from null-open-security-community/main
Browse files Browse the repository at this point in the history
Add slugs for jobs and users
  • Loading branch information
YogeshUpdhyay authored Jan 4, 2025
2 parents 6450517 + 833e3fa commit def5c10
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
15 changes: 14 additions & 1 deletion apps/accounts/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
3 changes: 2 additions & 1 deletion apps/accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class Meta:
"is_verified",
"is_profile_completed",
"is_active",
"user_type"
"user_type",
"slug"
]


Expand Down
14 changes: 14 additions & 0 deletions apps/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion apps/jobs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit def5c10

Please sign in to comment.