def post(self, request, *args, **kwargs): print(request.data) serializer = AuthenticateUserSerializer(data=request.data) if serializer.is_valid(): username = serializer.validated_data.get("username") imei_number = serializer.validated_data['imei_number'] if User.objects.filter(email=username).exists(): username = User.objects.get(email=username).username else: username = sms_utils.Sms().format_phone_number(username) user = authenticate(username=username, password=serializer.validated_data.get("pin")) if user is not None: imei_exists = Member.objects.filter( imei_number=imei_number).exists() if imei_exists: if imei_number == user.member.imei_number: login(request, user) user.member.device_token = serializer.validated_data[ 'app_token'] user.member.save() token, created = Token.objects.get_or_create(user=user) serializer = MemberSerializer(request.user.member) data = { "status": 1, "token": token.key, "member": serializer.data } return Response(data, status=status.HTTP_200_OK) else: data = { "status": 0, "message": "Accessing your account from other devices is not permitted." } return Response(data, status=status.HTTP_200_OK) else: login(request, user) user.member.device_token = serializer.validated_data[ 'app_token'] user.member.imei_number = imei_number user.member.save() token, created = Token.objects.get_or_create(user=user) serializer = MemberSerializer(request.user.member) data = { "status": 1, "token": token.key, "member": serializer.data } return Response(data, status=status.HTTP_200_OK) data = {"status": 0, "message": "Invalid credentials"} return Response(data, status=status.HTTP_200_OK) data = {"status": 0, "message": serializer.errors} return Response(data, status=status.HTTP_200_OK)
def put(self, request): """ Modify details for currently logged in member, fields that is not included in the JSON will be left unchanged --- serializer: member.serializers.MemberSerializer """ serializedMember = MemberSerializer(request.user ,data=request.data, partial=True) if serializedMember.is_valid(): serializedMember.save() return Response({'user':serializedMember.data}) return Response({'error':serializedMember.errors}, status=status.HTTP_400_BAD_REQUEST)
class Participated_Group_Serializer(ModelSerializer): GroupPid = GroupSerializer(read_only=True) member_id = MemberSerializer(read_only=True) class Meta: model = Participate fields = ['member_id', 'GroupPid', 'Nickname']
class ParticipateSerializer(ModelSerializer): GroupKey = GroupSerializer(read_only=True) MemberKey = MemberSerializer(read_only=True) class Meta: model = Participate fields = '__all__'
class DeliveryBookingSerializer(serializers.HyperlinkedModelSerializer): member = MemberSerializer(required=False, read_only=True) class Meta: model = DeliveryBooking fields = [ 'member', 'booking_no', 'id', 'vehicle_type', 'driver', 'destination_latitude', 'destination_longitude', 'destination_address', 'pick_up_latitude', 'pick_up_longitude', 'pick_up_address', 'fee', 'booking_time', 'pick_up_time', 'recipient_name', 'recipient_phone_number', 'total_weight' ] read_only_fields = [ 'booking_no', 'id', 'vehicle_type', 'driver', 'booking_time', 'pick_up_time' ] def create(self, validated_data): booking = DeliveryBooking.objects.create(**validated_data) booking.member = self.context['request'].user.member if booking.total_weight > 10: booking.vehicle_type = 1 booking.destination_address = booking.destination_address.replace( ", null, null", ".") booking.pick_up_address = booking.pick_up_address.replace( ", null, null", ".") booking.save() return booking
def member_list(request): """ List all code member, or create a new member. """ if request.method == 'GET': member = Member.objects.all() serializer = MemberSerializer(member, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = MemberSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def post(self, request, *args, **kwargs): member_data = request.data.get('member') member_data['employee_id'] = get_next_bp_employee_id() organization_period = request.data.get('organization_period') salesperson_period = request.data.get('salesperson_period') bp_contract_data = request.data.get('bp_contract') member_serializer = MemberSerializer(data=member_data) errors = {} if member_serializer.is_valid(): member = member_serializer.save() organization_period['member'] = member.pk salesperson_period['member'] = member.pk bp_contract_data['member'] = member.pk bp_contract_data['company'] = kwargs.get('pk') organization_period_serializer = OrganizationPeriodSerializer( data=organization_period) salesperson_period_serializer = SalespersonPeriodSerializer( data=salesperson_period) bp_contract_serializer = serializers.BpContractSerializer( data=bp_contract_data) if organization_period_serializer.is_valid() \ and salesperson_period_serializer.is_valid() \ and bp_contract_serializer.is_valid(): organization_period_serializer.save() salesperson_period_serializer.save() bp_contract_serializer.save() return Response({'pk': member.pk}) else: errors[ 'organization_period'] = organization_period_serializer.errors errors[ 'salesperson_period'] = salesperson_period_serializer.errors errors['bp_contract'] = bp_contract_serializer.errors else: errors['member'] = member_serializer.errors return Response(errors, status=rest_status.HTTP_400_BAD_REQUEST)
def member_detail(request, pk): """ Retrieve, update or delete a code member. """ try: member = Member.objects.get(pk=pk) except Member.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = MemberSerializer(member) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = MemberSerializer(member, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': member.delete() return HttpResponse(status=204)
def user_member_info(request, email): """获取用户信息,权限合适则扩展队员信息,不合适则与`/user/user/<str:email>/`接口相同""" target: User = get_object_or_404(User.objects.filter(is_banned=False), email=email) inquirer: User = request.user if (not isinstance(inquirer, AnonymousUser)) \ and inquirer.role in (User.Role.MEMBER, User.Role.COACH) \ and target.role == User.Role.MEMBER : # 查询者与被查者都为队内人员 serializer = MemberSerializer(target.member) member_data = dict(serializer.data) user_data = member_data.pop('user') user_data['member'] = member_data return Response(user_data) return UserAPIView.as_view({'get': 'retrieve'})(request._request, email=email)
class RideBookingSerializer(serializers.HyperlinkedModelSerializer): member = MemberSerializer(required=False, read_only=True) class Meta: model = RideBooking fields = [ 'member', 'booking_no', 'id', 'vehicle_type', 'driver', 'booking_time', 'pick_up_time', 'destination_latitude', 'destination_longitude', 'destination_address', 'pick_up_latitude', 'pick_up_longitude', 'pick_up_address', 'fee' ] read_only_fields = [ 'booking_no', 'id', 'driver', 'booking_time', 'pick_up_time' ] def create(self, validated_data): booking = RideBooking.objects.create(**validated_data) booking.member = self.context['request'].user.member booking.save() return booking