Skip to content

Commit

Permalink
refactors vies to use EnumeratedQuestions (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
smirolo committed Feb 23, 2018
1 parent d5f87c6 commit 90b1027
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 161 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2017, DjaoDjin inc.
Copyright (c) 2018, DjaoDjin inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -46,7 +46,7 @@ $(srcDir)/credentials: $(srcDir)/testsite/etc/credentials
# XXX Enter a superuser when asked otherwise the fixtures won't load
# correctly.
initdb: install-conf
-rm -f db.sqlite3
-rm -f $(srcDir)/db.sqlite3
cd $(srcDir) && $(PYTHON) ./manage.py migrate $(RUNSYNCDB) --noinput
cd $(srcDir) && $(PYTHON) ./manage.py loaddata \
testsite/fixtures/initial_data.json
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ Five minutes evaluation

The source code is bundled with a sample django project.

$ virtualenv-2.7 *virtual_env_dir*
$ virtualenv *virtual_env_dir*
$ cd *virtual_env_dir*
$ source bin/activate
$ pip install -r requirements.txt

$ python manage.py syncdb
$ pip install -r testsite/requirements.txt
$ make initdb
$ python manage.py runserver

# Visit url at http://localhost:8000/

# You can use username: donny, password: yoyo to test the manager options.

Releases
========
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
15 changes: 12 additions & 3 deletions survey/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand All @@ -24,9 +24,18 @@

from django.contrib import admin

from survey.models import Answer, Question, Sample, Campaign
from survey.models import (Answer, Campaign, Choice, EditableFilter,
EditablePredicate, EnumeratedQuestions, Question, Matrix, Metric,
Sample, Unit)

admin.site.register(Answer)
admin.site.register(Campaign)
admin.site.register(Choice)
admin.site.register(EditableFilter)
admin.site.register(EditablePredicate)
admin.site.register(EnumeratedQuestions)
admin.site.register(Question)
admin.site.register(Matrix)
admin.site.register(Metric)
admin.site.register(Sample)
admin.site.register(Answer)
admin.site.register(Unit)
2 changes: 1 addition & 1 deletion survey/api/matrix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion survey/compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
14 changes: 1 addition & 13 deletions survey/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,12 @@ class QuestionForm(forms.ModelForm):

class Meta:
model = get_question_model()
exclude = ['survey', 'rank']

def save(self, commit=True):
if 'survey' in self.initial:
self.instance.survey = self.initial['survey']
if 'rank' in self.initial and not self.instance.rank:
self.instance.rank = self.initial['rank']
return super(QuestionForm, self).save(commit)
fields = ('path', 'title', 'text', 'default_metric', 'extra')

def clean_choices(self):
self.cleaned_data['choices'] = self.cleaned_data['choices'].strip()
return self.cleaned_data['choices']

def clean_correct_answer(self):
self.cleaned_data['correct_answer'] \
= self.cleaned_data['correct_answer'].strip()
return self.cleaned_data['correct_answer']


class SampleCreateForm(forms.ModelForm):

Expand Down
58 changes: 32 additions & 26 deletions survey/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

from django.core.exceptions import ImproperlyConfigured
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _
from django.views.generic.detail import SingleObjectMixin
from rest_framework.generics import get_object_or_404

from . import settings
from .models import Matrix, EditableFilter, Sample, Campaign
from .models import (Campaign, EnumeratedQuestions, EditableFilter, Matrix,
Sample)
from .utils import get_account_model, get_question_model


Expand Down Expand Up @@ -114,7 +114,8 @@ def get_interviewee(self):
try:
kwargs = {'%s__exact' % settings.ACCOUNT_LOOKUP_FIELD:
self.kwargs.get(self.interviewee_slug)}
interviewee = get_object_or_404(account_model, **kwargs)
interviewee = get_object_or_404(
account_model.objects.all(), **kwargs)
except account_model.DoesNotExist:
interviewee = self.request.user
else:
Expand All @@ -140,21 +141,6 @@ def get_url_context(self):
return kwargs


class QuestionMixin(SingleObjectMixin):

num_url_kwarg = 'num'
survey_url_kwarg = 'survey'

def get_object(self, queryset=None):
"""
Returns a question object based on the URL.
"""
rank = self.kwargs.get(self.num_url_kwarg, 1)
slug = self.kwargs.get(self.survey_url_kwarg, None)
survey = get_object_or_404(Campaign, slug__exact=slug)
return get_object_or_404(get_question_model(), survey=survey, rank=rank)


class CampaignMixin(object):
"""
Returns a ``Campaign`` object associated with the request URL.
Expand All @@ -168,10 +154,29 @@ def campaign(self):
return self._campaign

def get_survey(self):
return get_object_or_404(Campaign, slug=self.kwargs.get(
return get_object_or_404(Campaign.objects.all(), slug=self.kwargs.get(
self.survey_url_kwarg))


class CampaignQuestionMixin(CampaignMixin):

num_url_kwarg = 'num'
model = EnumeratedQuestions

def get_queryset(self):
return self.model.objects.filter(
campaign=self.campaign).order_by('rank')

def get_object(self, queryset=None):
"""
Returns a question object based on the URL.
"""
if not queryset:
queryset = self.get_queryset()
return get_object_or_404(queryset,
rank=self.kwargs.get(self.num_url_kwarg, 1))


class SampleMixin(IntervieweeMixin, CampaignMixin):
"""
Returns a ``Sample`` to a ``Campaign``.
Expand Down Expand Up @@ -207,7 +212,7 @@ def get_sample(self, url_kwarg=None):
# Well no id, let's see if we can find a sample from
# a survey slug and a account
interviewee = self.get_interviewee()
sample = get_object_or_404(Sample,
sample = get_object_or_404(Sample.objects.all(),
survey=self.get_survey(), account=interviewee)
return sample

Expand All @@ -219,7 +224,7 @@ def get_reverse_kwargs(self):
return [self.interviewee_slug, 'survey', self.sample_url_kwarg]


class MatrixQuerysetMixin(AccountMixin):
class MatrixQuerysetMixin(object):

@staticmethod
def get_queryset():
Expand All @@ -233,18 +238,19 @@ class MatrixMixin(MatrixQuerysetMixin):
@property
def matrix(self):
if not hasattr(self, '_matrix'):
self._matrix = get_object_or_404(Matrix,
self._matrix = get_object_or_404(self.get_queryset(),
slug=self.kwargs.get(self.matrix_url_kwarg))
return self._matrix


class EditableFilterMixin(AccountMixin):
class EditableFilterMixin(object):

editable_filter_url_kwarg = 'editable_filter'

@property
def editable_filter(self):
if not hasattr(self, '_editable_filter'):
self._editable_filter = get_object_or_404(EditableFilter,
slug=self.kwargs.get(self.editable_filter_url_kwarg))
self._editable_filter = get_object_or_404(
EditableFilter.objects.all(),
slug=self.kwargs.get(self.editable_filter_url_kwarg))
return self._editable_filter
22 changes: 10 additions & 12 deletions survey/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,21 @@ class Meta:
text = models.TextField(
help_text=_("Detailed description about the question"))
question_type = models.CharField(
max_length=9, choices=QUESTION_TYPES, default=TEXT,
max_length=9, choices=QUESTION_TYPES, default=RADIO,
help_text=_("Choose the type of answser."))
correct_answer = models.ForeignKey(Choice, null=True)
correct_answer = models.ForeignKey(Choice, null=True, blank=True)
default_metric = models.ForeignKey(Metric)
extra = settings.get_extra_field_class()(null=True)
extra = settings.get_extra_field_class()(null=True, blank=True)

def __str__(self):
return self.path

def get_choices(self):
choices_list = []
if self.choices:
#pylint: disable=no-member
for choice in self.choices.split('\n'):
choice = choice.strip()
choices_list += [(choice, choice)]
return choices_list
@property
def choices(self):
if self.default_metric.unit.system == Unit.SYSTEM_ENUMERATED:
return [choice.text for choice
in Choice.objects.filter(unit=self.default_metric.unit)]
return None

def get_correct_answer(self):
correct_answer_list = []
Expand All @@ -181,7 +179,7 @@ def get_correct_answer(self):
class Question(AbstractQuestion):

# XXX Before migration:
pass
# pass
# XXX After migration
class Meta(AbstractQuestion.Meta):
swappable = 'QUESTION_MODEL'
Expand Down
2 changes: 1 addition & 1 deletion survey/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
File renamed without changes.
File renamed without changes.
26 changes: 13 additions & 13 deletions survey/templates/survey/question_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
{% block content %}
<section>
<div>
<h1>Questions for {{survey.title}}</h1>
<h1>Questions for {{campaign.title}}</h1>
</div>

<p><a href="{% url 'survey_question_new' survey %}">Add a question</a></p>
<p><a href="{% url 'survey_question_new' campaign %}">Add a question</a></p>
<table>
<tr>
<th>Question</th>
Expand All @@ -18,37 +18,37 @@ <h1>Questions for {{survey.title}}</h1>
<th>Order down</th>
<th>Order</th>
</tr>
{% if question_list %}
{% for question in question_list %}
{% if enumeratedquestions_list %}
{% for campaign_question in enumeratedquestions_list %}
<tr>
<td>{{question.text}}</td>
<td>{{question.question_type}}</td>
<td>{% if question.choices %}{{question.choices}}{% else %}N/A{% endif %}</td>
<td><a href="{% url 'survey_question_edit' survey question.id %}" >Edit</a></td>
<td>{{campaign_question.question.text}}</td>
<td>{{campaign_question.question.question_type}}</td>
<td>{% if campaign_question.question.choices %}{{campaign_question.question.choices}}{% else %}N/A{% endif %}</td>
<td><a href="{% url 'survey_question_edit' campaign campaign_question.rank %}" >Edit</a></td>
<td>
<form method="post" action="{% url 'survey_question_delete' survey question.id %}">
<form method="post" action="{% url 'survey_question_delete' campaign campaign_question.rank %}">
<input type="submit" value="Delete" />
{% csrf_token %}
</form>
</td>
<td>
<form method="post" action="{% url 'survey_question_up' survey question.id %}">
<form method="post" action="{% url 'survey_question_up' campaign campaign_question.rank %}">
<input type="submit" value="Up" />
{% csrf_token %}
</form>
</td>
<td>
<form method="post" action="{% url 'survey_question_down' survey question.id %}">
<form method="post" action="{% url 'survey_question_down' campaign campaign_question.rank %}">
<input type="submit" value="Down" />
{% csrf_token %}
</form>
</td>
<td>{{question.rank}}</td>
<td>{{campaign_question.question.rank}}</td>
</tr>
{% endfor %}
</table>
{% else %}
<em>No question for {{survey.title}} survey</em>
<em>No question for campaign titled &quot;{{campaign.title}}&quot;</em>
{% endif %}
</section>
{% endblock %}
2 changes: 1 addition & 1 deletion survey/templatetags/survey_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion survey/urls/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion survey/urls/manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion survey/urls/matrix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion survey/urls/sample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017, DjaoDjin inc.
# Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
Loading

0 comments on commit 90b1027

Please sign in to comment.