sss
							parent
							
								
									6add6729ec
								
							
						
					
					
						commit
						d9c93fd213
					
				
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,15 @@ | ||||
| # serializers.py in Osina app | ||||
| from rest_framework import serializers | ||||
| from osinacore.models import * | ||||
| 
 | ||||
| class UserSerializer(serializers.ModelSerializer): | ||||
|     class Meta: | ||||
|         model = User | ||||
|         fields = ['id', 'username', 'email', 'password'] | ||||
| 
 | ||||
| class CustomerProfileSerializer(serializers.ModelSerializer): | ||||
|     user = UserSerializer(required=True) | ||||
| 
 | ||||
|     class Meta: | ||||
|         model = CustomerProfile | ||||
|         fields = ['user', 'mobile_number', 'status', 'reference'] | ||||
| @ -0,0 +1,7 @@ | ||||
| from django.urls import path | ||||
| from . import views | ||||
| 
 | ||||
| 
 | ||||
| urlpatterns = [   | ||||
|     path('register/', views.register_customer, name='register_customer'), | ||||
| ] | ||||
| @ -0,0 +1,25 @@ | ||||
| from django.contrib.auth.models import User | ||||
| from rest_framework import status | ||||
| from rest_framework.decorators import api_view | ||||
| from rest_framework.response import Response | ||||
| from osinacore.models import * | ||||
| from .serializers import CustomerProfileSerializer, UserSerializer | ||||
| 
 | ||||
| @api_view(['POST']) | ||||
| def register_customer(request): | ||||
|     if 'user' in request.data and 'customer' in request.data: | ||||
|         user_serializer = UserSerializer(data=request.data['user']) | ||||
|         if user_serializer.is_valid(): | ||||
|             user = user_serializer.save() | ||||
|             customer_data = request.data['customer'] | ||||
|             customer_data['user'] = user.id | ||||
|             customer_serializer = CustomerProfileSerializer(data=customer_data) | ||||
|             if customer_serializer.is_valid(): | ||||
|                 customer_serializer.save() | ||||
|                 return Response({'message': 'User and Customer registered successfully'}, status=status.HTTP_201_CREATED) | ||||
|             else: | ||||
|                 return Response(customer_serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||||
|         else: | ||||
|             return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||||
|     else: | ||||
|         return Response({'message': 'User and Customer data are required'}, status=status.HTTP_400_BAD_REQUEST) | ||||
											
												Binary file not shown.
											
										
									
								
					Loading…
					
					
				
		Reference in New Issue