def delete(self, request):# pylint: disable=no-self-use """the method must accept the name of the file (string) of the image which is already used as a profile photo or icon """ key = request.body.decode() if not AwsService.check_if_in_bucket(key): if AwsService.del_photo(key): return RESPONSE_200_SUCCESS return RESPONSE_500_NO_SUCCESS return RESPONSE_404_NOT_FOUND
def get(self, request): """the method retrieves default icons from AWS S3 :param - request object """ tab = request.GET['tab'] if tab == 'fund': urls = AwsService.get_default_list_icons('standard_fund/')[1:] elif tab == 'income': urls = AwsService.get_default_list_icons('standard_income/')[1:] elif tab == 'spending': urls = AwsService.get_default_list_icons('standard/')[1:] elif tab == 'group': urls = AwsService.get_default_list_icons('standard_group/')[1:] return JsonResponse(urls, status=200, safe=False)
def groups_balance(request): """ Retrieving group`s budget information Args: request: request from the website Returns: Group-balance is dictionary which contains Total spendings, Total funds and Balance for every user`s group. """ user_id = request.user.id group_balance = {} users_groups = Group.filter_groups_by_user_id(user_id) for user_group in users_groups: group_balance[user_group.name] = { 'Total income': 0, 'Total spending': 0, 'Group icon': AwsService.get_image_url(user_group.icon), 'Group_id': user_group.id } income_values = filter_income_history_by_user_group(user_group) for i in income_values: group_balance[user_group.name]['Total income'] += i['value'] spend_values = filter_spending_history_by_user_group(user_group) for i in spend_values: group_balance[user_group.name]['Total spending'] += i['value'] group_balance[user_group.name]["Current balance"] = \ group_balance[user_group.name]['Total income'] - \ group_balance[user_group.name]['Total spending'] return JsonResponse(group_balance)
def show_fund_by_group(request): """Handling request for creating of spending categories list in group. Args: request (HttpRequest): Limitation data. Returns: HttpResponse object. """ user = request.user users_group = [] icon_if_none = AWS_S3_URL + STANDARD_FUNDS_FOLDER + ICON_FILE_NAME if user: for group in Group.filter_groups_by_user_id(user): for shared_fund in SharedFunds.objects.filter(group=group.id): if not FinancialGoal.has_goals(fund_id=shared_fund.fund.id) \ and shared_fund.fund.is_active: icon = FundCategories.objects.get( id=shared_fund.fund.id).icon url = AwsService.get_image_url( icon) if icon else icon_if_none users_group.append({ 'id_fund': shared_fund.fund.id, 'name_fund': shared_fund.fund.name, 'id_group': group.id, 'url': url }) return JsonResponse(users_group, status=200, safe=False) return JsonResponse({}, status=400)
def fund_summary(request): """ Handling request for getting summary info about fund. Args: request (HttpRequest) which consists fund_id Returns: JsonResponse object with summary info """ fund_id = json.loads(request.body)['fund_id'] fund = FundCategories.get_by_id(fund_id) fund_info = { 'icon': AwsService.get_image_url(fund.icon), 'name': fund.name } if fund.is_shared: fund_info['spend_group'] = SharedFunds.get_by_fund\ (fund_id).group.name spend_history = total_value_for_category\ (SpendingHistory.objects.filter(fund=fund_id), True) inc_history = total_value_for_category\ (IncomeHistory.objects.filter(fund=fund_id), True) fund_info['total'] = inc_history['total'] - spend_history['total'] last_value_info = total_category_validation(inc_history, spend_history) fund_info = {**last_value_info, **fund_info} return JsonResponse(fund_info)
def put(self, request):# pylint: disable=no-self-use """The method must accept the name of the file (string) of the image which is already used as a profile photo, and the new image which is passed in a form. The name property of the file which is passed is 'icon', the name of the file to be replaced is 'old', so in HTML form it must be set: <input type='file' name = 'icon'> """ request.method = 'POST' req_dict = request.POST old_file = req_dict['old'] new_file = request.FILES['icon'] if AwsService.upload(new_file): if AwsService.del_photo(old_file): if not AwsService.check_if_in_bucket(new_file): return RESPONSE_200_SUCCESS return RESPONSE_404_NOT_FOUND return RESPONSE_500_NO_SUCCESS
def post(self, request):# pylint: disable=no-self-use """The name property of the file which is passed is 'icon', so in HTML form it must be set: <input type='file' name = 'icon'> """ try: if request.FILES: pic = request.FILES['icon'] while not AwsService.check_if_in_bucket(pic): pic.name = AwsService.change_filename(pic) if AwsService.upload(pic): url = AwsService.get_image_url(pic) return HttpResponse(url, status=201) return RESPONSE_500_NO_SUCCESS return RESPONSE_400_UPLOAD_ERROR except datastructures.MultiValueDictKeyError: return RESPONSE_400_NO_FILE
def show_spending_group(request): """Handling request for creating of spending categories list in group. Args: request (HttpRequest): Limitation data. Returns: HttpResponse object. """ user = request.user users_group = [] icon_if_none = AWS_S3_URL + STANDARD_SPENDINGS_FOLDER + ICON_FILE_NAME if user: for group in Group.filter_groups_by_user_id(user): for shared_category in SharedSpendingCategories.objects.filter( group=group.id): if shared_category.spending_categories.is_active: icon = \ SpendingCategories.objects.get( id=shared_category.spending_categories.id).icon # pylint: disable=line-too-long url = AwsService.get_image_url( icon) if icon else icon_if_none users_group.append({ 'id_cat': shared_category.spending_categories.id, 'name_cat': shared_category.spending_categories.name, 'id_group': group.id, 'name_group': group.name, 'url': url }) return JsonResponse(users_group, status=200, safe=False) return JsonResponse({}, status=400)
def show_spending_ind(request): """Handling request for creating of spending categories list. Args: request (HttpRequest): Limitation data. Returns: HttpResponse object. """ user = request.user icon_if_none = AWS_S3_URL + STANDARD_SPENDINGS_FOLDER + ICON_FILE_NAME if user: user_categories = [] for entry in SpendingCategories.filter_by_user(user): url = AwsService.get_image_url( entry.icon) if entry.icon else icon_if_none user_categories.append({ 'id': entry.id, 'name': entry.name, 'url': url }) return JsonResponse( { 'categories': user_categories, 'fixed': user.ind_period_fixed }, status=200, safe=False) return JsonResponse({}, status=400)
def income_summary(request): """ Handling request for getting summary info about income category. Args: request (HttpRequest) which consists income_id Returns: JsonResponse object with summary info """ income_id = json.loads(request.body)['income_id'] income = IncomeCategories.get_by_id(income_id) income_info = { 'icon': AwsService.get_image_url(income.icon), 'name': income.name } history = IncomeHistory.objects.filter(income=income_id) income_info = {**total_value_for_category(history, True), **income_info} return JsonResponse(income_info)
def spending_summary(request): """ Handling request for getting summary info about spending category. Args: request (HttpRequest) which consists spending_id Returns: JsonResponse object with summary info """ spend_id = json.loads(request.body)['spend_id'] spend = SpendingCategories.get_by_id(spend_id) spend_info = { 'icon': AwsService.get_image_url(spend.icon), 'name': spend.name } if spend.is_shared: spend_info['spend_group'] = SharedSpendingCategories.get_by_spending_id\ (spend_id).group.name history = SpendingHistory.objects.filter(spending_categories_id=spend_id) spend_info = {**total_value_for_category(history, True), **spend_info} return JsonResponse(spend_info)
def get_by_group(request): """Handling request for creating of group list. Args: request (HttpRequest): request from server which ask some data. Returns: HttpResponse object. """ user = request.user icon_if_none = AWS_S3_URL + STANDARD_GROUPS_FOLDER + ICON_FILE_NAME if user: user_groups = [] for entry in Group.filter_groups_by_user_id(user): url = AwsService.get_image_url( entry.icon) if entry.icon else icon_if_none user_groups.append({ 'id': entry.id, 'name': entry.name, 'url': url }) return JsonResponse(user_groups, status=200, safe=False) return JsonResponse({}, status=400)
def show_fund(request): """Handling request for creating of spending categories list. Args: request (HttpRequest): request from server which ask some data. Returns: HttpResponse object. """ user = request.user icon_if_none = AWS_S3_URL + STANDARD_FUNDS_FOLDER + ICON_FILE_NAME if user: user_funds = [] for entry in FundCategories.filter_by_user(user): url = AwsService.get_image_url( entry.icon) if entry.icon else icon_if_none if not FinancialGoal.has_goals(fund_id=entry.id): user_funds.append({ 'id': entry.id, 'name': entry.name, 'url': url }) return JsonResponse(user_funds, status=200, safe=False) return JsonResponse({}, status=400)