Skip to content

Commit

Permalink
Extraido lib para usuário customizado do DJango
Browse files Browse the repository at this point in the history
close #23
  • Loading branch information
renzo authored and renzon committed Jul 18, 2024
1 parent 933b143 commit 919c60d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 342 deletions.
211 changes: 4 additions & 207 deletions backend/devpro/base/admin.py
Original file line number Diff line number Diff line change
@@ -1,212 +1,9 @@
from django.conf import settings
from django.contrib import admin, messages
from django.contrib.admin.options import IS_POPUP_VAR
from django.contrib.admin.utils import unquote
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.admin import sensitive_post_parameters_m
from django.contrib.auth.forms import UserChangeForm, UserCreationForm, AdminPasswordChangeForm
from django.core.exceptions import PermissionDenied
from django.db import transaction, router
from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import path, reverse
from django.utils.decorators import method_decorator
from django.utils.html import escape
from django.views.decorators.csrf import csrf_protect
from django.contrib import admin
from django_min_custom_user.admin import MinUserAdmin

from devpro.base.models import User
from django.utils.translation import gettext_lazy as _, gettext

csrf_protect_m = method_decorator(csrf_protect)


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
add_form_template = "admin/auth/user/add_form.html"
change_user_password_template = None
fieldsets = (
(None, {"fields": ("email", "password")}),
(_("Personal info"), {"fields": ("first_name",)}),
(
_("Permissions"),
{
"fields": (
"is_active",
"is_staff",
"is_superuser",
"groups",
"user_permissions",
),
},
),
(_("Important dates"), {"fields": ("last_login", "date_joined")}),
)
add_fieldsets = (
(
None,
{
"classes": ("wide",),
"fields": ("email", "password1", "password2"),
},
),
)
form = UserChangeForm
add_form = UserCreationForm
change_password_form = AdminPasswordChangeForm
list_display = ("email", "first_name", "is_staff")
list_filter = ("is_staff", "is_superuser", "is_active", "groups")
search_fields = ("first_name", "email")
ordering = ("email",)
filter_horizontal = (
"groups",
"user_permissions",
)

def get_fieldsets(self, request, obj=None):
if not obj:
return self.add_fieldsets
return super().get_fieldsets(request, obj)

def get_form(self, request, obj=None, **kwargs):
"""
Use special form during user creation
"""
defaults = {}
if obj is None:
defaults["form"] = self.add_form
defaults.update(kwargs)
return super().get_form(request, obj, **defaults)

def get_urls(self):
return [
path(
"<id>/password/",
self.admin_site.admin_view(self.user_change_password),
name="auth_user_password_change",
),
] + super().get_urls()

# RemovedInDjango60Warning: when the deprecation ends, replace with:
# def lookup_allowed(self, lookup, value, request):
def lookup_allowed(self, lookup, value, request=None):
# Don't allow lookups involving passwords.
return not lookup.startswith("password") and super().lookup_allowed(
lookup, value, request
)

@sensitive_post_parameters_m
@csrf_protect_m
def add_view(self, request, form_url="", extra_context=None):
with transaction.atomic(using=router.db_for_write(self.model)):
return self._add_view(request, form_url, extra_context)

def _add_view(self, request, form_url="", extra_context=None):
# It's an error for a user to have add permission but NOT change
# permission for users. If we allowed such users to add users, they
# could create superusers, which would mean they would essentially have
# the permission to change users. To avoid the problem entirely, we
# disallow users from adding users if they don't have change
# permission.
if not self.has_change_permission(request):
if self.has_add_permission(request) and settings.DEBUG:
# Raise Http404 in debug mode so that the user gets a helpful
# error message.
raise Http404(
'Your user does not have the "Change user" permission. In '
"order to add users, Django requires that your user "
'account have both the "Add user" and "Change user" '
"permissions set."
)
raise PermissionDenied
if extra_context is None:
extra_context = {}
username_field = self.opts.get_field(self.model.USERNAME_FIELD)
defaults = {
"auto_populated_fields": (),
"username_help_text": username_field.help_text,
}
extra_context.update(defaults)
return super().add_view(request, form_url, extra_context)

@sensitive_post_parameters_m
def user_change_password(self, request, id, form_url=""):
user = self.get_object(request, unquote(id))
if not self.has_change_permission(request, user):
raise PermissionDenied
if user is None:
raise Http404(
_("%(name)s object with primary key %(key)r does not exist.")
% {
"name": self.opts.verbose_name,
"key": escape(id),
}
)
if request.method == "POST":
form = self.change_password_form(user, request.POST)
if form.is_valid():
form.save()
change_message = self.construct_change_message(request, form, None)
self.log_change(request, user, change_message)
msg = gettext("Password changed successfully.")
messages.success(request, msg)
update_session_auth_hash(request, form.user)
return HttpResponseRedirect(
reverse(
"%s:%s_%s_change"
% (
self.admin_site.name,
user._meta.app_label,
user._meta.model_name,
),
args=(user.pk,),
)
)
else:
form = self.change_password_form(user)

fieldsets = [(None, {"fields": list(form.base_fields)})]
admin_form = admin.helpers.AdminForm(form, fieldsets, {})

context = {
"title": _("Change password: %s") % escape(user.get_username()),
"adminForm": admin_form,
"form_url": form_url,
"form": form,
"is_popup": (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET),
"is_popup_var": IS_POPUP_VAR,
"add": True,
"change": False,
"has_delete_permission": False,
"has_change_permission": True,
"has_absolute_url": False,
"opts": self.opts,
"original": user,
"save_as": False,
"show_save": True,
**self.admin_site.each_context(request),
}

request.current_app = self.admin_site.name

return TemplateResponse(
request,
self.change_user_password_template
or "admin/auth/user/change_password.html",
context,
)

def response_add(self, request, obj, post_url_continue=None):
"""
Determine the HttpResponse for the add_view stage. It mostly defers to
its superclass implementation but is customized because the User model
has a slightly different workflow.
"""
# We should allow further modification of the user just added i.e. the
# 'Save' button should behave like the 'Save and continue editing'
# button except in two scenarios:
# * The user has pressed the 'Save and add another' button
# * We are adding a user in a popup
if "_addanother" not in request.POST and IS_POPUP_VAR not in request.POST:
request.POST = request.POST.copy()
request.POST["_continue"] = 1
return super().response_add(request, obj, post_url_continue)
class UserAdmin(MinUserAdmin):
pass
62 changes: 0 additions & 62 deletions backend/devpro/base/manager.py

This file was deleted.

6 changes: 2 additions & 4 deletions backend/devpro/base/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Generated by Django 5.0.7 on 2024-07-11 01:13

import devpro.base.manager
import django.utils.timezone
from django.db import migrations, models
from django_min_custom_user.manager import MinUserManager


class Migration(migrations.Migration):
Expand Down Expand Up @@ -68,7 +66,7 @@ class Migration(migrations.Migration):
'abstract': False,
},
managers=[
('objects', devpro.base.manager.UserManager()),
('objects', MinUserManager()),
],
),
]
67 changes: 2 additions & 65 deletions backend/devpro/base/models.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,5 @@
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.core.mail import send_mail
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django_min_custom_user.models import MinAbstractUser

from devpro.base.manager import UserManager


class AbstractUser(AbstractBaseUser, PermissionsMixin):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
Email and password are required. Other fields are optional.
"""

first_name = models.CharField(_("first name"), max_length=150, blank=True)
email = models.EmailField(_("email address"), unique=True)
is_staff = models.BooleanField(
_("staff status"),
default=False,
help_text=_("Designates whether the user can log into this admin site."),
)
is_active = models.BooleanField(
_("active"),
default=True,
help_text=_(
"Designates whether this user should be treated as active. "
"Unselect this instead of deleting accounts."
),
)
date_joined = models.DateTimeField(_("date joined"), default=timezone.now)

objects = UserManager()

EMAIL_FIELD = "email"
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []

class Meta:
verbose_name = _("user")
verbose_name_plural = _("users")
abstract = True

def clean(self):
super().clean()
self.email = self.__class__.objects.normalize_email(self.email)

def get_full_name(self):
"""
Return the first_name plus the last_name, with a space in between.
"""
full_name = f"{self.first_name}"
return full_name.strip()

def get_short_name(self):
"""Return the short name for the user."""
return self.first_name

def email_user(self, subject, message, from_email=None, **kwargs):
"""Send an email to this user."""
send_mail(subject, message, from_email, [self.email], **kwargs)


class User(AbstractUser):
class User(MinAbstractUser):
pass
5 changes: 2 additions & 3 deletions backend/devpro/base/tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import pytest
from django.contrib.auth import get_user_model

from devpro.base.manager import UserManager
from django_min_custom_user.manager import MinUserManager


def test_user_has_customized_manager_instance():
User = get_user_model()
assert isinstance(User.objects, UserManager)
assert isinstance(User.objects, MinUserManager)


@pytest.mark.django_db
Expand Down
Loading

0 comments on commit 919c60d

Please sign in to comment.