Skip to content

Commit

Permalink
Fix: Override rest auth default password change view to remove transl…
Browse files Browse the repository at this point in the history
…ation, change cover image, logo url

Signed-off-by: George J Padayatti <george.padayatti@igrant.io>
  • Loading branch information
georgepadayatti committed Apr 5, 2024
1 parent 75838e5 commit 76f22ae
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .views import DataSourceView, DataSourceCoverImageView, DataSourceLogoImageView, AdminView, DataSourceVerificationView, VerificationTemplateView, DataSourceOpenApiUrlView
from connection.views import DISPConnectionView, DISPConnectionsView
from data_disclosure_agreement.views import DataDisclosureAgreementsView
from rest_auth.views import PasswordChangeView
from config.views import PasswordChangeView
from django.views.decorators.csrf import csrf_exempt

urlpatterns = [
Expand Down
70 changes: 58 additions & 12 deletions config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
)
from .models import DataSource, Verification, ImageModel, VerificationTemplate
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework import status, permissions
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_auth.serializers import PasswordChangeSerializer
from rest_auth.views import sensitive_post_parameters_m
from onboard.serializers import DataspaceUserSerializer
from connection.models import Connection
from dataspace_backend.settings import DATA_MARKETPLACE_DW_URL, DATA_MARKETPLACE_APIKEY
Expand All @@ -16,15 +21,25 @@
# Create your views here.


def construct_cover_image_url(baseurl: str, is_public_endpoint: bool = False):
def construct_cover_image_url(
baseurl: str,
data_source_id: str,
is_public_endpoint: bool = False
):
protocol = "https://" if os.environ.get("ENV") == "prod" else "http://"
endpoint = "/service/data-source/logoimage/" if is_public_endpoint else "/config/data-source/logoimage/"
url_prefix = "service" if is_public_endpoint else "config"
endpoint = f"/{url_prefix}/data-source/{data_source_id}/coverimage/"
return f"{protocol}{baseurl}{endpoint}"


def construct_logo_image_url(baseurl: str, is_public_endpoint: bool = False):
def construct_logo_image_url(
baseurl: str,
data_source_id: str,
is_public_endpoint: bool = False
):
protocol = "https://" if os.environ.get("ENV") == "prod" else "http://"
endpoint = "/service/data-source/logoimage/" if is_public_endpoint else "/config/data-source/logoimage/"
url_prefix = "service" if is_public_endpoint else "config"
endpoint = f"/{url_prefix}/data-source/{data_source_id}/logoimage/"
return f"{protocol}{baseurl}{endpoint}"


Expand All @@ -45,14 +60,8 @@ def post(self, request):

request_data = request.data.get("dataSource", {})

request_data["coverImageUrl"] = construct_cover_image_url(
baseurl=request.get_host(),
is_public_endpoint=False
)
request_data["logoUrl"] = construct_logo_image_url(
baseurl=request.get_host(),
is_public_endpoint=False
)
request_data["coverImageUrl"] = "nil"
request_data["logoUrl"] = "nil"

request_data["openApiUrl"] = ""

Expand All @@ -64,6 +73,20 @@ def post(self, request):
admin=admin, **serializer.validated_data
)

# TODO: Add default cover image and logo image URL
# Update data source with cover and logo image URL
datasource.coverImageUrl = construct_cover_image_url(
baseurl=request.get_host(),
data_source_id=str(datasource.id),
is_public_endpoint=True
)
datasource.logoUrl = construct_logo_image_url(
baseurl=request.get_host(),
data_source_id=str(datasource.id),
is_public_endpoint=True
)
datasource.save()

# Serialize the created instance to match the response format
response_serializer = self.serializer_class(datasource)
return JsonResponse(
Expand Down Expand Up @@ -189,6 +212,7 @@ def put(self, request):

datasource.coverImageUrl = construct_cover_image_url(
baseurl=request.get_host(),
data_source_id=str(datasource.id),
is_public_endpoint=True
)

Expand Down Expand Up @@ -251,6 +275,7 @@ def put(self, request):

datasource.logoUrl = construct_logo_image_url(
baseurl=request.get_host(),
data_source_id=str(datasource.id),
is_public_endpoint=True
)
datasource.save()
Expand Down Expand Up @@ -456,3 +481,24 @@ def put(self, request):
# Serialize the updated DataSource instance
serializer = self.serializer_class(datasource)
return JsonResponse({"dataSource": serializer.data}, status=status.HTTP_200_OK)


class PasswordChangeView(GenericAPIView):
"""
Calls Django Auth SetPasswordForm save method.
Accepts the following POST parameters: new_password1, new_password2
Returns the success/fail message.
"""
serializer_class = PasswordChangeSerializer
permission_classes = (IsAuthenticated,)

@sensitive_post_parameters_m
def dispatch(self, *args, **kwargs):
return super(PasswordChangeView, self).dispatch(*args, **kwargs)

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response({"detail": "New password has been saved."})
2 changes: 2 additions & 0 deletions dataspace_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
],
}

OLD_PASSWORD_FIELD_ENABLED = True

SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(days=1),
}
Expand Down
4 changes: 2 additions & 2 deletions service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

urlpatterns = [

path("data-source/<uuid:dataSourceId>/coverimage",
path("data-source/<uuid:dataSourceId>/coverimage/",
DataSourceCoverImageView.as_view(), name="datasource_coverimage"),
path("data-source/<uuid:dataSourceId>/logoimage",
path("data-source/<uuid:dataSourceId>/logoimage/",
DataSourceLogoImageView.as_view(), name="datasource_logoimage"),
path("data-sources/",
DataSourcesView.as_view(), name="datasources"),
Expand Down

0 comments on commit 76f22ae

Please sign in to comment.