diff --git a/src/urban/upmigrate/urls.py b/src/urban/upmigrate/urls.py new file mode 100644 index 0000000..ab29b36 --- /dev/null +++ b/src/urban/upmigrate/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from .views import UpdateUserPassword, UpdateResourceOwner + +urlpatterns = [ + path('api/v2/extra/admin-set-user-password/', view=UpdateUserPassword.as_view()), + path('api/v2/extra/admin-set-resource-owner/', view=UpdateResourceOwner.as_view()), +] diff --git a/src/urban/upmigrate/views.py b/src/urban/upmigrate/views.py new file mode 100644 index 0000000..27f3109 --- /dev/null +++ b/src/urban/upmigrate/views.py @@ -0,0 +1,45 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import serializers, status, permissions + +from geonode.base.models import ResourceBase + +from django.contrib.auth import get_user_model + +User = get_user_model() + +class UserPasswordSerializer(serializers.Serializer): + username = serializers.CharField(required=True) + password = serializers.CharField(required=True) + + +class ChangeResourceOwnershipSerializer(serializers.Serializer): + username = serializers.CharField(required=True) + resource_id = serializers.IntegerField(required=True) + + +class UpdateUserPassword(APIView): + permission_classes = [permissions.IsAdminUser] + + def post(self, request): + serializer = UserPasswordSerializer(data=request.data) + if serializer.is_valid(): + u = User.objects.get(username=serializer.data['username']) + u.set_password(serializer.data['password']) + u.save() + return Response({'success': True}) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + +class UpdateResourceOwner(APIView): + permission_classes = [permissions.IsAdminUser] + + def post(self, request): + serializer = ChangeResourceOwnershipSerializer(data=request.data) + if serializer.is_valid(): + u = User.objects.get(username=serializer.data['username']) + r = ResourceBase.objects.get(id=serializer.data['resource_id']) + r.owner = u + r.save() + return Response({'success': True}) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file diff --git a/src/urban/urls.py b/src/urban/urls.py index 95be19c..760d819 100644 --- a/src/urban/urls.py +++ b/src/urban/urls.py @@ -18,13 +18,10 @@ # ######################################################################### +from django.urls import path, include from geonode.urls import urlpatterns -""" -# You can register your own urlpatterns here + urlpatterns = [ - url(r'^/?$', - homepage, - name='home'), - ] + urlpatterns -""" + path('', include('upmigrate')), +] + urlpatterns