Skip to content

Commit

Permalink
Fixup: format code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
meschac38700 committed May 18, 2021
1 parent 6df19ef commit b19082f
Show file tree
Hide file tree
Showing 170 changed files with 14,298 additions and 11,286 deletions.
2 changes: 1 addition & 1 deletion pod/authentication/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default_app_config = 'pod.authentication.apps.AuthConfig'
default_app_config = "pod.authentication.apps.AuthConfig"
120 changes: 61 additions & 59 deletions pod/authentication/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,127 +16,130 @@
# Define an inline admin descriptor for Owner model
# which acts a bit like a singleton

USE_ESTABLISHMENT_FIELD = getattr(
settings,
'USE_ESTABLISHMENT_FIELD',
False)
USE_ESTABLISHMENT_FIELD = getattr(settings, "USE_ESTABLISHMENT_FIELD", False)


class GroupSiteInline(admin.StackedInline):
model = GroupSite
form = GroupSiteAdminForm
can_delete = False
verbose_name_plural = 'groupssite'
verbose_name_plural = "groupssite"

def get_fields(self, request, obj=None):
if not request.user.is_superuser:
exclude = ()
exclude += ('sites',)
exclude += ("sites",)
self.exclude = exclude
return list(super(GroupSiteInline, self).get_fields(request, obj))

class Media:
css = {
"all": (
'bootstrap-4/css/bootstrap.min.css',
'bootstrap-4/css/bootstrap-grid.css',
'css/pod.css'
"bootstrap-4/css/bootstrap.min.css",
"bootstrap-4/css/bootstrap-grid.css",
"css/pod.css",
)
}
js = (
'podfile/js/filewidget.js',
'js/main.js',
'feather-icons/feather.min.js',
'bootstrap-4/js/bootstrap.min.js')
"podfile/js/filewidget.js",
"js/main.js",
"feather-icons/feather.min.js",
"bootstrap-4/js/bootstrap.min.js",
)


class OwnerInline(admin.StackedInline):
model = Owner
form = OwnerAdminForm
can_delete = False
verbose_name_plural = 'owners'
readonly_fields = ('hashkey',)
verbose_name_plural = "owners"
readonly_fields = ("hashkey",)

def get_fields(self, request, obj=None):
fields = list(super(OwnerInline, self).get_fields(request, obj))
exclude_set = set()
# obj will be None on the add page, and something on change pages
if not obj:
exclude_set.add('hashkey')
exclude_set.add('auth_type')
exclude_set.add('affiliation')
exclude_set.add('commentaire')
exclude_set.add("hashkey")
exclude_set.add("auth_type")
exclude_set.add("affiliation")
exclude_set.add("commentaire")
if not request.user.is_superuser:
exclude_set.add('sites')
exclude_set.add("sites")
return [f for f in fields if f not in exclude_set]

class Media:
css = {
"all": (
'bootstrap-4/css/bootstrap.min.css',
'bootstrap-4/css/bootstrap-grid.css',
'css/pod.css'
"bootstrap-4/css/bootstrap.min.css",
"bootstrap-4/css/bootstrap-grid.css",
"css/pod.css",
)
}
js = (
'podfile/js/filewidget.js',
'js/main.js',
'feather-icons/feather.min.js',
'bootstrap-4/js/bootstrap.min.js')
"podfile/js/filewidget.js",
"js/main.js",
"feather-icons/feather.min.js",
"bootstrap-4/js/bootstrap.min.js",
)


class UserAdmin(BaseUserAdmin):

def clickable_email(self, obj):
email = obj.email
return format_html('<a href="mailto:{}">{}</a>', email, email)

clickable_email.allow_tags = True
clickable_email.short_description = _('Email')
clickable_email.short_description = _("Email")
list_display = (
'username',
'last_name',
'first_name',
'clickable_email',
'date_joined',
'last_login',
'is_active',
'is_staff',
'is_superuser',
'owner_hashkey'
"username",
"last_name",
"first_name",
"clickable_email",
"date_joined",
"last_login",
"is_active",
"is_staff",
"is_superuser",
"owner_hashkey",
)

list_filter = (
'is_staff',
'is_superuser',
'is_active',
('groups', admin.RelatedOnlyFieldListFilter)
"is_staff",
"is_superuser",
"is_active",
("groups", admin.RelatedOnlyFieldListFilter),
)
if USE_ESTABLISHMENT_FIELD:
list_display = list_display + ('owner_establishment',)
list_display = list_display + ("owner_establishment",)

def owner_hashkey(self, obj):
return "%s" % Owner.objects.get(user=obj).hashkey

def formfield_for_manytomany(self, db_field, request, **kwargs):
if (db_field.name) == "groups":
kwargs["queryset"] = Group.objects.filter(
groupsite__sites=Site.objects.get_current())
kwargs['widget'] = widgets.FilteredSelectMultiple(
db_field.verbose_name,
False)
groupsite__sites=Site.objects.get_current()
)
kwargs["widget"] = widgets.FilteredSelectMultiple(
db_field.verbose_name, False
)
return super().formfield_for_foreignkey(db_field, request, **kwargs)

def owner_establishment(self, obj):
return "%s" % Owner.objects.get(user=obj).establishment
owner_establishment.short_description = _('Establishment')

ordering = ('-is_superuser', 'username', )
owner_establishment.short_description = _("Establishment")

ordering = (
"-is_superuser",
"username",
)

def get_queryset(self, request):
qs = super().get_queryset(request)
if not request.user.is_superuser:
qs = qs.filter(owner__sites=get_current_site(
request))
qs = qs.filter(owner__sites=get_current_site(request))
return qs

def save_model(self, request, obj, form, change):
Expand All @@ -158,13 +161,12 @@ class GroupAdmin(admin.ModelAdmin):
# Use our custom form.
form = GroupAdminForm
# Filter permissions horizontal as well.
filter_horizontal = ['permissions']
filter_horizontal = ["permissions"]

def get_queryset(self, request):
qs = super().get_queryset(request)
if not request.user.is_superuser:
qs = qs.filter(groupsite__sites=get_current_site(
request))
qs = qs.filter(groupsite__sites=get_current_site(request))
return qs

def save_model(self, request, obj, form, change):
Expand All @@ -182,11 +184,11 @@ def get_inline_instances(self, request, obj=None):


class AccessGroupAdmin(admin.ModelAdmin):
search_fields = ['id', 'code_name', 'display_name']
search_fields = ["id", "code_name", "display_name"]
list_display = (
'id',
'code_name',
'display_name',
"id",
"code_name",
"display_name",
)


Expand Down
4 changes: 3 additions & 1 deletion pod/authentication/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

def create_groupsite_if_not_exists(g):
from pod.authentication.models import GroupSite

try:
GroupSite.objects.get(group=g)
except ObjectDoesNotExist:
Expand All @@ -16,6 +17,7 @@ def set_default_site(sender, **kwargs):
from django.contrib.sites.models import Site
from django.contrib.auth.models import Group
from pod.authentication.models import GroupSite

for g in Group.objects.all():
create_groupsite_if_not_exists(g)
for gs in GroupSite.objects.all():
Expand All @@ -29,7 +31,7 @@ def set_default_site(sender, **kwargs):


class AuthConfig(AppConfig):
name = 'pod.authentication'
name = "pod.authentication"

def ready(self):
post_migrate.connect(set_default_site, sender=self)
17 changes: 8 additions & 9 deletions pod/authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
class ShibbBackend(ShibbolethRemoteUserBackend):
@staticmethod
def update_user_params(user, params):
super(ShibbBackend,
ShibbBackend).update_user_params(user, params)
super(ShibbBackend, ShibbBackend).update_user_params(user, params)
user.owner.auth_type = "Shibboleth"
if get_current_site(None) not in user.owner.sites.all():
user.owner.sites.add(get_current_site(None))
Expand All @@ -18,25 +17,25 @@ def update_user_params(user, params):
# Fetch profile claims to fill User model
# ref: https://mozilla-django-oidc.readthedocs.io/en/stable/installation.html\
# #changing-how-django-users-are-created
OIDC_CLAIM_GIVEN_NAME = getattr(
settings, 'OIDC_CLAIM_GIVEN_NAME', "given_name")
OIDC_CLAIM_GIVEN_NAME = getattr(settings, "OIDC_CLAIM_GIVEN_NAME", "given_name")
OIDC_CLAIM_FAMILY_NAME = getattr(
settings, 'OIDC_CLAIM_FAMILY_NAME', "family_name")
settings, "OIDC_CLAIM_FAMILY_NAME", "family_name"
)


class OIDCBackend(OIDCAuthenticationBackend):
def create_user(self, claims):
user = super(OIDCBackend, self).create_user(claims)

user.first_name = claims.get(OIDC_CLAIM_GIVEN_NAME, '')
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, '')
user.first_name = claims.get(OIDC_CLAIM_GIVEN_NAME, "")
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, "")
user.save()

return user

def update_user(self, user, claims):
user.first_name = claims.get(OIDC_CLAIM_GIVEN_NAME, '')
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, '')
user.first_name = claims.get(OIDC_CLAIM_GIVEN_NAME, "")
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, "")
user.save()

user.owner.auth_type = "OIDC"
Expand Down
34 changes: 16 additions & 18 deletions pod/authentication/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,36 @@
from django.contrib.sites.models import Site

FILEPICKER = False
if getattr(settings, 'USE_PODFILE', False):
if getattr(settings, "USE_PODFILE", False):
from pod.podfile.widgets import CustomFileWidget

FILEPICKER = True


class OwnerAdminForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(OwnerAdminForm, self).__init__(*args, **kwargs)
if FILEPICKER:
self.fields['userpicture'].widget = CustomFileWidget(
type="image")
self.fields["userpicture"].widget = CustomFileWidget(type="image")

class Meta(object):
model = Owner
fields = '__all__'
fields = "__all__"


class GroupSiteAdminForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(GroupSiteAdminForm, self).__init__(*args, **kwargs)

class Meta(object):
model = GroupSite
fields = '__all__'
fields = "__all__"


class FrontOwnerForm(OwnerAdminForm):

class Meta(object):
model = Owner
fields = ('userpicture',)
fields = ("userpicture",)


User = get_user_model()
Expand All @@ -54,11 +51,11 @@ class Meta:

# Add the users field.
users = forms.ModelMultipleChoiceField(
queryset=User.objects.all(),
required=False,
# Use the pretty 'filter_horizontal widget'.
widget=FilteredSelectMultiple(_('Users'), False),
label=_('Users')
queryset=User.objects.all(),
required=False,
# Use the pretty 'filter_horizontal widget'.
widget=FilteredSelectMultiple(_("Users"), False),
label=_("Users"),
)

def __init__(self, *args, **kwargs):
Expand All @@ -67,13 +64,14 @@ def __init__(self, *args, **kwargs):
# If it is an existing group (saved objects have a pk).
if self.instance.pk:
# Populate the users field with the current Group users.
self.fields['users'].initial = self.instance.user_set.all()
self.fields['users'].queryset = self.fields['users'].queryset.filter(
owner__sites=Site.objects.get_current())
self.fields["users"].initial = self.instance.user_set.all()
self.fields["users"].queryset = self.fields["users"].queryset.filter(
owner__sites=Site.objects.get_current()
)

def save_m2m(self):
# Add the users to the Group.
self.instance.user_set.set(self.cleaned_data['users'])
self.instance.user_set.set(self.cleaned_data["users"])

def save(self, *args, **kwargs):
# Default save
Expand Down
Loading

0 comments on commit b19082f

Please sign in to comment.