Exemplo n.º 1
0
 def put(self, request, format=None):
     user = request.user
     serializer = UserSerializer(user, request.data, partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def put(self, request, pk, format=None):
     user = self.get_object(pk)
     serializer = UserSerializer(user, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def post(self, request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         UserProfile.objects.create(user=NewUser.objects.get(id=serializer.data['id']))
         return Response({'status': 'success'}, status=status.HTTP_201_CREATED)
     return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
def create_user(request):
    serialized = UserSerializer(data=request.data)
    if serialized.is_valid():
        serialized.save()
        return Response("You have successfully created user!",status=status.HTTP_201_CREATED)
    else:
        return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
 def test_updating_other_field_with_userserialize(self):
     """Test not updating other fields"""
     serializer = UserSerializer(instance=self.organizer,
                                 data={'email': fake.safe_email()})
     self.assertTrue(serializer.is_valid())
     serializer.save()
     self.assertEqual(self.organizer.email, self.email)
Exemplo n.º 6
0
def add_user(request, format=None):
    data = request.data
    serializer = UserSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 7
0
 def post(self, request, *args, **kwargs):
     serializer_data = request.data.get('username', {})
     request.user.is_active = False
     serializer = UserSerializer(request.user, data=request.data, partial=True)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response("The user has been deactivated", status=status.HTTP_200_OK)
Exemplo n.º 8
0
 def update(self, instance: Member, validated_data: dict):
     user_data: dict = validated_data.pop('user', None)
     if user_data is not None:
         serializer = UserSerializer(instance.user, user_data, partial=True)
         serializer.is_valid(raise_exception=True)
         serializer.save()
     return super().update(instance, validated_data)
Exemplo n.º 9
0
    def create(self, request, *args, **kwargs):
        context = {'request': request}

        serializer = UserSerializer(data=request.data, context=context)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Exemplo n.º 10
0
Arquivo: entry.py Projeto: moment-x/f
def update_user_info(request):
    request.data['contact'] = request.user.contact
    # keep contact inviriant, password is automatically safe
    serializer = UserSerializer(request.user, data=request.data)
    if serializer.is_valid():
        serializer.save()
        return CostaResponse('0', 'OK')
Exemplo n.º 11
0
    def post(self, request):
        profile = UserProfile.objects.get(user=request.user)
        serializer = UserSerializer(instance=profile, data=request.data)
        if serializer.is_valid():
            logger.critical('user ' + profile.first_name + ' updated account')
            serializer.save()

        return Response(serializer.data)
Exemplo n.º 12
0
 def post(self, request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         logger.info(f"user with username={request.data.get('username')} was created.")
         return Response(serializer.data)
     logger.error(serializer.errors)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 13
0
    def test_update_user_successful(self):
        """Test update user fields successful"""
        data = {'is_guide': False}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertTrue(serializer.is_valid())

        serializer.save()
        self.assertEqual(serializer.data['is_guide'], data['is_guide'])
Exemplo n.º 14
0
 def put(self, request, pk):
     user = self.get_object(pk)
     serializer = UserSerializer(user, data=request.data, partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(response_data(True, serializer.data))
     else:
         return Response(response_data(False, serializer.errors),
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 15
0
def updateUser(request):  # 根据id修改数据
    if User.objects.filter(id=request.data['id']):
        user = User.objects.get(id=request.data['id'])
        ser = UserSerializer(instance=user, data=request.data)  # 注意指定参数
        if ser.is_valid():
            ser.save()
            return Response(str('success'))
        return Response(ser.errors)
    return Response(str('没有此id'))
Exemplo n.º 16
0
    def post(self, request):
        json_parser = JSONParser()
        data = json_parser.parse(request)
        serializer = UserSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 17
0
def sign_up(request):
    if request.method == "POST":
        data = JSONParser().parse(request)
        hashed = bcrypt.hashpw(data["password"].encode('utf-8'), SALT).decode("utf-8")
        data["password"] = hashed
        user = UserSerializer(data = data)
        if user.is_valid():
            user.save()
            return JsonResponse(user.data, status = 200)
        return JsonResponse(user.errors, status = 400)
Exemplo n.º 18
0
def register(request):
    """
    Register a new account
    """
    if request.method == 'POST':
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 19
0
 def post(self, request, format=None):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         username = serializer.data.get('username')
         if (User.objects.filter(username=username).exists()):
             return Response({"username": ["用户名已存在"]}, status=status.HTTP_400_BAD_REQUEST)
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     print(serializer.errors)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 20
0
def createUser(request):
    user = request.data
    print(user)
    if not user.__getitem__('name') or not user.__getitem__('fbId'):
        return Response('Ajout impossible : Champs manquants',
                        status=status.HTTP_400_BAD_REQUEST)
    serializer = UserSerializer(data=user)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 21
0
def register_user(request):
    if request.method == 'POST':
        user_data = JSONParser().parse(request)
        #user_data = {'id':5,'firstname':'salma','email': '*****@*****.**','password':'******'}
        user_serializer = UserSerializer(data=user_data)
        if user_serializer.is_valid():
            user_serializer.save()
            return JsonResponse(user_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(user_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 22
0
def insert(request):
    if request.method == 'POST':
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 23
0
def create_user_default(request, user_id):
    try:
        user = User.objects.get(pk=user_id)
    except:
        return HttpResponse(status=404)
    received_data = JSONParser().parse(request)
    user_data = UserSerializer(user, received_data, partial=True)
    print(user_data.data)
    if not user_data.is_valid():
        return JsonResponse(user_data.errors, status=400)
    user_data.save()
    return JsonResponse(user_data.data)
Exemplo n.º 24
0
 def post(self, request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(response_data(True, serializer.data))
     else:
         try:
             User.objects.get(username=request.data['username'])
             return Response(response_data(False, serializer.errors),
                             status=status.HTTP_409_CONFLICT)
         except User.DoesNotExist:
             return Response(response_data(False, serializer.errors))
Exemplo n.º 25
0
 def post(self, request):
     user = request.data
     serializer = UserSerializer(data=user)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     user = User.objects.get(email=request.data['email'], password=request.data['password'])
     payload = jwt_payload_handler(user)
     token = jwt.encode(payload, settings.SECRET_KEY)
     user_details = {}
     user_details['name'] = "%s %s" % (user.first_name, user.last_name)
     user_details['token'] = token
     user_logged_in.send(sender=user.__class__, request=request, user=user)
     return Response(user_details, status=status.HTTP_201_CREATED)
def get_update_edit(request, id):
    try:
        user = User.objects.get(id=id)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

        # update details of a single employee
    if request.method == 'PUT':
        serializer = UserSerializer(user, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 27
0
def add_update(data):
    '''
    @summary: add (if no) or update a user record.
    @parm data: a dict for the User's serializer.
    @return: None.
    '''
    try:
        user = User.objects.get(openid=data['openid'])
        user_se = UserSerializer(user, data=data, partial=True)
    except User.DoesNotExist:
        user_se = UserSerializer(data=data)
    if user_se.is_valid():
        user_se.save()
def add(request):
    if request.method == 'POST':
        data = {
            "uid": request.data.get('uid'),
            "uname": request.data.get('uname'),
            "uemail": request.data.get('uemail'),
            "upassword": request.data.get('upassword'),
            "ucontact": request.data.get('ucontact')
        }
        serializer = UserSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 29
0
def create_empty_user(request):
    data = {
        'name': '',
        'clock': None,
        'token': '',
        'drinking': False,
        'smoking': False
    }
    serializer = UserSerializer(data=data)

    if serializer.is_valid():
        serializer.save()
        return JsonResponse(serializer.data, status=201)

    return JsonResponse(serializer.errors, status=400)
Exemplo n.º 30
0
def signup(request, format=None):
    if request.method == 'POST':
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            digits = [i for i in range(0, 10)]
            random_str = ""

            for i in range(6):
                index = math.floor(random.random() * 10)
                random_str += str(digits[index])
            serializer.save()
            newdict = {'otp': random_str}
            newdict.update(serializer.data)
            return Response(newdict, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 31
0
    def post(self, request, *args, **kwargs):
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.validated_data
            try:
                okta_user_id = create_okta_user(data)
                send_email(data['email'], okta_user_id)
                serializer.save(okta_id=okta_user_id)
                return JsonResponse({"result": {"okta_user_id": okta_user_id}},
                                    status=status.HTTP_201_CREATED)
            except Exception as e:
                return JsonResponse({"result": {'error': e.args[0]}},
                                    status=status.HTTP_400_BAD_REQUEST)

        return JsonResponse({"result": {'error': serializer.errors}},
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 32
0
 def post(self, request):
     user_token = request.data['token']
     res = self.debug_token(user_token)
     sns_id = res['data']['user_id']
     try:
         user = User.objects.get(sns_type='facebook', sns_id=sns_id)
         # token 갱신
         data = {'sns_access_token': user_token}
         serializer = UserSerializer(user, data=data, partial=True)
         if serializer.is_valid():
             serializer.save()
             return Response(response_data(True, serializer.data))
         else:
             return Response(response_data(False, 'FB 토큰 갱신 에러'))
     except User.DoesNotExist:
         return Response(response_data(True, None))