Skip to content

Commit

Permalink
Feat - Update to django 2+
Browse files Browse the repository at this point in the history
Fix #15, #16, #17
  • Loading branch information
Pierre-Sassoulas committed Sep 16, 2018
1 parent 8290323 commit e58b387
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Django>=1.11,<2.0
Django==2.1.1
django-bootstrap-form==3.2.1
django-tastypie==0.14.0 # API
django-registration==2.2 # account logic, views and workflows
Expand Down
3 changes: 1 addition & 2 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@
},
]

MIDDLEWARE_CLASSES = (
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
Expand Down
6 changes: 3 additions & 3 deletions survey/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import uuid

from django import forms
from django.core.urlresolvers import reverse
from django.forms import models
from django.urls import reverse
from django.utils.text import slugify

from survey.models import Answer, Question, Response
Expand Down Expand Up @@ -56,7 +56,7 @@ def _get_preexisting_response(self):
The user must be logged.
:rtype: Response or None"""
if not self.user.is_authenticated():
if not self.user.is_authenticated:
return None
try:
return Response.objects.get(user=self.user, survey=self.survey)
Expand Down Expand Up @@ -206,7 +206,7 @@ def save(self, commit=True):
response = super(ResponseForm, self).save(commit=False)
response.survey = self.survey
response.interview_uuid = self.uuid
if self.user.is_authenticated():
if self.user.is_authenticated:
response.user = self.user
response.save()
# response "raw" data as dict (for signal)
Expand Down
24 changes: 12 additions & 12 deletions survey/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Migration(migrations.Migration):
('required', models.BooleanField()),
('question_type', models.CharField(default=b'text', max_length=200, choices=[(b'text', 'text (multiple line)'), (b'short-text', 'short text (one line)'), (b'radio', 'radio'), (b'select', 'select'), (b'select-multiple', 'Select Multiple'), (b'select_image', 'Select Image'), (b'integer', 'integer')])),
('choices', models.TextField(help_text="if the question type is 'radio', 'select', or 'select multiple' provide a comma-separated list of options for this question .", null=True, blank=True)),
('category', models.ForeignKey(blank=True, to='survey.Category', null=True)),
('category', models.ForeignKey(blank=True, to='survey.Category', null=True, on_delete=models.SET_NULL)),
],
options={
'ordering': ('survey', 'order'),
Expand Down Expand Up @@ -87,7 +87,7 @@ class Migration(migrations.Migration):
fields=[
('answerbase_ptr', models.OneToOneField(
parent_link=True, auto_created=True, primary_key=True,
serialize=False, to='survey.AnswerBase')
serialize=False, to='survey.AnswerBase', on_delete=models.CASCADE)
),
('body', models.IntegerField(null=True, blank=True)),
],
Expand All @@ -98,7 +98,7 @@ class Migration(migrations.Migration):
fields=[
('answerbase_ptr', models.OneToOneField(
parent_link=True, auto_created=True, primary_key=True,
serialize=False, to='survey.AnswerBase')
serialize=False, to='survey.AnswerBase', on_delete=models.CASCADE)
),
('body', models.TextField(null=True, blank=True)),
],
Expand All @@ -109,7 +109,7 @@ class Migration(migrations.Migration):
fields=[
('answerbase_ptr', models.OneToOneField(
parent_link=True, auto_created=True, primary_key=True,
serialize=False, to='survey.AnswerBase')),
serialize=False, to='survey.AnswerBase', on_delete=models.CASCADE)),
('body', models.TextField(null=True, blank=True)),
],
bases=('survey.answerbase',),
Expand All @@ -119,7 +119,7 @@ class Migration(migrations.Migration):
fields=[
('answerbase_ptr', models.OneToOneField(
parent_link=True, auto_created=True, primary_key=True,
serialize=False, to='survey.AnswerBase')),
serialize=False, to='survey.AnswerBase', on_delete=models.CASCADE)),
('body', models.TextField(null=True, blank=True)),
],
bases=('survey.answerbase',),
Expand All @@ -129,40 +129,40 @@ class Migration(migrations.Migration):
fields=[
('answerbase_ptr', models.OneToOneField(
parent_link=True, auto_created=True, primary_key=True,
serialize=False, to='survey.AnswerBase')),
serialize=False, to='survey.AnswerBase', on_delete=models.CASCADE)),
('body', models.TextField(null=True, blank=True)),
],
bases=('survey.answerbase',),
),
migrations.AddField(
model_name='response',
name='survey',
field=models.ForeignKey(to='survey.Survey'),
field=models.ForeignKey(to='survey.Survey', on_delete=models.CASCADE),
),
migrations.AddField(
model_name='response',
name='user',
field=models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL,
null=True),
null=True, on_delete=models.SET_NULL),
),
migrations.AddField(
model_name='question',
name='survey',
field=models.ForeignKey(to='survey.Survey'),
field=models.ForeignKey(to='survey.Survey', on_delete=models.CASCADE),
),
migrations.AddField(
model_name='category',
name='survey',
field=models.ForeignKey(to='survey.Survey'),
field=models.ForeignKey(to='survey.Survey', on_delete=models.CASCADE),
),
migrations.AddField(
model_name='answerbase',
name='question',
field=models.ForeignKey(to='survey.Question'),
field=models.ForeignKey(to='survey.Question', on_delete=models.CASCADE),
),
migrations.AddField(
model_name='answerbase',
name='response',
field=models.ForeignKey(to='survey.Response'),
field=models.ForeignKey(to='survey.Response', on_delete=models.CASCADE),
),
]
6 changes: 4 additions & 2 deletions survey/models/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

class Answer(models.Model):

question = models.ForeignKey(Question, verbose_name=_("Question"),
question = models.ForeignKey(Question, on_delete=models.CASCADE,
verbose_name=_("Question"),
related_name="answers")
response = models.ForeignKey(Response, verbose_name=_("Response"),
response = models.ForeignKey(Response, on_delete=models.CASCADE,
verbose_name=_("Response"),
related_name="answers")
created = models.DateTimeField(_("Creation date"), auto_now_add=True)
updated = models.DateTimeField(_("Update date"), auto_now=True)
Expand Down
3 changes: 2 additions & 1 deletion survey/models/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
class Category(models.Model):

name = models.CharField(_("Name"), max_length=400)
survey = models.ForeignKey(Survey, verbose_name=_("Survey"),
survey = models.ForeignKey(Survey, on_delete=models.CASCADE,
verbose_name=_("Survey"),
related_name="categories")
order = models.IntegerField(_("Display order"), blank=True, null=True)
description = models.CharField(_("Description"), max_length=2000,
Expand Down
6 changes: 4 additions & 2 deletions survey/models/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ class Question(models.Model):
text = models.TextField(_("Text"))
order = models.IntegerField(_("Order"),)
required = models.BooleanField(_("Required"),)
category = models.ForeignKey(Category, verbose_name=_("Category"),
category = models.ForeignKey(Category, on_delete=models.SET_NULL,
verbose_name=_("Category"),
blank=True, null=True,
related_name="questions")
survey = models.ForeignKey(Survey, verbose_name=_("Survey"),
survey = models.ForeignKey(Survey, on_delete=models.CASCADE,
verbose_name=_("Survey"),
related_name="questions")
type = models.CharField(_("Type"), max_length=200, choices=QUESTION_TYPES,
default=TEXT)
Expand Down
6 changes: 4 additions & 2 deletions survey/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class Response(models.Model):

created = models.DateTimeField(_("Creation date"), auto_now_add=True)
updated = models.DateTimeField(_("Update date"), auto_now=True)
survey = models.ForeignKey(Survey, verbose_name=_("Survey"),
survey = models.ForeignKey(Survey, on_delete=models.CASCADE,
verbose_name=_("Survey"),
related_name="responses")
user = models.ForeignKey(user_model, verbose_name=_("User"), null=True,
user = models.ForeignKey(user_model, on_delete=models.SET_NULL,
verbose_name=_("User"), null=True,
blank=True)
interview_uuid = models.CharField(_("Interview unique identifier"),
max_length=36)
Expand Down
4 changes: 2 additions & 2 deletions survey/models/survey.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _


Expand Down Expand Up @@ -30,6 +31,5 @@ def latest_answer_date(self):
min_ = response.updated
return min_

@models.permalink
def get_absolute_url(self):
return ('survey-detail', [self.pk])
return reverse('survey-detail', kwargs={"id": self.pk})
2 changes: 1 addition & 1 deletion survey/tests/management/commands/test_exportresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_handle(self):
if os.path.exists(second_csv):
os.remove(second_csv)
call_command("exportresult", "--survey-all", "--tex", "--csv",
"--force", configuration=self.test_conf_path)
"--force", configuration_file=self.test_conf_path)
self.assertMultiLineEqual(self.expected_content,
self.get_file_content(first_csv))
expected = """\
Expand Down
2 changes: 1 addition & 1 deletion survey/views/index_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IndexView(TemplateView):
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
surveys = Survey.objects.filter(is_published=True)
if not self.request.user.is_authenticated():
if not self.request.user.is_authenticated:
surveys = surveys.filter(need_logged_user=False)
context['surveys'] = surveys
return context
4 changes: 2 additions & 2 deletions survey/views/survey_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get(self, request, *args, **kwargs):
template_name = 'survey/survey.html'
else:
template_name = 'survey/one_page_survey.html'
if survey.need_logged_user and not request.user.is_authenticated():
if survey.need_logged_user and not request.user.is_authenticated:
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
categories = Category.objects.filter(survey=survey).order_by('order')
form = ResponseForm(survey=survey, user=request.user,
Expand All @@ -34,7 +34,7 @@ def get(self, request, *args, **kwargs):

def post(self, request, *args, **kwargs):
survey = get_object_or_404(Survey, is_published=True, id=kwargs['id'])
if survey.need_logged_user and not request.user.is_authenticated():
if survey.need_logged_user and not request.user.is_authenticated:
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
categories = Category.objects.filter(survey=survey).order_by('order')
form = ResponseForm(request.POST, survey=survey, user=request.user,
Expand Down
2 changes: 1 addition & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def home(request):
url('accounts/', include('django.contrib.auth.urls')),
url(r'^rosetta/', include('rosetta.urls')),
url(r'^survey/', include('survey.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
]

0 comments on commit e58b387

Please sign in to comment.