Browse Source

allow variable number of screening questions

master
aatish 4 years ago
parent
commit
7b33b832fe
  1. 5
      jobs/forms.py
  2. 2
      jobs/models.py
  3. 28
      jobs/templates/jobs/job_form.html

5
jobs/forms.py

@ -24,4 +24,7 @@ class QuestionForm(forms.ModelForm):
QuestionFormSet = inlineformset_factory(
Job, Question, form=QuestionForm, can_delete=False, can_order=False)
Job, Question, form=QuestionForm, can_delete=False, can_order=False)
QuestionUpdateFormSet = inlineformset_factory(
Job, Question, form=QuestionForm, can_delete=True, can_order=False)

2
jobs/models.py

@ -55,7 +55,7 @@ class Job(models.Model):
class Question(models.Model):
''' A model to hold screening questions for a job post '''
name = models.TextField()
name = models.TextField('Question Title')
job = models.ForeignKey(
Job, related_name='questions', on_delete=models.CASCADE)

28
jobs/templates/jobs/job_form.html

@ -11,9 +11,37 @@
{{ form |crispy }}
<h4>Screening Questions</h4>
{{ question_form |crispy }}
<a id="add_more" href="">Add More</a><br/>
<input class="btn btn-primary submit" type="submit" value="Post Job"/>
</form>
{{form.media}}
</div>
</div>
<script>
// modified from https://stackoverflow.com/a/669982
function cloneMore(selector, type) {
var newElement = $(selector).clone(true);
var total = $('#id_' + type + '-TOTAL_FORMS').val();
newElement.find(':input').each(function () {
var name = $(this).attr('name').replace('-' + (total - 1) + '-', '-' + total + '-');
var id = 'id_' + name;
$(this).attr({ 'name': name, 'id': id }).val('').removeAttr('checked');
});
newElement.find('label').each(function () {
var newFor = $(this).attr('for').replace('-' + (total - 1) + '-', '-' + total + '-');
$(this).attr('for', newFor);
});
total++;
$('#id_' + type + '-TOTAL_FORMS').val(total);
$(selector).after(newElement);
}
$(document).ready(function() {
$('#add_more').click(function () {
cloneMore('.multiField:last', 'questions');
return false;
});
})
</script>
{% endblock %}
Loading…
Cancel
Save