Skip to content

Commit

Permalink
Merge pull request #257 from LenaWil/format/black+isort
Browse files Browse the repository at this point in the history
Format everything with Black

I hope this okay, but everything has been approved and I don’t want to wait more. 

Since this is build on the better formatting branch, it closes #256.
  • Loading branch information
LenaWil authored Jun 12, 2023
2 parents 61f27f7 + e393911 commit d16a050
Show file tree
Hide file tree
Showing 131 changed files with 3,838 additions and 1,880 deletions.
8 changes: 7 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ ignore =
D1
# Missing blank line after last docstring section
D413
# Makes it possible to split long (boolean) equations on multiple lines
W503
#Imports
I
extend-select =
W504
exclude =
.git,
__pycache__,
*/migrations,
venv
.venv
max-complexity = 10
max-line-length = 127

docstring-convention = google
import-order-style = pycharm
4 changes: 4 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
37079c3b2ee3fc4100c56b041d90faf5c5fbbe48
87ddf24356f15143780b469ad006b3e95273cd7b
5dad39e90d7f659c0f2e630ad9dfefa10cd45b99
437f1bc494b0afa4492411d5f44edf40a3d637d4
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ jobs:
uses: codecov/codecov-action@v3
- name: Lint with flake8
run: flake8
- uses: psf/black@stable
- run: isort --check --diff .
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-yaml
- id: check-json
- id: check-toml
- id: check-added-large-files
- id: check-ast
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3.8
26 changes: 13 additions & 13 deletions allauthproviders/quadrivium/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ class QuadriviumAccount(ProviderAccount):
def to_str(self):
# String that is displayed e.g. on the linked accounts page
data = self.account.extra_data
first_name = data.get('given_name')
last_name = data.get('family_name')
username = data.get('preferred_username')
first_name = data.get("given_name")
last_name = data.get("family_name")
username = data.get("preferred_username")
if not first_name or not last_name or not username:
return super().to_str()
return "{} {} ({})".format(first_name, last_name, username)


class QuadriviumProvider(AssociationProvider):
id = 'quadrivium'
name = 'ESMG Quadrivium'
id = "quadrivium"
name = "ESMG Quadrivium"
account_class = QuadriviumAccount

logo = static('images/allauthproviders/quadrivium.svg')
logo = static("images/allauthproviders/quadrivium.svg")

def get_scope(self, request):
return ['openid', 'email']
return ["openid", "email"]

def get_auth_params(self, request, action):
# Don't know what this does, but I'll leave it in case it does something
ret = super().get_auth_params(request, action)
if action == AuthAction.REAUTHENTICATE:
ret['prompt'] = 'select_account'
ret["prompt"] = "select_account"
return ret

def extract_uid(self, data):
return str(data['sub'])
return str(data["sub"])

def extract_common_fields(self, data):
return dict(
username=data.get('preferred_username', data.get('given_name')),
email=data.get('email'),
first_name=data.get('given_name'),
last_name=data.get('family_name')
username=data.get("preferred_username", data.get("given_name")),
email=data.get("email"),
first_name=data.get("given_name"),
last_name=data.get("family_name"),
)


Expand Down
16 changes: 11 additions & 5 deletions allauthproviders/quadrivium/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import requests
from allauth.socialaccount.providers.oauth2.views import OAuth2Adapter, OAuth2CallbackView, OAuth2LoginView
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)

from .provider import QuadriviumProvider


class QuadriviumOAuth2Adapter(OAuth2Adapter):
provider_id = QuadriviumProvider.id

access_token_url = 'https://keycloak2.esmgquadrivium.nl/auth/realms/q/protocol/openid-connect/token'
authorize_url = 'https://keycloak2.esmgquadrivium.nl/auth/realms/q/protocol/openid-connect/auth'
profile_url = 'https://keycloak2.esmgquadrivium.nl/auth/realms/q/protocol/openid-connect/userinfo'
access_token_url = "https://keycloak2.esmgquadrivium.nl/auth/realms/q/protocol/openid-connect/token"
authorize_url = (
"https://keycloak2.esmgquadrivium.nl/auth/realms/q/protocol/openid-connect/auth"
)
profile_url = "https://keycloak2.esmgquadrivium.nl/auth/realms/q/protocol/openid-connect/userinfo"

def complete_login(self, request, app, token, **kwargs):
auth = {'Authorization': 'Bearer ' + token.token}
auth = {"Authorization": "Bearer " + token.token}
resp = requests.get(self.profile_url, headers=auth)
resp.raise_for_status()
extra_data = resp.json()
Expand Down
47 changes: 29 additions & 18 deletions creditmanagement/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class AccountTypeListFilter(admin.SimpleListFilter):
"""Allows filtering on account type which is either user, association or special."""

title = 'account type' # (displayed in side bar)
parameter_name = 'type' # (used in URL query)
title = "account type" # (displayed in side bar)
parameter_name = "type" # (used in URL query)

# Queries can be overridden in a subclass
user_query = Q(user__isnull=False)
Expand All @@ -18,17 +18,17 @@ class AccountTypeListFilter(admin.SimpleListFilter):
def lookups(self, request, model_admin):
# First element is URL param, second element is display value
return (
('user', "User"),
('association', "Association"),
('special', "Special"),
("user", "User"),
("association", "Association"),
("special", "Special"),
)

def queryset(self, request, queryset):
if self.value() == 'user':
if self.value() == "user":
return queryset.filter(self.user_query)
if self.value() == 'association':
if self.value() == "association":
return queryset.filter(self.association_query)
if self.value() == 'special':
if self.value() == "special":
return queryset.filter(self.special_query)


Expand All @@ -42,28 +42,36 @@ class AccountAdmin(admin.ModelAdmin):
you could change the user or association linked to the account.
"""

ordering = ('special', 'association__name', 'user__first_name', 'user__last_name')
list_display = ('__str__', 'get_balance', 'negative_since')
ordering = ("special", "association__name", "user__first_name", "user__last_name")
list_display = ("__str__", "get_balance", "negative_since")
list_filter = (AccountTypeListFilter,)
search_fields = ('user__first_name', 'user__last_name', 'user__username', 'association__name', 'special')
search_fields = (
"user__first_name",
"user__last_name",
"user__username",
"association__name",
"special",
)

def has_change_permission(self, request, obj=None):
return False


class SourceTypeListFilter(AccountTypeListFilter):
"""Allows filtering on the source account type."""

title = "source account type"
parameter_name = 'source_type'
parameter_name = "source_type"
user_query = Q(source__user__isnull=False)
association_query = Q(source__association__isnull=False)
special_query = Q(source__special__isnull=False)


class TargetTypeListFilter(AccountTypeListFilter):
"""Allows filtering on the target account type."""

title = "target account type"
parameter_name = 'target_type'
parameter_name = "target_type"
user_query = Q(target__user__isnull=False)
association_query = Q(target__association__isnull=False)
special_query = Q(target__special__isnull=False)
Expand All @@ -73,13 +81,16 @@ class TargetTypeListFilter(AccountTypeListFilter):
class TransactionAdmin(admin.ModelAdmin):
"""The transaction admin enables viewing transactions and creating new transactions."""

ordering = ('-moment',)
list_display = ('moment', 'source', 'target', 'amount', 'description')
ordering = ("-moment",)
list_display = ("moment", "source", "target", "amount", "description")
list_filter = (SourceTypeListFilter, TargetTypeListFilter)

fields = ('source', 'target', 'amount', 'moment', 'description', 'created_by')
readonly_fields = ('moment', 'created_by') # Only applicable for the add transaction form (changing is not allowed)
autocomplete_fields = ('source', 'target')
fields = ("source", "target", "amount", "moment", "description", "created_by")
readonly_fields = (
"moment",
"created_by",
) # Only applicable for the add transaction form (changing is not allowed)
autocomplete_fields = ("source", "target")

def has_change_permission(self, request, obj=None):
return False
Expand Down
2 changes: 1 addition & 1 deletion creditmanagement/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class CreditManagementConfig(AppConfig):
name = 'creditmanagement'
name = "creditmanagement"

def ready(self):
# noinspection PyUnresolvedReferences
Expand Down
52 changes: 43 additions & 9 deletions creditmanagement/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

from django.utils.timezone import get_default_timezone

from creditmanagement.models import Transaction, Account
from creditmanagement.models import Account, Transaction


def write_transactions_csv(csv_file, transactions: Iterable[Transaction], account_self: Account):
def write_transactions_csv(
csv_file, transactions: Iterable[Transaction], account_self: Account
):
"""Writes a transactions CSV file to the given file object.
Args:
Expand All @@ -17,23 +19,55 @@ def write_transactions_csv(csv_file, transactions: Iterable[Transaction], accoun
opposite account is used for the (counterpart) name column.
"""
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['date', 'direction', 'account_type', 'name', 'email', 'amount', 'description', 'created_by'])
csv_writer.writerow(
[
"date",
"direction",
"account_type",
"name",
"email",
"amount",
"description",
"created_by",
]
)

for t in transactions:
# Determine direction and counterparty
if t.source == account_self:
direction = 'out'
direction = "out"
counterparty = t.target
elif t.target == account_self:
direction = 'in'
direction = "in"
counterparty = t.source
else:
raise ValueError("Transaction does not involve account_self")

# Set timezone to ours (Europe/Amsterdam) and get rid of microseconds
date = t.moment.astimezone(get_default_timezone()).replace(microsecond=0).isoformat()
account_type = 'user' if counterparty.user else 'association' if counterparty.association else 'special'
date = (
t.moment.astimezone(get_default_timezone())
.replace(microsecond=0)
.isoformat()
)
account_type = (
"user"
if counterparty.user
else "association"
if counterparty.association
else "special"
)
name = str(counterparty)
email = counterparty.user.email if account_type == 'user' else ''
email = counterparty.user.email if account_type == "user" else ""

csv_writer.writerow([date, direction, account_type, name, email, t.amount, t.description, str(t.created_by)])
csv_writer.writerow(
[
date,
direction,
account_type,
name,
email,
t.amount,
t.description,
str(t.created_by),
]
)
Loading

0 comments on commit d16a050

Please sign in to comment.