Skip to content

Conversation

@SourceryAI
Copy link

Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨

Here's your pull request refactoring your most popular Python repo.

If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch https://github.com/sourcery-ai-bot/django-windowsauth main
git merge --ff-only FETCH_HEAD
git reset HEAD^

else:
# for all domains
return True
return user.ldap.domain == domain if domain else True
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function domain_required.check_domain refactored with the following changes:

This removes the following comments ( why? ):

# for all domains
# for specific domain

Comment on lines -51 to +62
if user.is_authenticated and LDAPUser.objects.filter(user=user).exists():
try:
if WAUTH_USE_CACHE:
user.ldap.sync()
else:
# check via database query
if not timedelta or not user.ldap.last_sync or user.ldap.last_sync < timezone.now() - timedelta:
user.ldap.sync()

return True
except Exception as e:
if raise_exception:
raise e
else:
return False
else:
if (
not user.is_authenticated
or not LDAPUser.objects.filter(user=user).exists()
):
return allow_non_ldap
try:
if WAUTH_USE_CACHE:
user.ldap.sync()
elif not timedelta or not user.ldap.last_sync or user.ldap.last_sync < timezone.now() - timedelta:
user.ldap.sync()

return True
except Exception as e:
if raise_exception:
raise e
else:
return False
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ldap_sync_required.check_sync refactored with the following changes:

This removes the following comments ( why? ):

# check via database query

def __init__(self, domain: str, settings: Optional[LDAPSettings] = None):
self.domain = domain
self.settings = settings if settings else LDAPSettings.for_domain(domain)
self.settings = settings or LDAPSettings.for_domain(domain)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LDAPManager.__init__ refactored with the following changes:

Comment on lines -43 to +58
else:
# check via database query
if not ldap_user.last_sync or ldap_user.last_sync < timezone.now() - timezone.timedelta(seconds=timeout):
ldap_user.sync()
elif not ldap_user.last_sync or ldap_user.last_sync < timezone.now() - timezone.timedelta(seconds=timeout):
ldap_user.sync()
except LDAPUser.DoesNotExist:
# user is getting created the first time
pass
except Exception as e:
logger.exception(f"Failed to synchronize user {request.user} against LDAP")
# return error response
if WAUTH_REQUIRE_RESYNC:
if isinstance(WAUTH_ERROR_RESPONSE, int):
return HttpResponse(f"Authorization Failed.", status=WAUTH_ERROR_RESPONSE)
elif callable(WAUTH_ERROR_RESPONSE):
if isinstance(WAUTH_ERROR_RESPONSE, int):
if WAUTH_REQUIRE_RESYNC:
return HttpResponse("Authorization Failed.", status=WAUTH_ERROR_RESPONSE)
elif callable(WAUTH_ERROR_RESPONSE):
if WAUTH_REQUIRE_RESYNC:
return WAUTH_ERROR_RESPONSE(request, e)
else:
raise e
response = self.get_response(request)
return response
elif WAUTH_REQUIRE_RESYNC:
raise e
return self.get_response(request)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UserSyncMiddleware.__call__ refactored with the following changes:

This removes the following comments ( why? ):

# return error response
# check via database query

Comment on lines +48 to +52
elif "\\" in username:
domain, sam_account_name = username.split("\\", 2)

else:
raise ValueError("Username must be in DOMAIN\\username format.")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LDAPUserManager.create_user refactored with the following changes:

Comment on lines -65 to +67
return format_bytes(obj.bytes_received) + " / " + format_bytes(obj.bytes_transmitted)
return f'{format_bytes(obj.bytes_received)} / ' + format_bytes(
obj.bytes_transmitted
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LDAPUsageAdmin.bytes refactored with the following changes:

Comment on lines -68 to +70
return str(obj.messages_received) + " / " + str(obj.messages_transmitted)
return f'{str(obj.messages_received)} / {str(obj.messages_transmitted)}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LDAPUsageAdmin.messages refactored with the following changes:

Comment on lines -71 to +75
return str(obj.open_sockets) + " / " + str(obj.closed_sockets) + " / " + str(obj.wrapped_sockets)
return f'{str(obj.open_sockets)} / {str(obj.closed_sockets)} / ' + str(
obj.wrapped_sockets
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LDAPUsageAdmin.sockets refactored with the following changes:

def collect_metrics(unbind: bool = True):
if unbind:
logger.info(f"Unbinding all LDAP Connections for collecting metrics")
logger.info("Unbinding all LDAP Connections for collecting metrics")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function collect_metrics refactored with the following changes:

Comment on lines -69 to +73
# predefined tasks
if command in PREDEFINED_TASKS:
create_task = PREDEFINED_TASKS[command]
create_task(command=command, name=name, desc=desc, identity=identity, folder=folder,
interval=interval, random=random, timeout=timeout, priority=priority)
else:
if command not in PREDEFINED_TASKS:
raise CommandError(f"Predefined task for \"{command}\" does not exist.")
create_task = PREDEFINED_TASKS[command]
create_task(command=command, name=name, desc=desc, identity=identity, folder=folder,
interval=interval, random=random, timeout=timeout, priority=priority)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Command.handle refactored with the following changes:

This removes the following comments ( why? ):

# predefined tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant