369 lines
15 KiB
Python
369 lines
15 KiB
Python
from django import forms
|
|
from .validators import validate_hash_tags
|
|
from crispy_forms.helper import FormHelper
|
|
from django.core.validators import URLValidator
|
|
from django.utils.translation import gettext_lazy as _
|
|
from crispy_forms.layout import Layout, Submit, HTML, Div, Field
|
|
from .models import ZoomMeeting, Candidate,Job,TrainingMaterial,JobPosting
|
|
|
|
class CandidateForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Candidate
|
|
fields = ['job', 'first_name', 'last_name', 'phone', 'email', 'resume', 'stage']
|
|
labels = {
|
|
'first_name': _('First Name'),
|
|
'last_name': _('Last Name'),
|
|
'phone': _('Phone'),
|
|
'email': _('Email'),
|
|
'resume': _('Resume'),
|
|
'stage': _('Application Stage'),
|
|
}
|
|
widgets = {
|
|
'first_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter first name')}),
|
|
'last_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter last name')}),
|
|
'phone': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter phone number')}),
|
|
'email': forms.EmailInput(attrs={'class': 'form-control', 'placeholder': _('Enter email')}),
|
|
'stage': forms.Select(attrs={'class': 'form-select'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.helper = FormHelper()
|
|
self.helper.form_method = 'post'
|
|
self.helper.form_class = 'form-horizontal'
|
|
self.helper.label_class = 'col-md-3'
|
|
self.helper.field_class = 'col-md-9'
|
|
|
|
# Make job field read-only if it's being pre-populated
|
|
job_value = self.initial.get('job')
|
|
if job_value:
|
|
self.fields['job'].widget.attrs['readonly'] = True
|
|
|
|
self.helper.layout = Layout(
|
|
Field('job', css_class='form-control'),
|
|
Field('first_name', css_class='form-control'),
|
|
Field('last_name', css_class='form-control'),
|
|
Field('phone', css_class='form-control'),
|
|
Field('email', css_class='form-control'),
|
|
Field('stage', css_class='form-control'),
|
|
Field('resume', css_class='form-control'),
|
|
Submit('submit', _('Submit'), css_class='btn btn-primary')
|
|
)
|
|
|
|
class CandidateStageForm(forms.ModelForm):
|
|
"""Form specifically for updating candidate stage with validation"""
|
|
|
|
class Meta:
|
|
model = Candidate
|
|
fields = ['stage']
|
|
labels = {
|
|
'stage': _('New Application Stage'),
|
|
}
|
|
widgets = {
|
|
'stage': forms.Select(attrs={'class': 'form-select'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
# Get the current candidate instance for validation
|
|
self.candidate = kwargs.pop('candidate', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
# Dynamically filter stage choices based on current stage
|
|
if self.candidate and self.candidate.pk:
|
|
current_stage = self.candidate.stage
|
|
available_stages = self.candidate.get_available_stages()
|
|
|
|
# Filter choices to only include available stages
|
|
choices = [(stage, self.candidate.Stage(stage).label)
|
|
for stage in available_stages]
|
|
self.fields['stage'].choices = choices
|
|
|
|
# Set initial value to current stage
|
|
self.fields['stage'].initial = current_stage
|
|
else:
|
|
# For new candidates, only show 'Applied' stage
|
|
self.fields['stage'].choices = [('Applied', _('Applied'))]
|
|
self.fields['stage'].initial = 'Applied'
|
|
|
|
def clean_stage(self):
|
|
"""Validate stage transition"""
|
|
new_stage = self.cleaned_data.get('stage')
|
|
if not new_stage:
|
|
raise forms.ValidationError(_('Please select a stage.'))
|
|
|
|
# Use model validation for stage transitions
|
|
if self.candidate and self.candidate.pk:
|
|
current_stage = self.candidate.stage
|
|
if new_stage != current_stage:
|
|
if not self.candidate.can_transition_to(new_stage):
|
|
allowed_stages = self.candidate.get_available_stages()
|
|
raise forms.ValidationError(
|
|
_('Cannot transition from "%(current)s" to "%(new)s". '
|
|
'Allowed transitions: %(allowed)s') % {
|
|
'current': current_stage,
|
|
'new': new_stage,
|
|
'allowed': ', '.join(allowed_stages) or 'None (final stage)'
|
|
}
|
|
)
|
|
|
|
return new_stage
|
|
|
|
class ZoomMeetingForm(forms.ModelForm):
|
|
class Meta:
|
|
model = ZoomMeeting
|
|
fields = ['topic', 'start_time', 'duration']
|
|
labels = {
|
|
'topic': _('Topic'),
|
|
'start_time': _('Start Time'),
|
|
'duration': _('Duration'),
|
|
}
|
|
widgets = {
|
|
'topic': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter meeting topic'),}),
|
|
'start_time': forms.DateTimeInput(attrs={'class': 'form-control','type': 'datetime-local'}),
|
|
'duration': forms.NumberInput(attrs={'class': 'form-control','min': 1, 'placeholder': _('60')}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.helper = FormHelper()
|
|
self.helper.form_method = 'post'
|
|
self.helper.form_class = 'form-horizontal'
|
|
self.helper.label_class = 'col-md-3'
|
|
self.helper.field_class = 'col-md-9'
|
|
self.helper.layout = Layout(
|
|
Field('topic', css_class='form-control'),
|
|
Field('start_time', css_class='form-control'),
|
|
Field('duration', css_class='form-control'),
|
|
Submit('submit', _('Create Meeting'), css_class='btn btn-primary')
|
|
)
|
|
|
|
# Old JobForm removed - replaced by JobPostingForm
|
|
|
|
class TrainingMaterialForm(forms.ModelForm):
|
|
class Meta:
|
|
model = TrainingMaterial
|
|
fields = ['title', 'content', 'video_link', 'file']
|
|
labels = {
|
|
'title': _('Title'),
|
|
'content': _('Content'),
|
|
'video_link': _('Video Link'),
|
|
'file': _('File'),
|
|
}
|
|
widgets = {
|
|
'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter title')}),
|
|
'content': forms.Textarea(attrs={'rows': 6, 'class': 'form-control', 'placeholder': _('Enter material content')}),
|
|
'video_link': forms.URLInput(attrs={'class': 'form-control', 'placeholder': _('https://www.youtube.com/watch?v=...')}),
|
|
'file': forms.FileInput(attrs={'class': 'form-control'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.helper = FormHelper()
|
|
self.helper.form_method = 'post'
|
|
self.helper.form_class = 'form-horizontal'
|
|
self.helper.label_class = 'col-md-3'
|
|
self.helper.field_class = 'col-md-9'
|
|
self.helper.layout = Layout(
|
|
Field('title', css_class='form-control'),
|
|
Field('content', css_class='form-control'),
|
|
Div(
|
|
Field('video_link', css_class='form-control'),
|
|
Field('file', css_class='form-control'),
|
|
css_class='row'
|
|
),
|
|
Submit('submit', _('Save Material'), css_class='btn btn-primary mt-3')
|
|
)
|
|
|
|
|
|
class JobPostingForm(forms.ModelForm):
|
|
"""Form for creating and editing job postings"""
|
|
|
|
class Meta:
|
|
model = JobPosting
|
|
fields = [
|
|
'title', 'department', 'job_type', 'workplace_type',
|
|
'location_city', 'location_state', 'location_country',
|
|
'description', 'qualifications', 'salary_range', 'benefits',
|
|
'application_url', 'application_deadline', 'application_instructions',
|
|
'position_number', 'reporting_to', 'start_date', 'status',
|
|
'created_by','open_positions','hash_tags'
|
|
]
|
|
widgets = {
|
|
# Basic Information
|
|
'title': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Assistant Professor of Computer Science',
|
|
'required': True
|
|
}),
|
|
'department': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Computer Science, Human Resources, etc.'
|
|
}),
|
|
'job_type': forms.Select(attrs={
|
|
'class': 'form-select',
|
|
'required': True
|
|
}),
|
|
'workplace_type': forms.Select(attrs={
|
|
'class': 'form-select',
|
|
'required': True
|
|
}),
|
|
|
|
# Location
|
|
'location_city': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Boston'
|
|
}),
|
|
'location_state': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'MA'
|
|
}),
|
|
'location_country': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'value': 'United States'
|
|
}),
|
|
|
|
# Job Details
|
|
'description': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 6,
|
|
'placeholder': 'Provide a comprehensive description of the role, responsibilities, and expectations...',
|
|
'required': True
|
|
}),
|
|
'qualifications': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 4,
|
|
'placeholder': 'List required qualifications, skills, education, and experience...'
|
|
}),
|
|
'salary_range': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': '$60,000 - $80,000'
|
|
}),
|
|
'benefits': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 2,
|
|
'placeholder': 'Health insurance, retirement plans, tuition reimbursement, etc.'
|
|
}),
|
|
|
|
# Application Information
|
|
'application_url': forms.URLInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'https://university.edu/careers/job123',
|
|
'required': True
|
|
}),
|
|
'application_deadline': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'application_instructions': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Special instructions for applicants (e.g., required documents, reference requirements, etc.)'
|
|
}),
|
|
'open_positions': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': 1,
|
|
'placeholder': 'Number of open positions'
|
|
}),
|
|
'hash_tags': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': '#hiring,#jobopening',
|
|
'validators':validate_hash_tags,
|
|
|
|
}),
|
|
|
|
# Internal Information
|
|
'position_number': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'UNIV-2025-001'
|
|
}),
|
|
'reporting_to': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Department Chair, Director, etc.'
|
|
}),
|
|
'start_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
|
|
'created_by': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'University Administrator'
|
|
}),
|
|
}
|
|
|
|
def __init__(self,*args,**kwargs):
|
|
|
|
# Extract your custom argument BEFORE calling super()
|
|
self.is_anonymous_user = kwargs.pop('is_anonymous_user', False)
|
|
# Now call the parent __init__ with remaining args
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if not self.instance.pk:# Creating new job posting
|
|
if not self.is_anonymous_user:
|
|
self.fields['created_by'].initial = 'University Administrator'
|
|
self.fields['status'].initial = 'Draft'
|
|
self.fields['location_city'].initial='Riyadh'
|
|
self.fields['location_state'].initial='Riyadh Province'
|
|
self.fields['location_country'].initial='Saudi Arabia'
|
|
|
|
|
|
def clean_hash_tags(self):
|
|
hash_tags=self.cleaned_data.get('hash_tags')
|
|
if hash_tags:
|
|
tags=[tag.strip() for tag in hash_tags.split(',') if tag.strip()]
|
|
for tag in tags:
|
|
if not tag.startswith('#'):
|
|
raise forms.ValidationError("Each hashtag must start with '#' symbol and must be comma(,) sepearted.")
|
|
return ','.join(tags)
|
|
return hash_tags # Allow blank
|
|
|
|
def clean_title(self):
|
|
title=self.cleaned_data.get('title')
|
|
if not title or len(title.strip())<3:
|
|
raise forms.ValidationError("Job title must be at least 3 characters long.")
|
|
if len(title)>200:
|
|
raise forms.ValidationError("Job title cannot exceed 200 characters.")
|
|
return title.strip()
|
|
|
|
def clean_description(self):
|
|
description=self.cleaned_data.get('description')
|
|
if not description or len(description.strip())<20:
|
|
raise forms.ValidationError("Job description must be at least 20 characters long.")
|
|
return description.strip() # to remove leading/trailing whitespace
|
|
|
|
def clean_application_url(self):
|
|
url=self.cleaned_data.get('application_url')
|
|
if url:
|
|
validator=URLValidator()
|
|
try:
|
|
validator(url)
|
|
except forms.ValidationError:
|
|
raise forms.ValidationError('Please enter a valid URL (e.g., https://example.com)')
|
|
return url
|
|
|
|
def clean(self):
|
|
"""Cross-field validation"""
|
|
cleaned_data = super().clean()
|
|
|
|
# Validate dates
|
|
start_date = cleaned_data.get('start_date')
|
|
application_deadline = cleaned_data.get('application_deadline')
|
|
|
|
# Perform cross-field validation only if both fields have values
|
|
if start_date and application_deadline:
|
|
if application_deadline > start_date:
|
|
self.add_error('application_deadline',
|
|
'The application deadline must be set BEFORE the job start date.')
|
|
|
|
# # Validate that if status is ACTIVE, we have required fields
|
|
# status = cleaned_data.get('status')
|
|
# if status == 'ACTIVE':
|
|
# if not cleaned_data.get('application_url'):
|
|
# self.add_error('application_url',
|
|
# 'Application URL is required for active jobs.')
|
|
# if not cleaned_data.get('description'):
|
|
# self.add_error('description',
|
|
# 'Job description is required for active jobs.')
|
|
|
|
return cleaned_data
|