Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/utils.py
Show First 20 Lines • Show All 235 Lines • ▼ Show 20 Lines | def get_client_ip(request): | ||||
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR") | x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR") | ||||
if x_forwarded_for: | if x_forwarded_for: | ||||
ip = x_forwarded_for.split(",")[0] | ip = x_forwarded_for.split(",")[0] | ||||
else: | else: | ||||
ip = request.META.get("REMOTE_ADDR") | ip = request.META.get("REMOTE_ADDR") | ||||
return ip | return ip | ||||
browsers_supported_image_mimes = set( | |||||
[ | |||||
"image/gif", | |||||
"image/png", | |||||
"image/jpeg", | |||||
"image/bmp", | |||||
"image/webp", | |||||
"image/svg", | |||||
"image/svg+xml", | |||||
] | |||||
) | |||||
def context_processor(request): | def context_processor(request): | ||||
""" | """ | ||||
Django context processor used to inject variables | Django context processor used to inject variables | ||||
in all swh-web templates. | in all swh-web templates. | ||||
""" | """ | ||||
config = get_config() | config = get_config() | ||||
if ( | if ( | ||||
hasattr(request, "user") | hasattr(request, "user") | ||||
and request.user.is_authenticated | and request.user.is_authenticated | ||||
and not hasattr(request.user, "backend") | and not hasattr(request.user, "backend") | ||||
): | ): | ||||
# To avoid django.template.base.VariableDoesNotExist errors | # To avoid django.template.base.VariableDoesNotExist errors | ||||
# when rendering templates when standard Django user is logged in. | # when rendering templates when standard Django user is logged in. | ||||
request.user.backend = "django.contrib.auth.backends.ModelBackend" | request.user.backend = "django.contrib.auth.backends.ModelBackend" | ||||
return { | return { | ||||
"swh_object_icons": swh_object_icons, | "swh_object_icons": swh_object_icons, | ||||
"available_languages": None, | "available_languages": None, | ||||
"swh_client_config": config["client_config"], | "swh_client_config": config["client_config"], | ||||
"oidc_enabled": bool(config["keycloak"]["server_url"]), | "oidc_enabled": bool(config["keycloak"]["server_url"]), | ||||
"browsers_supported_image_mimes": browsers_supported_image_mimes, | |||||
} | } | ||||
class EnforceCSRFAuthentication(SessionAuthentication): | class EnforceCSRFAuthentication(SessionAuthentication): | ||||
""" | """ | ||||
Helper class to enforce CSRF validation on a DRF view | Helper class to enforce CSRF validation on a DRF view | ||||
when a user is not authenticated. | when a user is not authenticated. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |