コード例 #1
0
ファイル: user.py プロジェクト: LEEJ0NGWAN/FreeChart
    def post(self, request):
        data = json.loads(request.body.decode("utf-8"))

        if 'password' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        if 'email' in data:
            email = data.get('email')
            username = data.get('username', email)
            password = data.get('password', '')

            if User.objects.filter(email=email).exists():
                return JsonResponse({}, status=HTTP_409_CONFLICT)

            new_user = User.objects.create_user(
                username=username,
                email_verified=False,
                email=email,
                password=password,
            )

            SetDefaultData(new_user.id)

            refresh = RefreshToken.for_user(new_user)

        else:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        return JsonResponse(
            serialize({
                'refresh': str(refresh),
                'access': str(refresh.access_token),
                'user': serialize(new_user)
            }))
コード例 #2
0
ファイル: user.py プロジェクト: LEEJ0NGWAN/FreeChart
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)
        data = request.GET

        if 'id' in data:
            user = User.objects.filter(id=data['id']).first()

            if not user:
                return JsonResponse({}, status=HTTP_404_NOT_FOUND)
            return JsonResponse(serialize({'user': user}))
        return JsonResponse(serialize({'user': request.user}))
コード例 #3
0
ファイル: board.py プロジェクト: LEEJ0NGWAN/FreeChart
    def put(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)

        data = json.loads(request.body.decode("utf-8"))

        if 'id' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        board = Board.objects\
            .filter(
                id=data.get('id'),
                owner_id=request.user.id,
                deleted=False).first()

        if not board:
            return JsonResponse({}, status=HTTP_404_NOT_FOUND)

        if 'title' in data:
            board.title = data['title']

        if 'parent_id' in data:
            board.parent_id = data['parent_id']

        board.save()

        return JsonResponse(serialize({'board': board}))
コード例 #4
0
    def put(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)

        data = json.loads(request.body.decode("utf-8"))

        if 'id' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        sheet = Sheet.objects\
            .filter(
                id=data.get('id'),
                owner_id=request.user.id,
                deleted=False).first()

        if not sheet:
            return JsonResponse({}, status=HTTP_404_NOT_FOUND)

        if 'title' in data:
            sheet.title = data.get('title')

        if 'board_id' in data:
            sheet.board_id = data.get('board_id')

        sheet.save()

        return JsonResponse(serialize({'sheet': sheet}))
コード例 #5
0
ファイル: board.py プロジェクト: LEEJ0NGWAN/FreeChart
    def delete(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)
        data = request.GET

        if 'id' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        board = Board.objects\
            .filter(
                id=data['id'],
                owner_id=request.user.id,
                deleted=False).first()

        if not board:
            return JsonResponse({}, status=HTTP_404_NOT_FOUND)

        board.deleted = True
        board.save()

        if data.get('save_child'):
            boards = Board.objects\
                .filter(
                    parent=board,
                    deleted=False).all()

            sheets = Sheet.objects\
                .filter(
                    board=board,
                    deleted=False).all()

            response = serialize(
                {
                    'parent': board,
                    'boards': boards,
                    'sheets': sheets
                },
                change_parent=True,
                new_parent_id=board.parent_id)

            boards.update(parent_id=board.parent_id)
            sheets.update(board_id=board.parent_id)

            return JsonResponse(response)

        return JsonResponse(serialize({'board': board}))
コード例 #6
0
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)
        data = request.GET

        if 'id' in data:
            sheet = Sheet.objects\
                .filter(
                    id=data['id'],
                    owner_id=request.user.id,
                    deleted=False).first()

            if not sheet:
                return JsonResponse({}, status=HTTP_404_NOT_FOUND)

            return JsonResponse(serialize({'sheet': sheet}))

        order = data.get('order', '-modify_date')
        if order not in [
                '-modify_date', '-create_date', '-title', 'modify_date',
                'create_date', 'title'
        ]:
            order = '-modify_date'

        if 'board_id' in data:
            sheets = Sheet.objects\
                .filter(
                    board_id=data['board_id'],
                    owner_id=request.user.id,
                    deleted=False)\
                .order_by(order).all()

        else:
            sheets = Sheet.objects\
                .filter(
                    owner_id=request.user.id,
                    deleted=False)\
                .order_by(order).all()

        if not sheets:
            return JsonResponse({}, status=HTTP_404_NOT_FOUND)

        return JsonResponse(serialize({'sheets': sheets}))
コード例 #7
0
    def post(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)

        data = json.loads(request.body.decode("utf-8"))

        new_sheet = Sheet.objects.create(title=data.get('title'),
                                         board_id=data.get('board_id'),
                                         owner_id=request.user.id)

        return JsonResponse(serialize({'sheet': new_sheet}))
コード例 #8
0
ファイル: views_rest.py プロジェクト: LEEJ0NGWAN/FreeChart
    def post(self, request):
        """
        {string} email
        {string} password
        """

        data = json.loads(request.body.decode("utf-8"))
        email = data.get('email')
        password = data.get('password')

        if (email is None or password is None)\
        or (email == '' or password == ''):
            return JsonResponse({'error': 'email or password error'},
                                status=HTTP_400_BAD_REQUEST)

        try:
            user = User.objects.get(email=email)
        except:
            user = None

        if not user:
            return JsonResponse({'error': 'no user'},
                                status=HTTP_404_NOT_FOUND)

        if not user.check_password(password):
            return JsonResponse({'error': 'incorrect password'},
                                status=HTTP_400_BAD_REQUEST)

        # legacy
        # user = authenticate(username=email, password=password)

        # SESSION_KEY = '_auth_user_id'
        # BACKEND_SESSION_KEY = '_auth_user_backend'
        # HASH_SESSION_KEY = '_auth_user_hash'

        # key = user._meta.pk.value_to_string(user)

        # session_auth_hash = user.get_session_auth_hash()

        # request.session[SESSION_KEY] = key
        # request.session[BACKEND_SESSION_KEY] = user.backend
        # request.session[HASH_SESSION_KEY] = session_auth_hash
        # request.session.save()

        # request.user = user

        refresh = RefreshToken.for_user(user)

        return JsonResponse(
            serialize({
                'refresh': str(refresh),
                'access': str(refresh.access_token),
                'user': user
            }))
コード例 #9
0
ファイル: views_rest.py プロジェクト: LEEJ0NGWAN/FreeChart
    def get(self, request):
        data = request.GET

        if not data or 'email' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        res = {'email': True}

        if User.objects.filter(email=data['email']).exists():
            res['email'] = False

        return JsonResponse(serialize(res))
コード例 #10
0
ファイル: board.py プロジェクト: LEEJ0NGWAN/FreeChart
    def post(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)

        data = json.loads(request.body.decode("utf-8"))

        if 'title' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        new_board = Board.objects.create(title=data.get('title'),
                                         owner_id=request.user.id,
                                         parent_id=data.get('parent_id'))

        return JsonResponse(serialize({'board': new_board}))
コード例 #11
0
ファイル: user.py プロジェクト: LEEJ0NGWAN/FreeChart
    def put(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)

        user = request.user
        data = json.loads(request.body.decode("utf-8"))

        if 'username' in data:
            user.username = data['username']

        if 'password' in data:
            user.set_password(data['password'])
            # update_session_auth_hash(request, request.user)

        user.save()

        return JsonResponse(serialize({'user': user}))
コード例 #12
0
ファイル: views_rest.py プロジェクト: LEEJ0NGWAN/FreeChart
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)
        data = request.GET

        parent = None
        if 'id' in data:
            board = Board.objects\
                .filter(
                    id=data['id'],
                    owner_id=request.user.id,
                    deleted=False).first()

            if not board:
                return JsonResponse({}, status=HTTP_404_NOT_FOUND)

            parent = board

        order = data.get('order', '-modify_date')
        if order not in [
                '-modify_date', '-create_date', '-title', 'modify_date',
                'create_date', 'title'
        ]:
            order = '-modify_date'

        boards = Board.objects\
            .filter(
                owner_id=request.user.id,
                parent_id=data.get('id'),
                deleted=False)\
            .order_by(order).all()

        sheets = Sheet.objects\
            .filter(
                owner_id=request.user.id,
                board_id=data.get('id'),
                deleted=False)\
            .order_by(order).all()

        return JsonResponse(
            serialize({
                'parent': parent,
                'boards': boards,
                'sheets': sheets
            }))
コード例 #13
0
    def delete(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)

        data = request.GET

        if 'id' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        sheet = Sheet.objects\
            .filter(
                id=data['id'],
                owner_id=request.user.id,
                deleted=False).first()

        if not sheet:
            return JsonResponse({}, status=HTTP_404_NOT_FOUND)

        sheet.deleted = True
        sheet.save()

        return JsonResponse(serialize({'sheet': sheet}))
コード例 #14
0
ファイル: element.py プロジェクト: LEEJ0NGWAN/FreeChart
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({}, status=HTTP_401_UNAUTHORIZED)
        data = request.GET

        if 'sheet_id' not in data:
            return JsonResponse({}, status=HTTP_400_BAD_REQUEST)

        if not Sheet.objects\
            .filter(id=data['sheet_id'],
            owner_id=request.user.id,deleted=False).exists():
            return JsonResponse({}, status=HTTP_404_NOT_FOUND)

        nodes = Node.objects\
            .filter(
                sheet_id=data['sheet_id'],
                deleted=False).all()

        edges = Edge.objects\
            .filter(
                sheet_id=data['sheet_id'],
                deleted=False).all()

        return JsonResponse(serialize({'nodes': nodes, 'edges': edges}))
コード例 #15
0
ファイル: views_rest.py プロジェクト: LEEJ0NGWAN/FreeChart
 def get(self, request):
     return JsonResponse(
         serialize({
             'nodes': test_node_parse,
             'edges': test_edge_parse,
         }))