From 2c52e6bbaa052fb8ea2490335822047c56ec7949 Mon Sep 17 00:00:00 2001 From: Tibor Leupold Date: Tue, 31 Dec 2019 10:52:29 -1000 Subject: [PATCH 1/3] Django registration not compatible with Django 3 This commit mirrors the commit a18b64145199b4ecba37979dbd64620a08abfe53 by @bbelderbos which fixes this issue in days/053-056. --- days/069-072-django-rest/demo/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/days/069-072-django-rest/demo/requirements.txt b/days/069-072-django-rest/demo/requirements.txt index 2241bd23..b4433424 100644 --- a/days/069-072-django-rest/demo/requirements.txt +++ b/days/069-072-django-rest/demo/requirements.txt @@ -1,5 +1,5 @@ -django -django-registration +django==2.2 +django-registration==3.0.1 djangorestframework requests django-rest-swagger From 70ee2b6cdc91c4196975e0b676dcca7eb281aff1 Mon Sep 17 00:00:00 2001 From: Tibor Leupold Date: Tue, 31 Dec 2019 11:15:19 -1000 Subject: [PATCH 2/3] Show errors in registration form error This commit is mirroring ab86ca9b2a2083db633849e6505d43b6e9ee2830 in days/053-056. Previously, the registration form would not show any field errors that might occur during registration. This commit updates the `registration_form.html` template in the demo as well as the starter code to show occurring field errors. --- .../registration_form.html | 21 ++++++++++++++++++ days/069-072-django-rest/starter_code.zip | Bin 24296 -> 23639 bytes 2 files changed, 21 insertions(+) diff --git a/days/069-072-django-rest/demo/mysite/templates/django_registration/registration_form.html b/days/069-072-django-rest/demo/mysite/templates/django_registration/registration_form.html index 5df14191..0425b7aa 100644 --- a/days/069-072-django-rest/demo/mysite/templates/django_registration/registration_form.html +++ b/days/069-072-django-rest/demo/mysite/templates/django_registration/registration_form.html @@ -14,19 +14,40 @@

Sign up today!

{{ form.username.label }} {{ form.username }} +
+ {% for error in form.username.errors %} + {{ error }} + {% endfor %} +
{{ form.email.label }} {{ form.email }} +
+ {% for error in form.email.errors %} + {{ error }} + {% endfor %} +
{{ form.password1.label }} {{ form.password1 }} +
+ {% for error in form.password1.errors %} + {{ error }} + {% endfor %} +
{{ form.password2.label }} {{ form.password2 }} +
+ {% for error in form.password2.errors %} + {{ error }} + {% endfor %} +
+ diff --git a/days/069-072-django-rest/starter_code.zip b/days/069-072-django-rest/starter_code.zip index 9e760e39c035f6efb9128f73c84e7134ec1e27dc..79e83ebf3bda4ec0a58a54f2a7371c4a943438f9 100644 GIT binary patch delta 8286 zcmbVQ2~<;88h&9*1VTUp1P}y46a^&gV3b4@wIYI6!4^@(XrpWj+B&1f)QW8#ta=@_ zE}#zJnYxXQTKBEus8d_(q3YD7uB|hQ)$xqWD0TjuyyV^c^75(;CobpkegAsz{lEXt zLCJ+ZlDxcO&Mxi@{`B$@_%{6*Z={t3SQ%K0 zXu2d*FO@Y)2Y#gicn(Wpahvahr`7H9+0}*`z0G^p8?$j?WB?fVaSDt9_wg_PHo%wh zUgoTfwdUuy0bpU7vq}&koX{`~=w?j^Yrv#&Ug88PReZ86yWWXmrny>D>M$yBQf(-o zBA5zcyWPx@p&U1FzU^6V=}sf1vRU;Hrl)}I_g%S%rnVl9RP%POZBD&pR$DKFzkepj(Z0(Ad-uglg|5{4;q7R#u{ zOxK@Zm1jN~m;?3d{I^u#m`Tz4K98tVTNsMdX1FzE%jwG-$Z z2byVfOUb7>6tDznbW!qC^+0n-w1jb%5~6yQC!c0PU>16;>H<`kTv`-GKvN+~Wl4RK zYEor|UNGwm0`)-VzpG6>5r}F;$%ss;EOo4TuZGuMs3>S25I%4^kUkg4j*Aj@H3u4i zyn^{@Hzfpr;S4;6F?k!eLxa|IB9G_&!p0FmZ2R9>wC1#{dq6@*+G? zRlXNL-AB9DffGs1o6gS%W9#ATxcD4hI9eTMSGgl$)i@r%xy@BTQzuF_(B@eB5 zFW%Gj?3Gb-+{e16UI^`e+)Fb2NcPGl`|qw9-#PbZ=Y8#NpGO`)lk(lZk23uybefyq zCa-AkbMknbYV*V7yj^qhhDl{Uxs$`Pzz_W867D{I9aVKMwBA-f9%Xhi7rk})KNf!w zudI8bY2ESh^wA%0nQT1pp<;sj)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zun4&d#^|zGCiQ)rp*E)D5!*Mk!9s#=AxVLc@RH?g zOuH&1=3Y#nOgWE5OsalzC2g>f=;f%j;I-b9=&SWEbicqySTD=H;m0iy_FWex&l(|& z;q{`u7c!}tfRXk3Fv820$I%uHiMu!srOXOodEm#0<_sXEF@9LH`F9)wR+9 From d9ee51b08ed05003603cc35a699c536a73486b1e Mon Sep 17 00:00:00 2001 From: Tibor Leupold Date: Tue, 31 Dec 2019 11:36:07 -1000 Subject: [PATCH 3/3] Hide user field in api front-end and set default Before, it was possible in the api font-end for a logged-in user to set the user value of a quote during create or update. This means, the user could set another user as the owner of the quote. This behavior seems unintended. To prevent this option, the user field is defined as a hidden field in the serializer. Hidden fields will not show up in the api frontend form. A quote should have a user assigned to it (even if it is not required on the model level) to allow management (update/delete) of the quote. The quote can only be managed by it's owner. To prevent quotes without an owner, the hidden user field is assigned the default value of the currently logged-in user in the serializer. --- days/069-072-django-rest/demo/api/serializers.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/days/069-072-django-rest/demo/api/serializers.py b/days/069-072-django-rest/demo/api/serializers.py index 7d811729..baebb6d0 100644 --- a/days/069-072-django-rest/demo/api/serializers.py +++ b/days/069-072-django-rest/demo/api/serializers.py @@ -5,6 +5,13 @@ class QuoteSerializer(serializers.ModelSerializer): + # This is not included in the videos. Without this setting, it was possible + # to set the user value to something other than the currently logged-in + # user. This setting hides the user field from the form in the API frontend + # and sets the currently logged-in users as the field value by default. + # See also: https://stackoverflow.com/a/53193276 + user = serializers.HiddenField(default=serializers.CurrentUserDefault()) + class Meta: model = Quote fields = ('quote', 'author', 'source', 'cover', 'user')