jobportal/users/models.py

109 lines
3.9 KiB
Python
Raw Normal View History

2020-12-28 20:28:52 +01:00
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin
from django.db import models
2021-01-06 19:07:42 +01:00
from django.db.models.signals import post_save
from django.dispatch import receiver
2020-12-28 20:28:52 +01:00
from django.utils.translation import gettext_lazy as _
2020-12-31 15:56:44 +01:00
from PIL import Image
2021-01-06 19:07:42 +01:00
from ckeditor.fields import RichTextField
2020-12-28 20:28:52 +01:00
2021-01-15 12:03:28 +01:00
from jobs.models import Job
2020-12-28 20:28:52 +01:00
class UserManager(BaseUserManager):
2020-12-29 23:26:10 +01:00
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError('Your email is not correct!')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser = True')
return self._create_user(email, password, **extra_fields)
2020-12-28 20:28:52 +01:00
class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'), max_length=50, blank=False)
last_name = models.CharField(_('last name'), max_length=50, blank=False)
date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
is_active = models.BooleanField(_('active'), default=True)
is_staff = models.BooleanField(_('is_staff'), default=False)
is_employee = models.BooleanField(default=False)
is_employer = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
2020-12-31 15:56:44 +01:00
2021-01-13 19:01:13 +01:00
def get_profile_id(self):
return self.profile.id
2021-01-15 15:19:10 +01:00
def count_unread_messages(self):
return self.invites.filter(unread=True).count()
2021-01-15 18:10:05 +01:00
def unread_messages(self):
return self.invites.filter(unread=True).values_list('job_id', flat=True)
2020-12-31 15:56:44 +01:00
class Profile(models.Model):
user = models.OneToOneField(Account, on_delete=models.CASCADE, related_name="profile")
2021-01-06 19:07:42 +01:00
image = models.ImageField(upload_to="media/users", default="media/users/person_1.jpg")
2020-12-31 15:56:44 +01:00
birth_day = models.DateField(default=None, blank=True, null=True)
location = models.CharField(max_length=100, blank=True)
2021-01-06 19:07:42 +01:00
resume = RichTextField(blank=True)
2020-12-31 15:56:44 +01:00
company = models.CharField(max_length=250, blank=True)
2021-01-16 09:20:59 +01:00
wish_list = models.ManyToManyField(Job, default=None, blank=True, related_name="wish_list")
2020-12-31 15:56:44 +01:00
def __str__(self):
return self.user.first_name + " " + self.user.last_name + " " + self.user.email
def save(self, *args, **kwargs):
super(Profile, self).save(*args, **kwargs)
img = Image.open(self.image)
if img.height > 200 or img.width > 200:
new_size = (200, 200)
img.thumbnail(new_size)
img.save(self.image.path)
2021-01-06 19:07:42 +01:00
2021-01-15 12:03:28 +01:00
class Invite(models.Model):
user = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="invites")
job = models.ForeignKey(Job, on_delete=models.CASCADE, related_name="invites")
date = models.DateField(default=None, blank=True, null=True)
message = RichTextField(blank=True)
unread = models.BooleanField(default=True)
2021-01-15 18:10:05 +01:00
def __str__(self):
return self.job.title
2021-01-15 12:03:28 +01:00
2021-01-06 19:07:42 +01:00
@receiver(models.signals.post_save, sender=Account)
def post_save_user_signal(sender, instance, created, **kwargs):
if created:
instance.save()
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=Account)