from django.db import models from django.contrib.auth import get_user_model from django.urls import reverse_lazy from .date_utils import after_30_days User = get_user_model() class Tag(models.Model): name = models.CharField(max_length=255, unique=True, db_index=True) class Meta: ordering = ['name'] def __str__(self): return self.name class Job(models.Model): CLOSED_REASON_FILLED = 'filled' CLOSED_REASON_NOT_FOUND = 'not-found' CLOSED_REASON_CANCELLED = 'cancelled' CLOSED_REASON_CHOICES = ( (CLOSED_REASON_FILLED, "Job filled"), (CLOSED_REASON_CANCELLED, "Job Cancelled"), (CLOSED_REASON_NOT_FOUND, "Suitable candidate not found") ) title = models.CharField(max_length=2055) description = models.TextField() tags = models.ManyToManyField(Tag, related_name="jobs") created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) expires = models.DateTimeField(default=after_30_days, editable=False) closed = models.BooleanField(default=False) close_reason = models.CharField( max_length=2055, choices=CLOSED_REASON_CHOICES, blank=True) posted_by = models.ForeignKey( User, related_name="jobs", on_delete=models.CASCADE) def renew(self): self.expires = after_30_days() self.save() def __str__(self): return self.title def get_absolute_url(self): return reverse_lazy("jobs:job_detail", args=[self.id]) class Question(models.Model): ''' A model to hold screening questions for a job post ''' name = models.TextField('Question Title') job = models.ForeignKey( Job, related_name='questions', on_delete=models.CASCADE) def __str__(self): return self.name class Application(models.Model): ''' A model representing applications to job ''' applicant = models.ForeignKey( User, related_name="applications", on_delete=models.CASCADE) job = models.ForeignKey( Job, related_name="applications", on_delete=models.CASCADE) cover_letter = models.TextField(blank=True) created = models.DateTimeField(auto_now_add=True) class Meta: unique_together = ('applicant', 'job') def __str__(self): return "{0} - Job: {1}".format(self.applicant, self.job) class Answer(models.Model): ''' a model to hold answers for screening questions by job applicants ''' question = models.ForeignKey( Question, related_name='answers', on_delete=models.CASCADE) application = models.ForeignKey( Application, related_name='answers', on_delete=models.CASCADE ) text = models.TextField() class Meta: unique_together = ('question', 'application') def __str__(self): return "Answer for : {0} - {1}".format(self.question, self.application)