Ejemplo n.º 1
0
def show_users_group_data(request):
    """Handling request for creating of user's groups list.
    Args:
        request (HttpRequest): request from server which ask list of groups data for user.
    Returns:
        HttpResponse object.
    """
    user = request.user
    if user:
        groups = []
        user_role = None
        for item in groups_for_user(user):
            group = Group.get_group_by_id(item)
            count = users_email_for_group(group.id)
            if group.owner == user:
                user_role = "Owner"
            else:
                if is_user_admin_group(item, user):
                    user_role = "Admin"
                else:
                    user_role = "Member"
            groups.append({
                'id': item,
                'user_role': user_role,
                'group_name': group.name,
                'count': len(count)
            })
        return JsonResponse(groups, status=200, safe=False)
    return JsonResponse({}, status=400)
Ejemplo n.º 2
0
def change_users_role_in_group(request):
    """Handling request for updating user's role in group.
    Args:
        request (HttpRequest): request from server which contain
        email, is_admin and group_id
    Returns:
        HttpResponse object.
    """
    data = json.loads(request.body)
    user_email = data["user_email"]
    group_id = data["group_id"]
    is_admin = data["is_admin"]
    user_to_change = UserProfile.get_by_email(user_email)
    is_admin = True if is_admin == 'Admin' else False
    user = request.user
    if user:
        if not is_user_admin_group(group_id, user):
            return HttpResponse(status=409)
        if not is_user_in_group(group_id, user_to_change.id):
            return HttpResponse(status=406)
        group = UsersInGroups.group_data_for_user_by_group_id(
            group_id, user_to_change)
        group.is_admin = is_admin
        try:
            group.save()
        except (ValueError, AttributeError):
            return HttpResponse(status=400)
    return HttpResponse(status=200)
Ejemplo n.º 3
0
def add_shared_fund_to_group(request):
    """Handling request for adding new shared SpendingCategories to group.
    Args:
        request (HttpRequest): request from server which contain
        shared_spending and group_id
    Returns:
        HttpResponse object.
    """
    user = request.user
    data = json.loads(request.body)
    shared_category = data["shared_fund"]
    group_id = data["group_id"]
    fund = FundCategories.get_by_id(shared_category)
    group = Group.get_group_by_id(group_id)
    if not is_valid_data_shared_fund_to_group(data):
        return HttpResponse(status=400)
    if not is_user_admin_group(group_id, user) or not fund.owner == user:
        return HttpResponse(status=409)
    if not fund and not group:
        return HttpResponse(status=406)
    if SharedFunds.get_by_fund(fund):
        return HttpResponse(status=409)
    new_shared_fund = SharedFunds(group=group, fund=fund)
    fund.is_shared = True
    try:
        new_shared_fund.save()
        fund.save()
    except (AttributeError, ValueError):
        return HttpResponse(status=400)
    return HttpResponse(status=201)
Ejemplo n.º 4
0
def add_new_users_to_group(request):
    """Handling request for adding new user to group.
   Args:
       request (HttpRequest): request from server which contain
           email, group and is_admin(True or False)
   Returns:
       HttpResponse object.
   """
    data = json.loads(request.body)
    user = request.user
    if not is_valid_data_add_user_to_group(data):
        return HttpResponse(status=400)
    user_add = UserProfile.get_by_email(data["users_email"])
    is_admin = data["is_admin"]
    group = Group.get_group_by_id(data["group_id"])
    if not is_user_admin_group(group.id, user):
        return HttpResponse(status=403)
    if is_user_in_group(group, user_add):
        return HttpResponse(status=409)
    if not user_add and not group:
        return HttpResponse(status=406)
    if user:
        new_user = UsersInGroups(user=user_add, group=group, is_admin=is_admin)
        try:
            new_user.save()
        except (AttributeError, ValueError):
            return HttpResponse(status=400)
    return HttpResponse(status=201)
Ejemplo n.º 5
0
def show_users_in_group(request):
    """Handling request for creating of group's users list.
    Args:
        request (HttpRequest): request from server which ask list of users for group.
    Returns:
        HttpResponse object.
    """
    users_in_group = []
    user = request.user
    user_role = None
    if user:
        for item in groups_for_user(user):
            group = Group.get_group_by_id(item)
            for user in users_email_for_group(group.id):
                if group.owner == user:
                    user_role = "Owner"
                else:
                    if is_user_admin_group(item, user):
                        user_role = "Admin"
                    else:
                        user_role = "Member"
                users_in_group.append({
                    'email': user.email,
                    'user_role': user_role,
                    'group_id': group.id
                })
        return JsonResponse(users_in_group, status=200, safe=False)
    return HttpResponse(status=400)
Ejemplo n.º 6
0
def create_new_goal(request):
    """Handling request for creating of new goal category.
    Args:
        request (HttpRequest): request from server which contain
            value, start_date, finish_date, shred_group, name, icon
    Returns:
        HttpResponse object.
    """
    data = json.loads(request.body)
    if not input_fund_registration_validate(data):
        return HttpResponse(status=400)
    if not date_range_validate(data):
        return HttpResponse(status=400)
    user = request.user
    shared_group = data["shared_group"]
    is_shared = False
    if shared_group:
        is_shared = True
        group = Group.get_group_by_id(shared_group)
        if not group:
            return HttpResponse(status=400)
        if not is_user_admin_group(group.id, user):
            return HttpResponse(status=406)
    if save_new_goal(value=Decimal(data["value"]),
                     start_date=data["start_date"],
                     finish_date=data["finish_date"],
                     name=data["name"],
                     icon=data["icon"],
                     is_shared=is_shared,
                     owner=user,
                     shared_group=shared_group):
        return HttpResponse(status=201)
    return HttpResponse(status=409)
Ejemplo n.º 7
0
def delete_group(request, group_id):
    """Handling request for delete group.
        Args:
            request (HttpRequest): Data for delete group.
            group_id: Group Id
        Returns:
            HttpResponse object.
    """

    user = request.user
    if user:
        group = Group.get_group_by_id(group_id)
        if not group:
            return HttpResponse(status=406)
        if not is_user_admin_group(group.id, user):
            return HttpResponse(status=400)
        group.is_active = False
        try:
            group.save()
        except (ValueError, AttributeError):
            return HttpResponse(status=400)
    return HttpResponse(f"You've just deleted group: {group.name}", status=200)
Ejemplo n.º 8
0
def create_spending_history_for_admin(user, start_date, finish_date,
                                      utc_difference):
    """Creating array of spending history data for admin.
        Args:
            user (UserProfile): user who needs information.
            start_date (date): The beginning of statistic period
            finish_date (date): The end of statistic period
        Returns:
            Array of spending history data for admin.
    """
    history_for_admin = []
    groups_for_admin = [
        group for group in groups_for_user(user)
        if is_user_admin_group(group, user)
    ]

    for group in groups_for_admin:

        for entry in SharedSpendingCategories.filter_by_group(group=group):

            history_spending_category = []

            for person in UsersInGroups.filter_by_group(group=group):
                history_person = []
                if is_user_member_group(group, person):
                    for item in SpendingHistory.filter_by_user_date_spending(
                            person, start_date, finish_date, entry):
                        history_person.append({
                            'member':
                            person.email,
                            'value':
                            float(item.value),
                            'date': (item.date +
                                     timedelta(hours=utc_difference)).date(),
                            'fund':
                            'Individual fund',
                            'Delete':
                            item.id
                        })
                else:
                    for item in SpendingHistory.filter_by_user_date_spending(
                            person, start_date, finish_date, entry):
                        fund_entry = item.fund.name \
                            if item.fund in SharedFunds.filter_by_group(group=group) \
                            else 'Individual fund'
                        history_person.append({
                            'member':
                            person.email,
                            'value':
                            float(item.value),
                            'date': (item.date +
                                     timedelta(hours=utc_difference)).date(),
                            'fund':
                            fund_entry,
                            'Delete':
                            item.id
                        })

                if history_person:
                    history_spending_category.extend(history_person)

            if history_spending_category:
                history_for_admin.append({
                    'spending':
                    entry.name + ' / ' + Group.get_group_by_id(group).name,
                    'history':
                    history_spending_category
                })
    return history_for_admin