From e58b3877ab51f64f00ab2ad6f635446cac4c9037 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Fri, 14 Sep 2018 23:30:22 +0200 Subject: [PATCH] Feat - Update to django 2+ Fix #15, #16, #17 --- requirements.txt | 2 +- settings.py | 3 +-- survey/forms.py | 6 ++--- survey/migrations/0001_initial.py | 24 +++++++++---------- survey/models/answer.py | 6 +++-- survey/models/category.py | 3 ++- survey/models/question.py | 6 +++-- survey/models/response.py | 6 +++-- survey/models/survey.py | 4 ++-- .../management/commands/test_exportresult.py | 2 +- survey/views/index_view.py | 2 +- survey/views/survey_detail.py | 4 ++-- urls.py | 2 +- 13 files changed, 38 insertions(+), 32 deletions(-) diff --git a/requirements.txt b/requirements.txt index d549d79a..a3dec2a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/settings.py b/settings.py index 9d1b17d2..c6895523 100644 --- a/settings.py +++ b/settings.py @@ -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', ) diff --git a/survey/forms.py b/survey/forms.py index b00ece3f..d5e8d023 100644 --- a/survey/forms.py +++ b/survey/forms.py @@ -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 @@ -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) @@ -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) diff --git a/survey/migrations/0001_initial.py b/survey/migrations/0001_initial.py index 42f269a2..f4f18b61 100644 --- a/survey/migrations/0001_initial.py +++ b/survey/migrations/0001_initial.py @@ -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'), @@ -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)), ], @@ -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)), ], @@ -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',), @@ -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',), @@ -129,7 +129,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',), @@ -137,32 +137,32 @@ class Migration(migrations.Migration): 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), ), ] diff --git a/survey/models/answer.py b/survey/models/answer.py index 0f1d8c88..dc446718 100644 --- a/survey/models/answer.py +++ b/survey/models/answer.py @@ -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) diff --git a/survey/models/category.py b/survey/models/category.py index ee22f6ef..26966e70 100644 --- a/survey/models/category.py +++ b/survey/models/category.py @@ -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, diff --git a/survey/models/question.py b/survey/models/question.py index b58f8288..cae60452 100644 --- a/survey/models/question.py +++ b/survey/models/question.py @@ -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) diff --git a/survey/models/response.py b/survey/models/response.py index 1369b557..5e170aaa 100644 --- a/survey/models/response.py +++ b/survey/models/response.py @@ -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) diff --git a/survey/models/survey.py b/survey/models/survey.py index 616d30bc..3f4cfee0 100644 --- a/survey/models/survey.py +++ b/survey/models/survey.py @@ -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 _ @@ -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}) diff --git a/survey/tests/management/commands/test_exportresult.py b/survey/tests/management/commands/test_exportresult.py index 6e11ae8b..85163f6d 100755 --- a/survey/tests/management/commands/test_exportresult.py +++ b/survey/tests/management/commands/test_exportresult.py @@ -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 = """\ diff --git a/survey/views/index_view.py b/survey/views/index_view.py index 4c5c1bf9..4941e1a4 100644 --- a/survey/views/index_view.py +++ b/survey/views/index_view.py @@ -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 diff --git a/survey/views/survey_detail.py b/survey/views/survey_detail.py index 78a76485..9fdcbf94 100644 --- a/survey/views/survey_detail.py +++ b/survey/views/survey_detail.py @@ -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, @@ -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, diff --git a/urls.py b/urls.py index 8640ae75..3ae01586 100644 --- a/urls.py +++ b/urls.py @@ -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), ]