Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding questionnaire activity types #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ project/*/migrations/*
!project/boh/migrations/0004_v1_0_2.py
!project/boh/migrations/0005_v1_0_3.py
!project/boh/migrations/0006_v0_0_6.py
!project/boh/migrations/0007_v0_0_7.py
.idea

project/project/settings/dev_aparsons.py
Expand Down
2 changes: 1 addition & 1 deletion project/boh/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class ActivityTypeAdmin(admin.ModelAdmin):
class ActivityAdmin(admin.ModelAdmin):
date_hierarchy = 'open_date'
fieldsets = [
(None, {'fields': ['engagement', 'activity_type', 'status', 'users']}),
(None, {'fields': ['engagement', 'activity_type', 'status', 'questionnaire', 'users']}),
(_('Advanced options'), {
'classes': ['collapse'],
'fields': ['open_date', 'close_date', 'duration']
Expand Down
8 changes: 6 additions & 2 deletions project/boh/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class Meta:
class ActivityEditForm(forms.ModelForm):
class Meta:
model = models.Activity
fields = ['status', 'activity_type', 'description', 'users']
fields = ['status', 'activity_type', 'description', 'users', 'questionnaire']
labels = {
'users': _('Assigned users')
}
Expand All @@ -399,6 +399,10 @@ class Meta:
model = models.Activity
fields = ['status']

class ActivityQuestionnaireSaveForm(forms.ModelForm):
class Meta:
model = models.Activity
fields = ['questionnaire']

class ActivityCommentAddForm(forms.ModelForm):
class Meta:
Expand Down Expand Up @@ -494,7 +498,7 @@ class Meta:
class ActivityTypeForm(forms.ModelForm):
class Meta:
model = models.ActivityType
fields = ['name', 'documentation']
fields = ['name', 'documentation', 'questionnaire_template']


class ActivityTypeDeleteForm(forms.ModelForm):
Expand Down
25 changes: 25 additions & 0 deletions project/boh/migrations/0007_v0_0_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-03-20 01:45
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('boh', '0006_v0_0_6'),
]

operations = [
migrations.AddField(
model_name='activity',
name='questionnaire',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='activitytype',
name='questionnaire_template',
field=models.TextField(blank=True, help_text='A GoSDL JSON format questionnaire'),
),
]
3 changes: 3 additions & 0 deletions project/boh/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ class ActivityType(TimeStampedModel, models.Model):

name = models.CharField(max_length=128, unique=True, help_text=_('A unique name for this activity type.'))
documentation = models.TextField(blank=True, help_text=_('Guidelines, procedures, and techniques for this activity type.'))
questionnaire_template = models.TextField(blank=True, help_text=_('A GoSDL JSON format questionnaire'))

objects = managers.ActivityTypeManager.from_queryset(managers.ActivityTypeQuerySet)()
metrics = managers.ActivityTypeMetrics.from_queryset(managers.ActivityTypeQuerySet)()
Expand Down Expand Up @@ -633,6 +634,7 @@ class Activity(models.Model):
open_date = models.DateTimeField(blank=True, null=True, help_text='The date and time when the status is changed to open.')
close_date = models.DateTimeField(blank=True, null=True, help_text='The date and time when the status is changed to closed.')
duration = models.DurationField(blank=True, null=True)
questionnaire = models.TextField(blank=True)

activity_type = models.ForeignKey(ActivityType)
engagement = models.ForeignKey(Engagement)
Expand Down Expand Up @@ -734,3 +736,4 @@ class ActivityComment(Comment):
"""Comment for a specific activity."""

activity = models.ForeignKey(Activity)

60 changes: 60 additions & 0 deletions project/boh/templates/boh/activity/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,66 @@ <h3><span class="fa fa-file-text-o" aria-hidden="true"></span> {% trans 'Documen
<p>{{ activity.activity_type.documentation|markdown_render }}</p>
{% endif %}

{% if activity.questionnaire %}
<hr>
<h3><span class="fa fa-check" aria-hidden="true"></span> {% trans 'Questionnaire' %}</h3>
<p id="questions"></p>
<form method="post" action="{% url 'boh:activity.questionnaire.save' activity.id %}">
{% csrf_token %}
<input name="questionnaire" id="questionnaire-result" type="hidden"></input>
<button class="btn btn-success" type="submit" onClick="saveQuestionnaire();"><span class="fa fa-check" aria-hidden="true"></span> {% trans 'Save' %}</button>
</form>
<style>
#questions ul {
list-style: none;
}
#questions input[type=checkbox] {
margin-right: 20px;
}
</style>
<script>
injectPoint = document.getElementById('questions');
questionnaire = {{ activity.questionnaire|safe }};
var questionCount = 0;
for (topic in questionnaire.questions) {
var node = document.createElement('h4');
var text = document.createTextNode(topic);
var list = document.createElement('ul');
node.appendChild(text);
injectPoint.appendChild(node);
injectPoint.appendChild(list);
for (question in questionnaire.questions[topic]) {
var question_node = document.createElement('li');
var question_text = document.createTextNode(questionnaire.questions[topic][question]);
var question_choice = document.createElement('input');
question_choice.type = "checkbox";
if (questionnaire.answers[questionCount] == true) {
question_choice.checked = true;
}
question_node.appendChild(question_choice);
question_node.appendChild(question_text);
list.appendChild(question_node);
questionCount++;
}

function saveQuestionnaire() {
var answers = [];
var topics = injectPoint.getElementsByTagName('ul');
for (var i = 0; i < topics.length; i++) {
var questions = topics[i].getElementsByTagName('input');
for (var j = 0; j < questions.length; j++) {
console.log(questions[j]);
answers.push(questions[j].checked);
}
}
questionnaire.answers = answers;
document.getElementById('questionnaire-result').setAttribute('value', JSON.stringify(questionnaire));
preventDefault();
}
}
</script>
{% endif %}

<hr>

<h3><span class="fa fa-comments" aria-hidden="true"></span> {% trans 'Comments' %}</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h3 class="panel-title">{% trans 'Add Activity Type' %}</h3>
<div class="panel-body">
{% include "boh/snippets/form/_general.html" with control=activity_type_form.name %}
{% include "boh/snippets/form/_markdown.html" with control=activity_type_form.documentation %}
{% include "boh/snippets/form/_general.html" with control=activity_type_form.questionnaire_template %}
</div>
<div class="panel-footer">
<button class="btn btn-primary" type="submit">{% trans 'Add Activity Type' %}</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h3 class="panel-title">{% trans 'Edit Activity Type' %}</h3>
<div class="panel-body">
{% include "boh/snippets/form/_general.html" with control=activity_type_form.name %}
{% include "boh/snippets/form/_markdown.html" with control=activity_type_form.documentation %}
{% include "boh/snippets/form/_general.html" with control=activity_type_form.questionnaire_template %}
</div>
<div class="panel-footer">
<button class="btn btn-primary" type="submit">{% trans 'Update Activity Type' %}</button>
Expand Down
1 change: 1 addition & 0 deletions project/boh/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
url(r'^activities/(?P<activity_id>\d+)/status/$', views.activity_status, name='activity.status'),
url(r'^activities/(?P<activity_id>\d+)/delete/$', views.activity_delete, name='activity.delete'),
url(r'^activities/(?P<activity_id>\d+)/comments/add/$', views.activity_comment_add, name='activity.comment.add'),
url(r'^activities/(?P<activity_id>\d+)/questionnaire/save/$', views.activity_questionnaire_save, name='activity.questionnaire.save'),

# People
url(r'^people/$', views.person_list, name='person.list'),
Expand Down
14 changes: 14 additions & 0 deletions project/boh/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ def activity_add(request, engagement_id):
if request.method == 'POST' and form.is_valid():
activity = form.save(commit=False)
activity.engagement = engagement
activity.questionnaire = activity.activity_type.questionnaire_template
activity.save()
form.save_m2m() # https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#the-save-method
messages.success(request, _('You successfully added this activity.'), extra_tags=random.choice(success_messages))
Expand Down Expand Up @@ -1681,6 +1682,19 @@ def activity_comment_add(request, activity_id):

return redirect('boh:activity.detail', activity_id=activity.id)

@login_required
@require_http_methods(['POST'])
def activity_questionnaire_save(request, activity_id):
activity = get_object_or_404(models.Activity, pk=activity_id)

form = forms.ActivityQuestionnaireSaveForm(request.POST, instance=activity)

if form.is_valid():
activity = form.save()
messages.success(request, _('You successfully updated the questionnaire on this activity.'), extra_tags=random.choice(success_messages))

return redirect('boh:activity.detail', activity_id=activity.id)


# People

Expand Down
2 changes: 1 addition & 1 deletion project/project/settings/dev_aparsons.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
'django_filters',
'widget_tweaks',

'debug_toolbar',
#'debug_toolbar',
)
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Django>=1.11.18
Django==1.9.13
django-formtools==1.0
djangorestframework==3.3.3
django-filter==0.13.0
Expand Down
2 changes: 1 addition & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r base.txt

mysqlclient==1.3.13
mysqlclient==1.4.2.post1