Ejemplo n.º 1
0
    def post(self, request, format=None):

        # This is only for Parents
        if request.user.is_children:
            return Response(
                {
                    "status": status.HTTP_401_UNAUTHORIZED,
                    'message': "Children can't access this.",
                },
                status=status.HTTP_401_UNAUTHORIZED)

        accountTo = Account.objects.filter(user=request.user).first()

        accountFrom = Account.objects.filter(
            pk=request.data['account_to'],
            user__familymember__family__familymember__user=request.user).first(
            )

        if accountFrom is None:
            return Response(
                {
                    "status": status.HTTP_401_UNAUTHORIZED,
                    'message': "You can't withdraw outside your family",
                },
                status=status.HTTP_401_UNAUTHORIZED)

        request.data['account_to'] = accountTo.pk
        serializer = TransactionWriteSerializer(data=request.data)

        if serializer.is_valid():

            amount = serializer.validated_data['amount']

            createTransaction(serializer, accountFrom, accountTo, amount)

            # Notify the recipient
            user_from = User.objects.filter(account=accountFrom).first()

            if user_from is not None and user_from.expo_token != "" and user_from.expo_token is not None:
                send_push_message(
                    user_from.expo_token, request.user.firstname +
                    " withdraw " + str(amount) + " $ from your account")

            return Response({
                'balance': accountTo.balance,
                'status': status.HTTP_200_OK,
            })
        else:
            return Response(
                {
                    "status": status.HTTP_400_BAD_REQUEST,
                    'message': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
    def put(self, request, pk, format=None):

        task = Task.objects.filter(pk=pk).first()

        # Task doesnt exist
        if task is None:
            return Response(
                {
                    "status": status.HTTP_404_NOT_FOUND,
                    'message': "Not found",
                },
                status=status.HTTP_404_NOT_FOUND)

        group_user = GroupUser.objects.filter(group=task.group,
                                              user=request.user).first()

        # User not in the task group
        if group_user is None:
            return Response(
                {
                    "status": status.HTTP_401_UNAUTHORIZED,
                    'message': "You are not allowed to do this",
                },
                status=status.HTTP_401_UNAUTHORIZED)

        task_user = TaskUser.objects.filter(task=task).first()

        # Task already selected by another user
        if task_user is None:
            return Response(
                {
                    "status": status.HTTP_403_FORBIDDEN,
                    'message': "This task is not selected by you",
                },
                status=status.HTTP_403_FORBIDDEN)

        # Update the completed date
        task_user.date_completed = datetime.now()
        task_user.save()

        # Notify the parents
        users = User.objects.filter(groupuser__is_children=False,
                                    groupuser__group=task.group)
        for user in users:
            if user.expo_token != "":
                send_push_message(
                    user.expo_token, task.name + " was mark as Completed by " +
                    request.user.firstname)

        return Response({'status': status.HTTP_200_OK})
Ejemplo n.º 3
0
    def post(self, request, pk, format=None):
        group = Group.objects.filter(id=pk).first()

        # Group doesnt exist
        if group is None:
            return Response({
                'status': status.HTTP_404_NOT_FOUND,
                'message': "Not found",
            }, status=status.HTTP_404_NOT_FOUND)

        group_user = GroupUser.objects.filter(
            group=group, user=request.user).first()

        # User is not in this group?
        if group_user is None:
            return Response({
                'status': status.HTTP_403_FORBIDDEN,
                'message': "Can't do this",
            }, status=status.HTTP_403_FORBIDDEN)

        serializer = TaskWriteSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(group=group, user=request.user)

            # Notify the Children
            users = User.objects.filter(
                groupuser__is_children=True, groupuser__group=group)
            for user in users:
                if user.expo_token != "":
                    send_push_message(user.expo_token, serializer.data['name'] +
                                      " was added by " + request.user.firstname)

            return Response({
                'task': serializer.data,
                'status': status.HTTP_200_OK
            })

        return Response({
            "status": status.HTTP_400_BAD_REQUEST,
            'message': serializer.errors,
        }, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
    def post(self, request, format=None):
        serializer = TransactionWriteSerializer(data=request.data)

        accountFrom = Account.objects.filter(user=request.user).first()

        if serializer.is_valid():
            # Watch the balance first
            if accountFrom.balance < int(request.data['amount']):
                return Response(
                    {
                        "status": status.HTTP_400_BAD_REQUEST,
                        'message': "Not enough balance to do this",
                    },
                    status=status.HTTP_400_BAD_REQUEST)

            amount = serializer.validated_data['amount']

            accountTo = serializer.validated_data['account_to']

            createTransaction(serializer, accountFrom, accountTo, amount)

            # Notify the recipient
            user_to = User.objects.filter(account=accountTo).first()

            if user_to is not None and user_to.expo_token != "" and user_to.expo_token is not None:
                send_push_message(
                    user_to.expo_token,
                    request.user.firstname + " sent you " + str(amount) + " $")

            return Response({
                'balance': accountFrom.balance,
                'status': status.HTTP_200_OK,
            })
        else:
            return Response(
                {
                    "status": status.HTTP_400_BAD_REQUEST,
                    'message': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
    def put(self, request, pk, format=None):

        task = Task.objects.filter(pk=pk).first()

        # Task doesnt exist
        if task is None:
            return Response(
                {
                    "status": status.HTTP_404_NOT_FOUND,
                    'message': "Not found",
                },
                status=status.HTTP_404_NOT_FOUND)

        group_user = GroupUser.objects.filter(group=task.group,
                                              user=request.user).first()

        # User not in the task group
        if group_user is None:
            return Response(
                {
                    "status": status.HTTP_401_UNAUTHORIZED,
                    'message': "You are not allowed to do this",
                },
                status=status.HTTP_401_UNAUTHORIZED)

        task_user = TaskUser.objects.filter(task=task).first()

        # Task already selected by another user
        if task_user is None:
            return Response(
                {
                    "status": status.HTTP_400_BAD_REQUEST,
                    'message': "This task is not selected",
                },
                status=status.HTTP_400_BAD_REQUEST)

        # Task is not completed
        if not task_user.date_completed:
            return Response(
                {
                    "status": status.HTTP_400_BAD_REQUEST,
                    'message': "This task is not completed",
                },
                status=status.HTTP_400_BAD_REQUEST)

        # Set as validated
        task_user.date_validated = datetime.now()
        task_user.save()

        # Add the rewards to the children
        if task.reward > 0:
            user = User.objects.filter(pk=task_user.user.pk).first()
            if user is not None:
                user.rewards += task.reward
                user.save()

        # Mark the task as completed
        task.is_completed = True
        task.save()

        # Notify the Children
        users = User.objects.filter(groupuser__is_children=True,
                                    groupuser__group=task.group)
        for user in users:
            if user.expo_token != "":
                send_push_message(
                    user.expo_token,
                    task.name + " was validated by " + request.user.firstname +
                    ". You earned " + str(task.reward) + " point(s)")

        return Response({'status': status.HTTP_200_OK})