Skip to content

Commit

Permalink
refactor: create BaseModel with created_at and updated_at fields and …
Browse files Browse the repository at this point in the history
…implement it in all existing models
  • Loading branch information
DEENUU1 committed Feb 27, 2024
1 parent 22b3728 commit 685fc0b
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 47 deletions.
6 changes: 2 additions & 4 deletions candidates/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.conf import settings
from django.core.validators import FileExtensionValidator
from django.db import models

from company.models import Company
from offer.models import JobOffer
from users.models import UserAccount
from utils.base_model import BaseModel


class Candidate(models.Model):
class Candidate(BaseModel):
STATUS = (
("PENDING", "PENDING"),
("ACCEPTED", "ACCEPTED"),
Expand All @@ -20,8 +20,6 @@ class Candidate(models.Model):
phone = models.CharField(max_length=20)
future_recruitment = models.BooleanField(default=False)
message = models.TextField(max_length=500, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
job_offer = models.ForeignKey(JobOffer, on_delete=models.CASCADE)
user = models.ForeignKey(UserAccount, on_delete=models.CASCADE, blank=True, null=True)
status = models.CharField(max_length=10, choices=STATUS, default="PENDING")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.0 on 2024-02-27 21:37

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('company', '0006_company_category'),
]

operations = [
migrations.AddField(
model_name='company',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='companycategory',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='companycategory',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
6 changes: 3 additions & 3 deletions company/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from company.utils.validate_file_size import validate_file_size
from location.models import Address
from users.models import UserAccount
from utils.base_model import BaseModel


class CompanyCategory(models.Model):
class CompanyCategory(BaseModel):
name = models.CharField(max_length=50)

def __str__(self) -> str:
Expand All @@ -19,7 +20,7 @@ class Meta:
ordering = ['name']


class Company(models.Model):
class Company(BaseModel):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=200, unique=True, blank=True)
category = models.ForeignKey(CompanyCategory, on_delete=models.CASCADE, null=True, blank=True)
Expand All @@ -46,7 +47,6 @@ class Company(models.Model):
instagram_url = models.URLField(null=True, blank=True)
youtube_url = models.URLField(null=True, blank=True)
website_url = models.URLField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=False)

def __str__(self) -> str:
Expand Down
18 changes: 18 additions & 0 deletions favourite/migrations/0003_favourite_updated_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0 on 2024-02-27 21:37

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('favourite', '0002_initial'),
]

operations = [
migrations.AddField(
model_name='favourite',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
4 changes: 2 additions & 2 deletions favourite/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from offer.models import JobOffer
from users.models import UserAccount
from utils.base_model import BaseModel


class Favourite(models.Model):
class Favourite(BaseModel):
user = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
offer = models.ForeignKey(JobOffer, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f"{self.user} {self.offer}"
Expand Down
7 changes: 4 additions & 3 deletions favourite/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import path
from .views import (
FavouriteAPIView,
FavouriteListCreateAPIView,
FavouriteDeleteAPIView
)


urlpatterns = [
path("", FavouriteAPIView.as_view(), name="favourite_list_create_delete"),

path("", FavouriteListCreateAPIView.as_view(), name="favourite_list_create_delete"),
path("<int:pk>/", FavouriteDeleteAPIView.as_view(), name="favourite_delete_by_id")
]
7 changes: 6 additions & 1 deletion favourite/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .services.favourite import FavouriteService


class FavouriteAPIView(APIView):
class FavouriteListCreateAPIView(APIView):
"""
API view for handling Favourite operations.
Expand Down Expand Up @@ -53,6 +53,11 @@ def post(self, request):
self._service.create(serializer.validated_data)
return Response(serializer.data, status=status.HTTP_201_CREATED)


class FavouriteDeleteAPIView(APIView):
permission_classes = (IsAuthenticated,)
_service = FavouriteService(FavouriteRepository())

def delete(self, request, pk):
"""
Handles the HTTP DELETE request to delete a favourite.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Generated by Django 5.0 on 2024-02-27 21:37

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('location', '0002_alter_address_city_alter_address_country_and_more'),
]

operations = [
migrations.AddField(
model_name='address',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='address',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='city',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='city',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='country',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='country',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='region',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='region',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
9 changes: 5 additions & 4 deletions location/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.db import models
from utils.base_model import BaseModel


class Country(models.Model):
class Country(BaseModel):
name = models.CharField(max_length=255)

def __str__(self):
Expand All @@ -13,7 +14,7 @@ class Meta:
verbose_name_plural = "Countries"


class Region(models.Model):
class Region(BaseModel):
name = models.CharField(max_length=255)
country = models.ForeignKey(Country, on_delete=models.CASCADE)

Expand All @@ -26,7 +27,7 @@ class Meta:
verbose_name_plural = "Regions"


class City(models.Model):
class City(BaseModel):
name = models.CharField(max_length=255)
region = models.ForeignKey(Region, on_delete=models.CASCADE, null=True, blank=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True, blank=True)
Expand All @@ -42,7 +43,7 @@ class Meta:
verbose_name_plural = "Cities"


class Address(models.Model):
class Address(BaseModel):
country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True, blank=True)
city = models.ForeignKey(City, on_delete=models.CASCADE, null=True, blank=True)
region = models.ForeignKey(Region, on_delete=models.CASCADE, null=True, blank=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Generated by Django 5.0 on 2024-02-27 21:37

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('offer', '0005_alter_jobofferrate_options_jobofferrate_created_at'),
]

operations = [
migrations.AddField(
model_name='employmenttype',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='employmenttype',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='experience',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='experience',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='jobofferrate',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='salary',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='salary',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='worktype',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='worktype',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='jobofferrate',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
]
16 changes: 7 additions & 9 deletions offer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from company.models import Company
from location.models import Address
from utils.base_model import BaseModel


class WorkType(models.Model):
class WorkType(BaseModel):
name = models.CharField(max_length=50, unique=True)

def __str__(self):
Expand All @@ -19,7 +20,7 @@ class Meta:
verbose_name_plural = 'Work Types'


class EmploymentType(models.Model):
class EmploymentType(BaseModel):
name = models.CharField(max_length=50, unique=True)

def __str__(self):
Expand All @@ -31,7 +32,7 @@ class Meta:
verbose_name_plural = 'Employment Types'


class Experience(models.Model):
class Experience(BaseModel):
name = models.CharField(max_length=50, unique=True)

def __str__(self):
Expand All @@ -43,7 +44,7 @@ class Meta:
verbose_name_plural = 'Experiences'


class Salary(models.Model):
class Salary(BaseModel):
CURRENCIES = (
('PLN', 'PLN'),
('EURO', 'EURO'),
Expand Down Expand Up @@ -71,7 +72,7 @@ class Meta:
verbose_name_plural = 'Salaries'


class JobOffer(models.Model):
class JobOffer(BaseModel):
STATUS = (
("DRAFT", "DRAFT"),
("PENDING", "PENDING"),
Expand All @@ -93,8 +94,6 @@ class JobOffer(models.Model):
experience = models.ManyToManyField(Experience, blank=True)
work_type = models.ManyToManyField(WorkType, blank=True)
employment_type = models.ManyToManyField(EmploymentType, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS, default="DRAFT")

# These fields are only used for job offers that are scraped from other websites
Expand Down Expand Up @@ -130,7 +129,7 @@ def __str__(self) -> str:
return self.title


class JobOfferRate(models.Model):
class JobOfferRate(BaseModel):
RATE = (
(1, 1),
(2, 2),
Expand All @@ -140,7 +139,6 @@ class JobOfferRate(models.Model):
)
job_offer = models.ForeignKey(JobOffer, on_delete=models.CASCADE)
rate = models.IntegerField(choices=RATE)
created_at = models.DateTimeField(auto_now_add=True, null=True)

def __str__(self) -> str:
return f"{self.job_offer} - {self.rate}"
Expand Down
Loading

0 comments on commit 685fc0b

Please sign in to comment.