def test_save_base_usage_when_there_is_no_by_warehouse(self): cost = 100.0 forecast_cost = 200.0 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) usage_type = factory.UsageTypeFactory( is_manually_type=True, usage_type='BU', ) self.client.post( '/scrooge/rest/allocationadmin/{0}/{1}/baseusages/save'.format( self.date.year, self.date.month, ), { "rows": [{ 'cost': cost, 'forecast_cost': forecast_cost, 'type': { 'id': '{0}'.format(usage_type.id), 'name': '{0}'.format(usage_type.name), } }] }, format='json' ) usage_price = models.UsagePrice.objects.all()[0] self.assertEquals(usage_price.cost, cost) self.assertEquals(usage_price.forecast_cost, forecast_cost) self.assertEquals(usage_price.start, first_day) self.assertEquals(usage_price.end, last_day) self.assertEquals(usage_price.type, usage_type) self.assertEquals(usage_price.warehouse, None)
def get(self, request, month, year, format=None): first_day, last_day, days_in_month = get_dates(year, month) base_usages = self._get_base_usages(first_day, last_day) team_costs = self._get_team_costs(first_day, last_day) dynamic_extra_costs = self._get_dynamic_extra_costs( first_day, last_day, ) extra_costs = self._get_extra_costs(first_day, last_day) return Response({ 'baseusages': { 'name': 'Base Usages', 'rows': base_usages, 'template': 'tabbaseusages.html', }, 'teamcosts': { 'name': 'Team Costs', 'rows': team_costs, 'template': 'tabteamcosts.html', }, 'dynamicextracosts': { 'name': 'Dynamic Extra Costs', 'rows': dynamic_extra_costs, 'template': 'tabdynamicextracosts.html', }, 'extracosts': { 'name': 'Extra Costs', 'rows': extra_costs, 'template': 'tabextracostsadmin.html', }, })
def get(self, request, year, month, service, env, *args, **kwargs): first_day, last_day, days_in_month = get_dates(year, month) response = { "serviceExtraCost": { "name": "Extra Cost", "template": "tabextracosts.html", "rows": self._get_service_extra_cost( service, env, first_day, last_day, ), }, } try: response.update({ "serviceDivision": { "name": "Service Division", "template": "taballocationclientdivision.html", "rows": self._get_service_divison( service, year, month, first_day, ), } }) except CannotDetermineValidServiceUsageTypeError: pass return Response(response)
def post(self, request, year, month, team, *args, **kwargs): post_data = json.loads(request.raw_post_data) first_day, last_day, days_in_month = get_dates(year, month) try: team = Team.objects.get(id=team) except Team.DoesNotExist: return {'status': False, 'message': 'Team Does Not Exist.'} TeamServiceEnvironmentPercent.objects.filter( team_cost__team=team, team_cost__start=first_day, team_cost__end=last_day, ).delete() team_cost = TeamCost.objects.get_or_create( team=team, start=first_day, end=last_day, )[0] for row in post_data['rows']: service_environment = ServiceEnvironment.objects.get( service__id=row.get('service'), environment__id=row.get('env')) tsep = TeamServiceEnvironmentPercent.objects.get_or_create( team_cost=team_cost, service_environment=service_environment, defaults=dict(percent=row.get('value'), ))[0] tsep.percent = row.get('value') tsep.save() return Response({"status": True})
def post(self, request, year, month, team, *args, **kwargs): post_data = json.loads(request.raw_post_data) first_day, last_day, days_in_month = get_dates(year, month) try: team = Team.objects.get(id=team) except Team.DoesNotExist: return {'status': False, 'message': 'Team Does Not Exist.'} TeamServiceEnvironmentPercent.objects.filter( team_cost__team=team, team_cost__start=first_day, team_cost__end=last_day, ).delete() team_cost = TeamCost.objects.get_or_create( team=team, start=first_day, end=last_day, )[0] for row in post_data['rows']: service_environment = ServiceEnvironment.objects.get( service__id=row.get('service'), environment__id=row.get('env') ) tsep = TeamServiceEnvironmentPercent.objects.get_or_create( team_cost=team_cost, service_environment=service_environment, defaults=dict( percent=row.get('value'), ) )[0] tsep.percent = row.get('value') tsep.save() return Response({"status": True})
def get(self, request, year, month, team, format=None, *args, **kwargs): first_day, last_day, days_in_month = get_dates(year, month) return Response({ "teamDivision": { "name": "Team Division", "template": "taballocationclientdivision.html", "rows": self._get_team_divison(team, first_day, last_day), } })
def test_get_extra_cost_when_there_is_one_additional_type_and_cost(self): cost = 100.0 forecast_cost = 200.0 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) extra_cost_type = factory.ExtraCostTypeFactory() service_environment = factory.ServiceEnvironmentFactory() extra_cost = factory.ExtraCostFactory( extra_cost_type=extra_cost_type, start=first_day, end=last_day, cost=cost, forecast_cost=forecast_cost, service_environment=service_environment, ) response = self.client.get( '/scrooge/rest/allocationadmin/{0}/{1}/'.format( self.date.year, self.date.month, ) ) self.assertEquals( json.loads(response.content)['extracosts'], { 'name': 'Extra Costs', 'rows': [{ 'extra_cost_type': { 'id': 1, 'name': 'Other' }, 'extra_costs': [{}] }, { 'extra_cost_type': { 'id': 2, 'name': 'Support' }, 'extra_costs': [{}] }, { 'extra_cost_type': { 'id': extra_cost_type.id, 'name': extra_cost_type.name }, 'extra_costs': [{ 'id': extra_cost.id, 'cost': extra_cost.cost, 'forecast_cost': extra_cost.forecast_cost, 'service': extra_cost.service_environment.service_id, 'env': extra_cost.service_environment.environment_id }] }], 'template': 'tabextracostsadmin.html' } )
def post(self, request, year, month, allocate_type, *args, **kwargs): post_data = json.loads(request.raw_post_data) first_day, last_day, days_in_month = get_dates(year, month) if allocate_type == 'baseusages': self._save_base_usages(first_day, last_day, post_data) if allocate_type == 'extracosts': self._save_extra_costs(first_day, last_day, post_data) if allocate_type == 'dynamicextracosts': self._save_dynamic_extra_costs(first_day, last_day, post_data) if allocate_type == 'teamcosts': self._save_team_costs(first_day, last_day, post_data) return Response({"status": True})
def post(self, request, year, month, service, env, *args, **kwargs): post_data = json.loads(request.raw_post_data) first_day, last_day, days_in_month = get_dates(year, month) if kwargs.get('allocate_type') == 'servicedivision': service_usage_type = self._get_service_usage_type( service, year, month, create_if_not_exist=True) self._clear_daily_usages( service_usage_type.usage_type, first_day, last_day, ) for row in post_data['rows']: service_environment = ServiceEnvironment.objects.get( service__id=row['service'], environment__id=row['env']) for day in xrange(days_in_month): iter_date = first_day + timedelta(days=day) dpo = (service_environment.dummy_pricing_object. get_daily_pricing_object(iter_date)) DailyUsage.objects.create( date=iter_date, service_environment=service_environment, daily_pricing_object=dpo, value=row['value'], type=service_usage_type.usage_type, ) if kwargs.get('allocate_type') == 'serviceextracost': service_environment = ServiceEnvironment.objects.get( service__id=service, environment__id=env, ) other_type = ExtraCostType.objects.get(id=1) ids = set() for row in post_data['rows']: ids.add(row.get('id')) extra_cost = ExtraCost.objects.get_or_create( id=row.get('id'), start=first_day, end=last_day, service_environment=service_environment, extra_cost_type=other_type, defaults=dict(cost=row['value'], remarks=row.get('remarks', '')))[0] extra_cost.cost = row['value'] extra_cost.remarks = row.get('remarks', '') extra_cost.save() ExtraCost.objects.filter( start=first_day, end=last_day, service_environment=service_environment, extra_cost_type=other_type, ).exclude(id__in=ids).delete() return Response({"status": True})
def test_get_base_usage_when_there_is_one_usage_price_by_warehouse(self): cost = 100.0 forecast_cost = 200.0 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) warehouse = factory.WarehouseFactory(show_in_report=True) usage_type = factory.UsageTypeFactory( is_manually_type=True, usage_type='BU', by_warehouse=True, ) factory.UsagePriceFactory( type=usage_type, start=first_day, end=last_day, cost=cost, forecast_cost=forecast_cost, warehouse=warehouse, ) response = self.client.get( '/scrooge/rest/allocationadmin/{0}/{1}/'.format( self.date.year, self.date.month, ) ) self.assertEquals( json.loads(response.content)['baseusages'], { "rows": [{ 'cost': cost, 'forecast_cost': forecast_cost, 'type': { 'id': usage_type.id, 'name': '{0}'.format(usage_type), }, "warehouse": { "id": warehouse.id, "name": warehouse.name, } }], "name": "Base Usages", "template": "tabbaseusages.html", } )
def test_update_extra_cost_when_everything_is_ok(self): cost = 100.0 forecast_cost = 200.0 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) extra_cost_type = factory.ExtraCostTypeFactory() service_environment = factory.ServiceEnvironmentFactory() extra_cost = models.ExtraCost.objects.create( cost=50.0, forecast_cost=50.0, service_environment=service_environment, extra_cost_type=extra_cost_type, start=first_day, end=last_day, ) self.client.post( '/scrooge/rest/allocationadmin/{0}/{1}/extracosts/save'.format( self.date.year, self.date.month, ), { 'rows': [{ 'extra_cost_type': { 'id': extra_cost_type.id, 'name': extra_cost_type.name }, 'extra_costs': [{ 'id': extra_cost.id, 'cost': cost, 'forecast_cost': forecast_cost, 'service': service_environment.service_id, 'env': service_environment.environment_id, }] }] }, format='json' ) extra_cost = models.ExtraCost.objects.all()[0] self.assertEquals(extra_cost.cost, cost) self.assertEquals(extra_cost.forecast_cost, forecast_cost)
def test_get_team_cost_when_there_is_one_team_cost(self): cost = 100.0 forecast_cost = 200.0 members = 4 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) team = factory.TeamFactory() factory.TeamCostFactory( team=team, start=first_day, end=last_day, cost=cost, forecast_cost=forecast_cost, members_count=members ) response = self.client.get( '/scrooge/rest/allocationadmin/{0}/{1}/'.format( self.date.year, self.date.month, ) ) self.assertEquals( json.loads(response.content)['teamcosts'], { "rows": [{ 'team': { 'id': team.id, 'name': team.name, }, 'cost': cost, 'forecast_cost': forecast_cost, 'members': members, }], "name": "Team Costs", "template": "tabteamcosts.html", } )
def test_save_dynamic_extra_cost(self): cost = 100.0 forecast_cost = 200.0 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) dynamic_extra_cost_type = factory.DynamicExtraCostTypeFactory() self.client.post( ('/scrooge/rest/allocationadmin/{0}/{1}/' 'dynamicextracosts/save').format( self.date.year, self.date.month, ), { "rows": [{ 'dynamic_extra_cost_type': { 'id': dynamic_extra_cost_type.id, 'name': dynamic_extra_cost_type.name, }, 'cost': cost, 'forecast_cost': forecast_cost, }], "name": "Dynamic Extra Costs", "template": "tabdynamicextracosts.html", }, format='json' ) dynamic_extra_cost = models.DynamicExtraCost.objects.all()[0] self.assertEquals(dynamic_extra_cost.cost, cost) self.assertEquals(dynamic_extra_cost.forecast_cost, forecast_cost) self.assertEquals(dynamic_extra_cost.start, first_day) self.assertEquals(dynamic_extra_cost.end, last_day) self.assertEquals( dynamic_extra_cost.dynamic_extra_cost_type, dynamic_extra_cost_type, )
def test_get_dynamic_extra_cost_when_there_is_one_type_and_cost(self): cost = 100.0 forecast_cost = 200.0 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) dynamic_extra_cost_type = factory.DynamicExtraCostTypeFactory() factory.DynamicExtraCostFactory( dynamic_extra_cost_type=dynamic_extra_cost_type, forecast_cost=forecast_cost, cost=cost, start=first_day, end=last_day, ) response = self.client.get( '/scrooge/rest/allocationadmin/{0}/{1}/'.format( self.date.year, self.date.month, ) ) self.assertEquals( json.loads(response.content)['dynamicextracosts'], { "rows": [{ 'dynamic_extra_cost_type': { 'id': dynamic_extra_cost_type.id, 'name': dynamic_extra_cost_type.name, }, 'cost': cost, 'forecast_cost': forecast_cost, }], "name": "Dynamic Extra Costs", "template": "tabdynamicextracosts.html", } )
def test_save_team_costs(self): cost = 100.0 forecast_cost = 200.0 members = 4 first_day, last_day, days_in_month = get_dates( self.date.year, self.date.month, ) team = factory.TeamFactory() self.client.post( '/scrooge/rest/allocationadmin/{0}/{1}/teamcosts/save'.format( self.date.year, self.date.month, ), { "rows": [{ 'team': { 'id': team.id, 'name': team.name, }, 'cost': cost, 'forecast_cost': forecast_cost, 'members': members, }], }, format='json' ) team_cost = models.TeamCost.objects.all()[0] self.assertEquals(team_cost.cost, cost) self.assertEquals(team_cost.forecast_cost, forecast_cost) self.assertEquals(team_cost.members_count, members) self.assertEquals(team_cost.start, first_day) self.assertEquals(team_cost.end, last_day) self.assertEquals(team_cost.team, team)
def get(self, request, service, env, month, year, format=None): """ Collecting costs and format it for angular view. :param service int: service id :param env int: environment id :param month int: ordinal value of month :param year int: year :returns object: json response """ first_day, last_day, days_in_month = get_dates(year, month) try: service_environment = ServiceEnvironment.objects.get( service__id=service, environment_id=env, ) except ServiceEnvironment.DoesNotExist: return Response( { "status": False, "message": _( "Service {0} or environment {1} " "does not exist".format(service, env) ) }, status=HTTP_404_NOT_FOUND ) forecast = request.QUERY_PARAMS.get('forecast', False) dates = CostDateStatus.objects.filter( date__gte=first_day, date__lte=last_day, **{'forecast_accepted' if forecast else 'accepted': True} ).values_list('date', flat=True) if len(dates) == 0: return Response({ "status": False, "message": _( 'There are no accepted costs for chosen date.' ' Please choose different date or back later.' ) }) monthly_costs = DailyCost.objects.filter( date__in=dates, service_environment=service_environment, forecast=forecast, ).values( 'type' ).annotate( sum_cost=Sum('cost') ) base_usages = {bu.id: bu.name for bu in BaseUsage.objects.all()} total = D(0) results = [] for monthly_cost in monthly_costs: results.append({ 'name': base_usages[monthly_cost['type']], 'cost': round(monthly_cost['sum_cost'], 2) }) total += monthly_cost['sum_cost'] results.append({ 'name': _('Total'), 'cost': round(total, 2), }) return Response({ "status": True, "results": results })
def post(self, request, year, month, service, env, *args, **kwargs): post_data = json.loads(request.raw_post_data) first_day, last_day, days_in_month = get_dates(year, month) if kwargs.get('allocate_type') == 'servicedivision': service_usage_type = self._get_service_usage_type( service, year, month, create_if_not_exist=True ) self._clear_daily_usages( service_usage_type.usage_type, first_day, last_day, ) for row in post_data['rows']: service_environment = ServiceEnvironment.objects.get( service__id=row['service'], environment__id=row['env'] ) for day in xrange(days_in_month): iter_date = first_day + timedelta(days=day) dpo = ( service_environment.dummy_pricing_object .get_daily_pricing_object(iter_date) ) DailyUsage.objects.create( date=iter_date, service_environment=service_environment, daily_pricing_object=dpo, value=row['value'], type=service_usage_type.usage_type, ) if kwargs.get('allocate_type') == 'serviceextracost': service_environment = ServiceEnvironment.objects.get( service__id=service, environment__id=env, ) other_type = ExtraCostType.objects.get(id=1) ids = set() for row in post_data['rows']: ids.add(row.get('id')) extra_cost = ExtraCost.objects.get_or_create( id=row.get('id'), start=first_day, end=last_day, service_environment=service_environment, extra_cost_type=other_type, defaults=dict( cost=row['value'], remarks=row.get('remarks', '') ) )[0] extra_cost.cost = row['value'] extra_cost.remarks = row.get('remarks', '') extra_cost.save() ExtraCost.objects.filter( start=first_day, end=last_day, service_environment=service_environment, extra_cost_type=other_type, ).exclude(id__in=ids).delete() return Response({"status": True})
def get(self, request, service, env, month, year, format=None): """ Collecting costs and format it for angular view. :param service int: service id :param env int: environment id :param month int: ordinal value of month :param year int: year :returns object: json response """ first_day, last_day, days_in_month = get_dates(year, month) try: service_environment = ServiceEnvironment.objects.get( service__id=service, environment_id=env, ) except ServiceEnvironment.DoesNotExist: return Response( { "status": False, "message": _("Service {0} or environment {1} " "does not exist".format(service, env)) }, status=HTTP_404_NOT_FOUND) forecast = request.QUERY_PARAMS.get('forecast', False) dates = CostDateStatus.objects.filter( date__gte=first_day, date__lte=last_day, **{ 'forecast_accepted' if forecast else 'accepted': True }).values_list('date', flat=True) if len(dates) == 0: return Response({ "status": False, "message": _('There are no accepted costs for chosen date.' ' Please choose different date or back later.') }) monthly_costs = DailyCost.objects.filter( date__in=dates, service_environment=service_environment, forecast=forecast, ).values('type').annotate(sum_cost=Sum('cost')) base_usages = {bu.id: bu.name for bu in BaseUsage.objects.all()} total = D(0) results = [] for monthly_cost in monthly_costs: results.append({ 'name': base_usages[monthly_cost['type']], 'cost': round(monthly_cost['sum_cost'], 2) }) total += monthly_cost['sum_cost'] results.append({ 'name': _('Total'), 'cost': round(total, 2), }) return Response({"status": True, "results": results})