A Python web application containing many reusable modules.
class AuthWithUsernameOrEmailBackend(ModelBackend):
"""
Override method authenticate, make it possible to login with Email.
"""
def authenticate(self, request, username=None, password=None, **kwargs):
if username is None:
username = kwargs.get(User.USERNAME_FIELD)
if username is None or password is None:
return
try:
try:
user = User._default_manager.get_by_natural_key(username)
except User.DoesNotExist:
user = User._default_manager.get(email=username)
except User.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a nonexistent user (#20760).
User().set_password(password)
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user
def permission_required(perm, login_url=None, raise_exception=False, exception=PermissionDenied):
"""
Override Django's built-in permission_required decorator.
Act like permission_required but let you set a optional `exception` argument.
You can set it to Http404 instead of the default PermissionDenied.
"""
def check_perms(user):
if isinstance(perm, str):
perms = (perm,)
else:
perms = perm
# First check if the user has the permission (even anon users)
if user.has_perms(perms):
return True
# In case the 403 handler should be called raise the exception
if raise_exception:
raise exception
# As the last resort, show the login form
return False
return user_passes_test(check_perms, login_url=login_url)
There are other reusable source code like flushmigrations
, dumpdatautf8
Django management commands.
git clone this repo
git clone https://github.com/thejimmylin/ctdb /Users/jimmy_lin/repos/ctdb
build venv & install packages with pip
python3 -m venv /Users/jimmy_lin/repos/ctdb/.venv
source /Users/jimmy_lin/repos/ctdb/.venv
pip install -r /Users/jimmy_lin/repos/ctdb/requirements/dev.txt
Make DB migrations
python3 /Users/jimmy_lin/repos/ctdb/manage.py makemigrations
python3 /Users/jimmy_lin/repos/ctdb/manage.py migrate
Jimmy Lin [email protected]
Distributed under the MIT license. See LICENSE
for more information.
https://github.com/thejimmylin/
- Fork it (https://github.com/thejimmylin/ctdb/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request