question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PasswordResetView proposal to move the User.is_active check from database to Python

See original GitHub issue

Something like this:

class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView):
    def get_users(self, email):
        if self._users is None:
            active_users = User._default_manager.filter(
                email__iexact=email,
                # is_active=True, <--- v0.6 code
            )
            # self._users = [u for u in active_users if u.has_usable_password()] <--- v0.6 code
            self._users = [u for u in active_users if u.is_active and u.has_usable_password()]
        return self._users

Here are the reasons:

  1. If is_active is not True, then this opportunity can be used to notify the user that they’ve already registered an account with that email address, and [offer to] resend the activation email.
_users = User._default_manager.filter(email=email)
self._users = []
for u in _users:
    if u.has_usable_password():
        if u.is_active:
            self._users.append(u)
        else:
            # TODO resend activation email
  1. I think with the v0.6 code, there will be some conflict if is_active is implemented in a model like this:
class User(AbstractBaseUser):
    score = models.SmallIntegerField(default=0)
    status = models.SmallIntegerField(default=0)
    email = models.CIEmailField(unique=True)
    name = models.CITextField()
    ip = models.ArrayField(models.GenericIPAddressField(unpack_ipv4=True))

    objects = UserManager()

    USERNAME_FIELD = 'email'

    @property
    def is_active(self):
        # FIXME can't do this, djoser uses .filter(is_active=True)
        return self.status > 0

    @is_active.setter
    def is_active(self, value):
        if value is True:
            self.status = abs(self.status or 1)
        elif value is False:
            self.status = -abs(self.status or 0)
        else:
            self.status = value
  1. Any concerns about performance/inefficiency of retrieving unnecessary rows from the database should be nominal in real-world usage because there wouldn’t be very many unactive accounts tied to a single email address in most cases. And again, if the activation email resend is implemented, then the retrieval of these rows wouldn’t be considered wasted.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pszpetkowskicommented, Aug 18, 2017

Same here, in case of any doubts do not hesitate to start a new issue and again - thank you for reporting and working on the issue.

0reactions
davidtgqcommented, Aug 18, 2017

Yes, this can be closed. Thank you for the great feedback, it’s been a pleasure 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Settings - Django documentation
Core Settings¶. Here's a list of settings available in Django core and their default values. Settings provided by contrib apps are listed below, ......
Read more >
Can someone suggest how to implement custom Django ...
What is the official way to customize password reset view. So that user can search account by username or email for sending password...
Read more >
Using Flask-Login for User Management with Flask
Flask-login is a Flask extension that enables user authentication. ... has an is_active() method that returns True if the user's account is active...
Read more >
Quickstart: Use Python to query a database - Microsoft Learn
In this quickstart, you use Python to connect to Azure SQL Database, Azure SQL Managed Instance, or Synapse SQL database and use T-SQL ......
Read more >
Using the Python Connector - Snowflake Documentation
conn = snowflake.connector.connect( user=USER, password=PASSWORD, account=ACCOUNT, warehouse=WAREHOUSE, database=DATABASE, schema=SCHEMA ).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found