From 4e1a21ee8157ce233bc951301ec8c668c5484c04 Mon Sep 17 00:00:00 2001 From: Frederic Date: Mon, 29 Apr 2019 00:54:57 +0200 Subject: [PATCH] add setter for hashed password, use it in admin --- bam/admin.py | 9 ++++++++- bam/models.py | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bam/admin.py b/bam/admin.py index cd01e0b..5d7d4b2 100644 --- a/bam/admin.py +++ b/bam/admin.py @@ -1,22 +1,28 @@ from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import User +from django.contrib.auth.hashers import make_password from django import forms from .models import Account class AccountInlineForm(forms.ModelForm): - hased_email_set = forms.EmailField(label='Set hashed email address') + hashed_email_set = forms.EmailField(label='Set hashed email address') class Meta: model = Account fields = ['hashed_email'] + def save(self, commit): + self.instance.set_hashed_email(self.cleaned_data['hashed_email_set']) + return super().save(commit) + class AccountInline(admin.StackedInline): model = Account can_delete = False form = AccountInlineForm + readonly_fields = ['hashed_email'] class UserAdmin(BaseUserAdmin): @@ -25,3 +31,4 @@ class UserAdmin(BaseUserAdmin): admin.site.unregister(User) admin.site.register(User, UserAdmin) +admin.site.register(Account) diff --git a/bam/models.py b/bam/models.py index 8cff54b..2f2fe82 100644 --- a/bam/models.py +++ b/bam/models.py @@ -1,5 +1,6 @@ from django.db import models from django.contrib.auth.models import User +from django.contrib.auth.hashers import make_password class Account(models.Model): @@ -8,3 +9,6 @@ class Account(models.Model): def __str__(self): return '%s' % (self.user.username) + + def set_hashed_email(self, email): + self.hashed_email = make_password(email)