Merge pull request #1014 from Mte90/rest-create-user

Create user by REST endpoint
This commit is contained in:
Garret Wassermann 2022-05-25 07:41:50 -04:00 committed by GitHub
commit f88603ab53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 2 deletions

View File

@ -46,6 +46,16 @@ Here is an example of a cURL request to create a ticket (using Basic authenticat
--header 'Content-Type: application/json' \ --header 'Content-Type: application/json' \
--data-raw '{"queue": 1, "title": "Test Ticket API", "description": "Test create ticket from API", "submitter_email": "test@mail.com", "priority": 4}' --data-raw '{"queue": 1, "title": "Test Ticket API", "description": "Test create ticket from API", "submitter_email": "test@mail.com", "priority": 4}'
Accessing the endpoint ``/api/users/`` with a **POST** request will let you create a new user.
You need to provide a JSON body with the following data :
- **first_name**: first name
- **last_name**: last name
- **username**: username
- **email**: user email
- **password**: user password
PUT PUT
--- ---

View File

@ -1,4 +1,5 @@
from rest_framework import serializers from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.humanize.templatetags import humanize from django.contrib.humanize.templatetags import humanize
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
@ -110,3 +111,18 @@ class TicketSerializer(serializers.ModelSerializer):
instance = super().update(instance, validated_data) instance = super().update(instance, validated_data)
instance.save_custom_field_values(validated_data) instance.save_custom_field_values(validated_data)
return instance return instance
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta:
model = User
fields = ('first_name', 'last_name', 'username', 'email', 'password')
def create(self, validated_data):
user = super(UserSerializer, self).create(validated_data)
user.is_active = True
user.set_password(validated_data['password'])
user.save()
return user

View File

@ -17,7 +17,7 @@ from rest_framework.routers import DefaultRouter
from helpdesk.decorators import helpdesk_staff_member_required, protect_view from helpdesk.decorators import helpdesk_staff_member_required, protect_view
from helpdesk.views import feeds, staff, public, login from helpdesk.views import feeds, staff, public, login
from helpdesk import settings as helpdesk_settings from helpdesk import settings as helpdesk_settings
from helpdesk.views.api import TicketViewSet from helpdesk.views.api import TicketViewSet, CreateUserView
if helpdesk_settings.HELPDESK_KB_ENABLED: if helpdesk_settings.HELPDESK_KB_ENABLED:
from helpdesk.views import kb from helpdesk.views import kb
@ -226,6 +226,7 @@ urlpatterns += [
if helpdesk_settings.HELPDESK_ACTIVATE_API_ENDPOINT: if helpdesk_settings.HELPDESK_ACTIVATE_API_ENDPOINT:
router = DefaultRouter() router = DefaultRouter()
router.register(r'tickets', TicketViewSet, basename='ticket') router.register(r'tickets', TicketViewSet, basename='ticket')
router.register(r'users', CreateUserView, basename='user')
urlpatterns += [ urlpatterns += [
url(r'^api/', include(router.urls)) url(r'^api/', include(router.urls))
] ]

View File

@ -1,8 +1,11 @@
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.permissions import IsAdminUser from rest_framework.permissions import IsAdminUser
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin
from django.contrib.auth import get_user_model
from helpdesk.models import Ticket from helpdesk.models import Ticket
from helpdesk.serializers import TicketSerializer from helpdesk.serializers import TicketSerializer, UserSerializer
class TicketViewSet(viewsets.ModelViewSet): class TicketViewSet(viewsets.ModelViewSet):
@ -23,3 +26,9 @@ class TicketViewSet(viewsets.ModelViewSet):
ticket = super().get_object() ticket = super().get_object()
ticket.set_custom_field_values() ticket.set_custom_field_values()
return ticket return ticket
class CreateUserView(CreateModelMixin, GenericViewSet):
queryset = get_user_model().objects.all()
serializer_class = UserSerializer
permission_classes = [IsAdminUser]