Skip to content

Commit

Permalink
Merge pull request #175 from valberg/add_pre_commit
Browse files Browse the repository at this point in the history
Add .pre-commit-config.yaml
  • Loading branch information
valberg authored Mar 30, 2022
2 parents b6ae99e + cff5b8b commit b8bac5b
Show file tree
Hide file tree
Showing 30 changed files with 863 additions and 635 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
with:
user: jazzband
password: ${{ secrets.JAZZBAND_RELEASE_KEY }}
repository_url: https://jazzband.co/projects/django-invitations/upload
repository_url: https://jazzband.co/projects/django-invitations/upload
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Test

on: [push, pull_request]

jobs:
build:
name: build (Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }})
Expand All @@ -20,12 +20,12 @@ jobs:
with:
python-version: ${{ matrix.python-version }}


- name: Get pip cache dir
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache
uses: actions/cache@v2
with:
Expand All @@ -34,17 +34,17 @@ jobs:
${{ matrix.python-version }}-v1-${{ hashFiles('**/setup.cfg') }}-${{ hashFiles('**/tox.ini') }}
restore-keys: |
${{ matrix.python-version }}-v1-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade tox tox-gh-actions
- name: Tox tests
run: tox -v
env:
DJANGO: ${{ matrix.django-version }}

- name: Upload coverage
uses: codecov/codecov-action@v1
with:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ git-push.bat
.python-version
.coverage
/.idea/

63 changes: 63 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
default_language_version:
python: python3
exclude: ^.*\b(migrations)\b.*$
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-ast
- id: check-merge-conflict
- id: check-case-conflict
- id: detect-private-key
- id: check-added-large-files
- id: check-json
- id: check-symlinks
- id: check-toml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- flake8-tidy-imports
args: [--max-line-length=88]
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.0.1
hooks:
- id: reorder-python-imports
args:
- --py37-plus
- --application-directories=.:src
exclude: migrations/
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
hooks:
- id: pyupgrade
args:
- --py37-plus
exclude: migrations/
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.4.0
hooks:
- id: django-upgrade
args:
- --target-version=3.2
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
- id: yesqa
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.2.1
hooks:
- id: add-trailing-comma
- repo: https://github.com/hadialqattan/pycln
rev: v1.2.5
hooks:
- id: pycln
2 changes: 0 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,3 @@ x.x (yyyy-mm-dd)
- Template path. [bee_keeper]

- Name changes. [bee_keeper]


2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
94 changes: 49 additions & 45 deletions invitations/adapters.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,78 @@
from django.conf import settings
from django.contrib import messages
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.core.mail import EmailMessage
from django.core.mail import EmailMultiAlternatives
from django.template import TemplateDoesNotExist
from django.template.loader import render_to_string
from django.utils.encoding import force_str

from .app_settings import app_settings
from .utils import import_attribute

try:
from django.utils.encoding import force_str as force_text
except ImportError:
try:
from django.utils.encoding import force_text
except ImportError:
from django.utils.encoding import force_unicode as force_text


# Code credits here to django-allauth
class BaseInvitationsAdapter(object):
class BaseInvitationsAdapter:
def stash_verified_email(self, request, email):
request.session['account_verified_email'] = email
request.session["account_verified_email"] = email

def unstash_verified_email(self, request):
ret = request.session.get('account_verified_email')
request.session['account_verified_email'] = None
ret = request.session.get("account_verified_email")
request.session["account_verified_email"] = None
return ret

def format_email_subject(self, subject, context):
prefix = app_settings.EMAIL_SUBJECT_PREFIX
if prefix is None:
site_name = context["site_name"]
prefix = "[{name}] ".format(name=site_name)
return prefix + force_text(subject)
prefix = f"[{site_name}] "
return prefix + force_str(subject)

def render_mail(self, template_prefix, email, context):
"""
Renders an e-mail to `email`. `template_prefix` identifies the
e-mail that is to be sent, e.g. "account/email/email_confirmation"
"""
subject = render_to_string('{0}_subject.txt'.format(template_prefix),
context)
subject = render_to_string(f"{template_prefix}_subject.txt", context)
# remove superfluous line breaks
subject = " ".join(subject.splitlines()).strip()
subject = self.format_email_subject(subject, context)

bodies = {}
for ext in ['html', 'txt']:
for ext in ["html", "txt"]:
try:
template_name = '{0}_message.{1}'.format(template_prefix, ext)
bodies[ext] = render_to_string(template_name,
context).strip()
template_name = f"{template_prefix}_message.{ext}"
bodies[ext] = render_to_string(template_name, context).strip()
except TemplateDoesNotExist:
if ext == 'txt' and not bodies:
if ext == "txt" and not bodies:
# We need at least one body
raise
if 'txt' in bodies:
msg = EmailMultiAlternatives(subject,
bodies['txt'],
settings.DEFAULT_FROM_EMAIL,
[email])
if 'html' in bodies:
msg.attach_alternative(bodies['html'], 'text/html')
if "txt" in bodies:
msg = EmailMultiAlternatives(
subject,
bodies["txt"],
settings.DEFAULT_FROM_EMAIL,
[email],
)
if "html" in bodies:
msg.attach_alternative(bodies["html"], "text/html")
else:
msg = EmailMessage(subject,
bodies['html'],
settings.DEFAULT_FROM_EMAIL,
[email])
msg.content_subtype = 'html' # Main content is now text/html
msg = EmailMessage(
subject,
bodies["html"],
settings.DEFAULT_FROM_EMAIL,
[email],
)
msg.content_subtype = "html" # Main content is now text/html
return msg

def send_mail(self, template_prefix, email, context):
msg = self.render_mail(template_prefix, email, context)
msg.send()

def is_open_for_signup(self, request):
if hasattr(request, 'session') and request.session.get(
'account_verified_email'):
if hasattr(request, "session") and request.session.get(
"account_verified_email",
):
return True
elif app_settings.INVITATION_ONLY is True:
# Site is ONLY open for invites
Expand All @@ -91,32 +88,39 @@ def clean_email(self, email):
"""
return email

def add_message(self, request, level, message_template,
message_context=None, extra_tags=''):
def add_message(
self,
request,
level,
message_template,
message_context=None,
extra_tags="",
):
"""
Wrapper of `django.contrib.messages.add_message`, that reads
the message text from a template.
"""
if 'django.contrib.messages' in settings.INSTALLED_APPS:
if "django.contrib.messages" in settings.INSTALLED_APPS:
try:
if message_context is None:
message_context = {}
message = render_to_string(message_template,
message_context).strip()
message = render_to_string(message_template, message_context).strip()
if message:
messages.add_message(request, level, message,
extra_tags=extra_tags)
messages.add_message(request, level, message, extra_tags=extra_tags)
except TemplateDoesNotExist:
pass


def get_invitations_adapter():
# Compatibility with legacy allauth only version.
LEGACY_ALLAUTH = hasattr(settings, 'ACCOUNT_ADAPTER') and \
settings.ACCOUNT_ADAPTER == 'invitations.models.InvitationsAdapter'
LEGACY_ALLAUTH = (
hasattr(settings, "ACCOUNT_ADAPTER")
and settings.ACCOUNT_ADAPTER == "invitations.models.InvitationsAdapter"
)
if LEGACY_ALLAUTH:
# defer to allauth
from allauth.account.adapter import get_adapter

return get_adapter()
else:
# load an adapter from elsewhere
Expand Down
17 changes: 9 additions & 8 deletions invitations/admin.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
from django.contrib import admin

from .utils import (get_invitation_admin_add_form,
get_invitation_admin_change_form, get_invitation_model)
from .utils import get_invitation_admin_add_form
from .utils import get_invitation_admin_change_form
from .utils import get_invitation_model

Invitation = get_invitation_model()
InvitationAdminAddForm = get_invitation_admin_add_form()
InvitationAdminChangeForm = get_invitation_admin_change_form()


class InvitationAdmin(admin.ModelAdmin):
list_display = ('email', 'sent', 'accepted')
list_display = ("email", "sent", "accepted")
raw_id_fields = ("inviter",)

def get_form(self, request, obj=None, **kwargs):
if obj:
kwargs['form'] = InvitationAdminChangeForm
kwargs["form"] = InvitationAdminChangeForm
else:
kwargs['form'] = InvitationAdminAddForm
kwargs['form'].user = request.user
kwargs['form'].request = request
return super(InvitationAdmin, self).get_form(request, obj, **kwargs)
kwargs["form"] = InvitationAdminAddForm
kwargs["form"].user = request.user
kwargs["form"].request = request
return super().get_form(request, obj, **kwargs)


admin.site.register(Invitation, InvitationAdmin)
Loading

0 comments on commit b8bac5b

Please sign in to comment.