Esempio n. 1
0
def user_detail(request, pk):
    """
    Retrieve, update or delete user
    You can only change fields 'exp' and 'stage'
    :param request: request
    :param pk: User's id in db
    :return: Response
    """
    try:
        snippet = User.objects.get(pk=pk)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = UserSerializer(snippet)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = UserSerializer(snippet, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Esempio n. 2
0
def register(request):
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = RegistrationForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # x = form.save()
            print(form.cleaned_data)
            # {'username': '******', 'password': '******'}
            password = form.cleaned_data.get('password')
            token = encrypt(password, 5)
            form.cleaned_data['password'] = token
            print(decrypt(token, 26 - 5))
            print(form.cleaned_data)
            tutorial_serializer = UserSerializer(data=form.cleaned_data)
            if tutorial_serializer.is_valid():
                tutorial_serializer.save()
                username = form.cleaned_data.get('username')
                #messages.success(request, "New account created: ",username)
                #login(request, tutorial_serializer)
                #return redirect("main:homepage")
                # process the data in form.cleaned_data as required
                # ...
                # redirect to a new URL:
                response = {"status": "Account Created"}
                return render(request=request,
                              template_name="main/home.html",
                              context={"response": response})

    # if a GET (or any other method) we'll create a blank form
    else:
        form = RegistrationForm
        return render(request=request,
                      template_name="main/register.html",
                      context={"form": form})
Esempio n. 3
0
    def post(self, request):
        serializer = UserSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response({"msg": "success"})
        return Response({"msg": "error"})
Esempio n. 4
0
 def put(self, request, pk, format=None):
     user = self.get_object(pk=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)
Esempio n. 5
0
 def post(self, request):
     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_500_INTERNAL_SERVER_ERROR)
Esempio n. 6
0
 def post(self, request, format='json'):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         user = serializer.save()
         if user:
             return Response(serializer.data,
                             status=status.HTTP_201_CREATED)
Esempio n. 7
0
    def post(self, request, format=None):
        serializer = UserSerializer(data=request.data)

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

        return JsonResponse(serializer.errors, status=400)
Esempio n. 8
0
def register(request):
    if request.method == 'POST':
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()
            if user:
                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)
Esempio n. 9
0
    def post(self, request, format='json'):
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()
            if user:
                token = Token.objects.create(user=user)
                json = serializer.data
                json['token'] = token.key
                return Response(json, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 10
0
 def put(self, request, id=None):
     user = User.objects.filter(id=id).last()
     if request.user.is_superuser or request.user == user:
         serializer = UserSerializer(user, data=request.data)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data)
         return Response(serializer.errors, status=400)
     return Response(
         {'result': 'You are not authorized to see the result.'},
         status=401)
Esempio n. 11
0
def user_list(request):
    if request.method == 'GET':
        countries = User.objects.all()
        serializer = UserSerializer(countries, many=True)
        return Response(serializer.data)

    elif 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({'error': serializer.errors},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Esempio n. 12
0
def register(request):
    if request.method != 'POST':
        return HttpResponse(status=405)

    data = JSONParser().parse(request)
    data['registerDate'] = datetime.datetime.now().date()
    data['password_hash'] = encode_password(data.get('password', ''))
    del data['password']

    serializer = UserSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
        return HttpResponse(status=201)
    return JsonResponse(serializer.errors, status=400)
Esempio n. 13
0
    def create(self, request):
        """
        User registration.

        POST /users
        """
        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,
            )
Esempio n. 14
0
    def post(self, request):
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            if serializer.validated_data[
                    'encrypted_password'] == serializer.validated_data[
                        'smtp_password']:
                serializer.validated_data.pop('smtp_password', None)
                user = serializer.save()
                user.set_password(
                    serializer.validated_data['encrypted_password'])
                user.save()
                return Response(serializer.data, status=201)
            else:
                return Response({'result': 'Both password should match.'})

        return Response(serializer.errors, status=400)
Esempio n. 15
0
    def post(self, request):
        """Handle the posting of data."""

        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()

            response = {"code": SUCCESS_CODE,
                        "id": user.id,
                        "email": serializer.validated_data["email"],
                        }
            return Response(response, status=status.HTTP_201_CREATED)

        response = {"code": ERROR_CODE,
                    "errors": serializer.errors
                    }

        return Response(response, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 16
0
def user_detail(request, country_id):
    try:
        user = User.objects.get(id=country_id)
    except User.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        serializer = UserSerializer(user)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = UserSerializer(instance=user, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors})

    elif request.method == 'DELETE':
        user.delete()
        return Response({'deleted': True})
Esempio n. 17
0
 def put(self, request):
     serializer = UserSerializer(request.user, data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data)
Esempio n. 18
0
 def post(request):
     user_serializer = UserSerializer(data=request.json)
     if user_serializer.is_valid():
         user_serializer.save()
         return JsonResponse({})
     return json_error(user_serializer.errors)
Esempio n. 19
0
 def patch(request):
     user_serializer = UserSerializer(request.user, data=request.json, partial=True)
     if user_serializer.is_valid():
         user_serializer.save()
         return JsonResponse(user_serializer.data)
     return json_error(user_serializer.errors)