Created
October 3, 2018 15:00
-
-
Save alex-jerez/ef321de8ed41cab76d346ab1c290b258 to your computer and use it in GitHub Desktop.
Sample code: "Job board" website models written in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.urls import reverse | |
from django.utils import timezone | |
# Code example for a "job board" style django site | |
# Written by Alex Jerez (alexjerez@fastmail.fm) | |
# for Django 2.0/Python 3.6 | |
JOB_CATEGORIES = ( | |
# ( slug, verbose) | |
('programming', 'Programming'), | |
('devops-it-sysadmin', 'DevOps & IT'), | |
('business-management', 'Business & Management'), | |
('sales-marketing', 'Sales & Marketing'), | |
('finance-legal', 'Finance & Legal'), | |
('other', 'Other'), | |
) | |
JOB_TYPE = ( | |
('Full-Time', 'Full-Time'), | |
('Part-Time', 'Part-Time'), | |
('Contract', 'Contract'), | |
('Temporary', 'Temporary'), | |
('Seasonal', 'Seasonal'), | |
('Internship', 'Internship'), | |
) | |
class JobPostingManager(models.Manager): | |
@staticmethod | |
def in_90_days(): | |
return timezone.datetime.today() + timezone.timedelta(days=90) | |
def get_queryset(self): | |
return super().get_queryset() \ | |
.filter(approved=True, visible=True, expires__lte=self.in_90_days()) | |
class JobPosting(models.Model): | |
title = models.CharField(max_length=255) | |
job_id = models.CharField(max_length=12, unique=True, blank=True, null=True) | |
job_type = models.CharField(max_length=20, choices=JOB_TYPE, default="Full Time") | |
date_submitted = models.DateField(auto_now_add=True) | |
company = models.CharField(max_length=255) | |
company_url = models.URLField(blank=True, null=True) | |
contact_name = models.CharField(max_length=255) | |
contact_email = models.EmailField() | |
description = models.TextField(max_length=2000) | |
category = models.CharField(max_length=255, choices=JOB_CATEGORIES, default='programming') | |
salary_range = models.CharField(max_length=255, blank=True, null=True) | |
remote = models.BooleanField(default=False) | |
location = models.CharField(max_length=512) | |
approved = models.BooleanField(default=False) | |
expires = models.DateField(default=JobPostingManager.in_90_days) | |
visible = models.BooleanField(default=False) | |
# Managers | |
objects = models.Manager() | |
valid_objects = JobPostingManager() | |
@property | |
def has_expired(self): | |
return timezone.datetime.today().date() > self.expires | |
def __str__(self): | |
if self.hide_company_name: | |
return '%s - %s' % (self.title, self.location) | |
return '%s - %s (%s)' % (self.title, self.location, self.company) | |
def get_absolute_url(self): | |
return reverse('job_detail', args=[self.job_id]) | |
class Meta: | |
ordering = ['-date_submitted', 'category'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment