[extensions-web/wip/ne0sight] auth: do not explicitly use django.contrib.auth.User model
- From: Yuri Konotopov <ykonotopov src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web/wip/ne0sight] auth: do not explicitly use django.contrib.auth.User model
- Date: Sun, 24 Nov 2019 11:25:23 +0000 (UTC)
commit 0e42d96b076632668764cba56c3ebb26393e019c
Author: Yuri Konotopov <ykonotopov gnome org>
Date: Sun Nov 24 15:25:20 2019 +0400
auth: do not explicitly use django.contrib.auth.User model
We want to migrate to custom user model soon.
bin/sweettooth-clean-users | 4 ++--
sweettooth/auth/views.py | 7 +++----
sweettooth/errorreports/models.py | 4 ++--
sweettooth/errorreports/tests.py | 1 -
.../extensions/management/commands/populate_extensions.py | 10 +++++-----
sweettooth/extensions/models.py | 4 ++--
sweettooth/ratings/management/commands/populate_ratings.py | 12 ++++++------
sweettooth/review/models.py | 9 +++++----
sweettooth/testutils.py | 4 ++--
9 files changed, 27 insertions(+), 28 deletions(-)
---
diff --git a/bin/sweettooth-clean-users b/bin/sweettooth-clean-users
index adeb0c0..9349011 100755
--- a/bin/sweettooth-clean-users
+++ b/bin/sweettooth-clean-users
@@ -15,9 +15,9 @@ sys.path.extend([os.path.join(_path, '..'),
django.setup()
from django.db.models import Count
-from django.contrib.auth.models import User
+from django.contrib.auth import get_user_model
-User.objects \
+get_user_model().objects \
.filter(is_active=False) \
.filter(date_joined__lte=datetime.date.today() - datetime.timedelta(days = 5)) \
.annotate(extensions_count=Count('extension')).filter(extensions_count=0) \
diff --git a/sweettooth/auth/views.py b/sweettooth/auth/views.py
index 6473886..9a30d3d 100644
--- a/sweettooth/auth/views.py
+++ b/sweettooth/auth/views.py
@@ -1,5 +1,4 @@
-
-from django.contrib.auth import models
+from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404, redirect, render
@@ -11,7 +10,7 @@ from sweettooth.extensions.models import Extension, ExtensionVersion
from sweettooth.decorators import ajax_view
def profile(request, user):
- userobj = get_object_or_404(models.User, username=user)
+ userobj = get_object_or_404(get_user_model(), username=user)
is_editable = (request.user == userobj) or request.user.has_perm('review.can-review-extensions')
@@ -41,7 +40,7 @@ def ajax_change_display_name(request, pk):
if request.POST['id'] != 'new_display_name':
return HttpResponseForbidden()
- userobj = get_object_or_404(models.User, pk=pk)
+ userobj = get_object_or_404(get_user_model(), pk=pk)
is_editable = (request.user == userobj) or request.user.has_perm('review.can-review-extensions')
if not is_editable:
diff --git a/sweettooth/errorreports/models.py b/sweettooth/errorreports/models.py
index 8f7e45a..01712cf 100644
--- a/sweettooth/errorreports/models.py
+++ b/sweettooth/errorreports/models.py
@@ -1,12 +1,12 @@
-from django.contrib import auth
+from django.conf import settings
from django.db import models
from django.dispatch import Signal
from sweettooth.extensions.models import Extension
class ErrorReport(models.Model):
comment = models.TextField(blank=True)
- user = models.ForeignKey(auth.models.User, on_delete=models.CASCADE, related_name="+")
+ user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
extension = models.ForeignKey(Extension, null=True, on_delete=models.CASCADE)
error_reported = Signal(providing_args=["request", "version", "report"])
diff --git a/sweettooth/errorreports/tests.py b/sweettooth/errorreports/tests.py
index 62966c8..1e82895 100644
--- a/sweettooth/errorreports/tests.py
+++ b/sweettooth/errorreports/tests.py
@@ -1,6 +1,5 @@
from django.core import mail
-from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
diff --git a/sweettooth/extensions/management/commands/populate_extensions.py
b/sweettooth/extensions/management/commands/populate_extensions.py
index a6a297a..ea62647 100644
--- a/sweettooth/extensions/management/commands/populate_extensions.py
+++ b/sweettooth/extensions/management/commands/populate_extensions.py
@@ -1,13 +1,12 @@
import uuid
import random
-from django.contrib.auth.models import User
+from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ObjectDoesNotExist
from sweettooth.extensions import models
from django.contrib.sites.models import Site
-
class Command(BaseCommand):
help = 'Populates the database with randomly generated extensions.'
@@ -41,20 +40,21 @@ class Command(BaseCommand):
'url': '%s' % current_site.domain
}
+ UserModel = get_user_model()
if not user:
random_name = 'randomuser%d' % random.randint(1, 9999)
try:
- user = models.User.objects.get(username=random_name)
+ user = UserModel.objects.get(username=random_name)
except ObjectDoesNotExist:
- user = User.objects.create_user(
+ user = UserModel.objects.create_user(
username=random_name,
email='%s@%s' % (random_name, current_site.domain),
password='password'
)
else:
try:
- user = models.User.objects.get(username=user)
+ user = UserModel.objects.get(username=user)
except ObjectDoesNotExist:
raise CommandError('The specified username (%s) does not exist.' % user)
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 67e283f..ca09fb9 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -3,7 +3,7 @@ import json
from zipfile import ZipFile, BadZipfile
-from django.contrib.auth.models import User
+from django.conf import settings
from django.db import models
from django.dispatch import Signal
from django.urls import reverse
@@ -93,7 +93,7 @@ class Extension(models.Model):
name = models.CharField(max_length=200)
uuid = models.CharField(max_length=200, unique=True, db_index=True)
slug = autoslug.AutoSlugField(populate_from="name")
- creator = models.ForeignKey(User, db_index=True, on_delete=models.PROTECT)
+ creator = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, on_delete=models.PROTECT)
description = models.TextField(blank=True)
url = models.URLField(blank=True)
created = models.DateTimeField(auto_now_add=True)
diff --git a/sweettooth/ratings/management/commands/populate_ratings.py
b/sweettooth/ratings/management/commands/populate_ratings.py
index 41e4577..aa3cd6d 100644
--- a/sweettooth/ratings/management/commands/populate_ratings.py
+++ b/sweettooth/ratings/management/commands/populate_ratings.py
@@ -1,7 +1,7 @@
import random
import datetime
-from django.contrib.auth.models import User
+from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType
@@ -11,7 +11,6 @@ from django.contrib.sites.models import Site
from sweettooth.extensions import models
from sweettooth.ratings.models import RatingComment
-
class Command(BaseCommand):
help = 'Populates all the Extensions with a number of randomly generated ratings.'
@@ -43,19 +42,20 @@ class Command(BaseCommand):
random_name = lorem_text_list[random.randint(0, len(lorem_text_list)-1)]
current_site = Site.objects.get_current()
+ UserModel = get_user_model()
for extension in models.Extension.objects.all():
if not user:
try:
- user = models.User.objects.get(username=random_name)
- except ObjectDoesNotExist:
- user = User.objects.create_user(
+ user = UserModel.objects.get(username=random_name)
+ except UserModel.DoesNotExists:
+ user = UserModel.objects.create_user(
username=random_name,
email='%s@%s' % (random_name, current_site.domain),
password='password'
)
else:
try:
- user = models.User.objects.get(username=user)
+ user = UserModel.objects.get(username=user)
except ObjectDoesNotExist:
raise CommandError('The specified username (%s) does not exist.' % user)
diff --git a/sweettooth/review/models.py b/sweettooth/review/models.py
index 171dc80..2b8555f 100644
--- a/sweettooth/review/models.py
+++ b/sweettooth/review/models.py
@@ -1,5 +1,6 @@
-
-from django.contrib.auth.models import User, Permission, Group
+from django.conf import settings
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Permission, Group
from django.db import models
from django.db.models import Q
@@ -11,10 +12,10 @@ def get_all_reviewers():
# Dark magic to get all the users with a specific permission
# Thanks to <schinckel> in #django
groups = Group.objects.filter(permissions=perm)
- return User.objects.filter(Q(is_superuser=True)|Q(user_permissions=perm)|Q(groups__in=groups)).distinct()
+ return
get_user_model().objects.filter(Q(is_superuser=True)|Q(user_permissions=perm)|Q(groups__in=groups)).distinct()
class CodeReview(models.Model):
- reviewer = models.ForeignKey(User, on_delete=models.CASCADE)
+ reviewer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
comments = models.TextField(blank=True)
version = models.ForeignKey(ExtensionVersion, on_delete=models.CASCADE, related_name="reviews")
diff --git a/sweettooth/testutils.py b/sweettooth/testutils.py
index af298ff..0e81382 100644
--- a/sweettooth/testutils.py
+++ b/sweettooth/testutils.py
@@ -1,5 +1,5 @@
-from django.contrib.auth.models import User
+from django.contrib.auth import get_user_model
class BasicUserTestCase(object):
def setUp(self):
@@ -7,6 +7,6 @@ class BasicUserTestCase(object):
self.username = 'TestUser1'
self.email = 'non-existant non-existant tld'
self.password = 'a random password'
- self.user = User.objects.create_user(self.username, self.email, self.password)
+ self.user = get_user_model().objects.create_user(self.username, self.email, self.password)
self.client.login(username=self.username, password=self.password)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]