From 608005863509ec958fa8b670fe3d7fb4b2a6bda2 Mon Sep 17 00:00:00 2001 From: Paul Konstantin Gerke Date: Thu, 20 Oct 2022 10:53:10 +0200 Subject: [PATCH] Refactor handle_api_key_post, fix type annotation --- babybuddy/views.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/babybuddy/views.py b/babybuddy/views.py index 78197112..d7324fba 100644 --- a/babybuddy/views.py +++ b/babybuddy/views.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from xmlrpc.client import Boolean from django.contrib import messages from django.contrib.auth import update_session_auth_hash from django.contrib.auth.forms import PasswordChangeForm @@ -149,16 +148,22 @@ class UserPassword(LoginRequiredMixin, View): return render(request, self.template_name, {"form": form}) -class RegenerateApiKey: - def handle_api_key_post(self, request) -> Boolean: - if request.POST.get("api_key_regenerate"): - request.user.settings.api_key(reset=True) - messages.success(request, _("User API key regenerated.")) - return True - return False +def handle_api_regenerate_request(request) -> bool: + """ + Checks if the current request contains a request to update the API key + and if it does, updeates the API key. + + Returns True, if the API-key regenerate request was detected and handled. + """ + + if request.POST.get("api_key_regenerate"): + request.user.settings.api_key(reset=True) + messages.success(request, _("User API key regenerated.")) + return True + return False -class UserSettings(LoginRequiredMixin, RegenerateApiKey, View): +class UserSettings(LoginRequiredMixin, View): """ Handles both the User and Settings models. Based on this SO answer: https://stackoverflow.com/a/45056835. @@ -181,7 +186,7 @@ class UserSettings(LoginRequiredMixin, RegenerateApiKey, View): ) def post(self, request): - if self.handle_api_key_post(request): + if handle_api_regenerate_request(request): return redirect("babybuddy:user-settings") form_user = self.form_user_class(instance=request.user, data=request.POST) @@ -204,7 +209,7 @@ class UserSettings(LoginRequiredMixin, RegenerateApiKey, View): ) -class UserAddDevice(LoginRequiredMixin, RegenerateApiKey, View): +class UserAddDevice(LoginRequiredMixin, View): form_user_class = forms.UserForm template_name = "babybuddy/user_add_device.html" qr_code_template = "babybuddy/login_qr_code.txt" @@ -223,7 +228,7 @@ class UserAddDevice(LoginRequiredMixin, RegenerateApiKey, View): ) def post(self, request): - if self.handle_api_key_post(request): + if handle_api_regenerate_request(request): return redirect("babybuddy:user-add-device") else: raise BadRequest()