def old_update_season_revenue(request): stock_ids = StockId.objects.all() for stockid in stock_ids: symbol = stockid.symbol statements = SeasonIncomeStatement.objects.filter(symbol=symbol).order_by('date') if statements: for statement in statements: season_revenue = SeasonRevenue.objects.filter(symbol=symbol, year=statement.year, season=statement.season) if not season_revenue: if statement.season == 1: if statements.filter(year=statement.year-1, season=4): last_season_statement = statements.get(year=statement.year-1, season=4) else: last_season_statement = None else: if statements.filter(year=statement.year, season=statement.season-1): last_season_statement = statements.get(year=statement.year, season=statement.season-1) else: last_season_statement = None if statements.filter(year=statement.year-1, season=statement.season): last_year_statement = statements.get(year=statement.year-1, season=statement.season) else: last_year_statement = None revenue = SeasonRevenue() revenue.surrogate_key = symbol + "_" + str(statement.year) + str(statement.season).zfill(2) revenue.year = statement.year revenue.season = statement.season if revenue.season == 1: revenue.date = datetime.date(revenue.year, 1, 1) elif revenue.season == 2: revenue.date = datetime.date(revenue.year, 4, 1) elif revenue.season == 3: revenue.date = datetime.date(revenue.year, 7, 1) elif revenue.season == 4: revenue.date = datetime.date(revenue.year, 10, 1) revenue.symbol = symbol revenue.revenue = statement.operating_revenue if last_season_statement: if last_season_statement.operating_revenue > 0: revenue.season_growth_rate = Decimal(((revenue.revenue / last_season_statement.operating_revenue) - 1) * 100) if last_year_statement: revenue.last_year_revenue = Decimal(last_year_statement.operating_revenue) if revenue.last_year_revenue > 0: revenue.year_growth_rate = Decimal(((revenue.revenue / revenue.last_year_revenue) - 1) * 100) revenue.acc_revenue = SeasonIncomeStatement.objects.filter(symbol=symbol, year=statement.year, season__lte=statement.season).aggregate(Sum('operating_revenue'))['operating_revenue__sum'] #revenue.acc_revenue = SeasonIncomeStatement.objects.filter(symbol=symbol, year=statement.year, season__lte=statement.season) if last_year_statement: last_acc_revenue = SeasonIncomeStatement.objects.filter(symbol=symbol, year=statement.year-1, season__lte=statement.season).aggregate(Sum('operating_revenue'))['operating_revenue__sum'] #print revenue.acc_revenue #print last_acc_revenue if last_acc_revenue > 0: revenue.acc_year_growth_rate = Decimal(((revenue.acc_revenue / last_acc_revenue) - 1) * 100) revenue.save() print symbol + ' season revenue updated' return HttpResponse('update season revenue')
def old_update_season_revenue(request): stock_ids = StockId.objects.all() for stockid in stock_ids: symbol = stockid.symbol statements = SeasonIncomeStatement.objects.filter( symbol=symbol).order_by('date') if statements: for statement in statements: season_revenue = SeasonRevenue.objects.filter( symbol=symbol, year=statement.year, season=statement.season) if not season_revenue: if statement.season == 1: if statements.filter(year=statement.year - 1, season=4): last_season_statement = statements.get( year=statement.year - 1, season=4) else: last_season_statement = None else: if statements.filter(year=statement.year, season=statement.season - 1): last_season_statement = statements.get( year=statement.year, season=statement.season - 1) else: last_season_statement = None if statements.filter(year=statement.year - 1, season=statement.season): last_year_statement = statements.get( year=statement.year - 1, season=statement.season) else: last_year_statement = None revenue = SeasonRevenue() revenue.surrogate_key = symbol + "_" + str( statement.year) + str(statement.season).zfill(2) revenue.year = statement.year revenue.season = statement.season if revenue.season == 1: revenue.date = datetime.date(revenue.year, 1, 1) elif revenue.season == 2: revenue.date = datetime.date(revenue.year, 4, 1) elif revenue.season == 3: revenue.date = datetime.date(revenue.year, 7, 1) elif revenue.season == 4: revenue.date = datetime.date(revenue.year, 10, 1) revenue.symbol = symbol revenue.revenue = statement.operating_revenue if last_season_statement: if last_season_statement.operating_revenue > 0: revenue.season_growth_rate = Decimal( ((revenue.revenue / last_season_statement.operating_revenue) - 1) * 100) if last_year_statement: revenue.last_year_revenue = Decimal( last_year_statement.operating_revenue) if revenue.last_year_revenue > 0: revenue.year_growth_rate = Decimal(( (revenue.revenue / revenue.last_year_revenue) - 1) * 100) revenue.acc_revenue = SeasonIncomeStatement.objects.filter( symbol=symbol, year=statement.year, season__lte=statement.season).aggregate( Sum('operating_revenue'))['operating_revenue__sum'] #revenue.acc_revenue = SeasonIncomeStatement.objects.filter(symbol=symbol, year=statement.year, season__lte=statement.season) if last_year_statement: last_acc_revenue = SeasonIncomeStatement.objects.filter( symbol=symbol, year=statement.year - 1, season__lte=statement.season).aggregate( Sum('operating_revenue') )['operating_revenue__sum'] #print revenue.acc_revenue #print last_acc_revenue if last_acc_revenue > 0: revenue.acc_year_growth_rate = Decimal(( (revenue.acc_revenue / last_acc_revenue) - 1) * 100) revenue.save() print symbol + ' season revenue updated' return HttpResponse('update season revenue')
def update_season_revenue(request): if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") if season == 1: startMonth = 1 elif season == 2: startMonth = 4 elif season == 3: startMonth = 7 elif season == 4: startMonth = 10 else: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") firtMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth).values_list('symbol', flat=True) secondMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth+1).values_list('symbol', flat=True) thirdMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth+2).values_list('symbol', flat=True) firstMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth) secondMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth+1) thirdMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth+2) date = datetime.date(year, startMonth, 1) lastYear, lastSeason = last_season(date) lastSeasonRevenues = SeasonRevenue.objects.filter(year=lastYear, season=lastSeason) symbols = list(set(firtMonthStockIds).intersection(set(secondMonthStockIds)).intersection(set(thirdMonthStockIds))) for symbol in symbols: print symbol revenue = SeasonRevenue() revenue.surrogate_key = symbol + '_' + str(year) + str(season).zfill(2) revenue.year = year revenue.season = season revenue.date = date revenue.symbol = symbol try: revenue.revenue = firstMonthRevenues.get(symbol=symbol).revenue +\ secondMonthRevenues.get(symbol=symbol).revenue +\ thirdMonthRevenues.get(symbol=symbol).revenue revenue.last_year_revenue = firstMonthRevenues.get(symbol=symbol).last_year_revenue +\ secondMonthRevenues.get(symbol=symbol).last_year_revenue +\ thirdMonthRevenues.get(symbol=symbol).last_year_revenue if revenue.last_year_revenue > 0: revenue.year_growth_rate = revenue.revenue / revenue.last_year_revenue * 100 - 100 if lastSeasonRevenues.filter(symbol=symbol): last_season_revenue = lastSeasonRevenues.get(symbol=symbol).revenue if last_season_revenue > 0: revenue.season_growth_rate = revenue.revenue / last_season_revenue * 100 - 100 revenue.acc_revenue = thirdMonthRevenues.get(symbol=symbol).acc_revenue revenue.acc_year_growth_rate = thirdMonthRevenues.get(symbol=symbol).acc_year_growth_rate revenue.save() except: pass cnt = SeasonRevenue.objects.filter(year=year, season=season).count() lastDate = SeasonRevenue.objects.all().aggregate(Max('date'))['date__max'] if lastDate == None: json_obj = json.dumps({"notes": "There is no data in SeasonRevenue"}) return HttpResponse(json_obj, content_type="application/json") lastDateDataCnt = SeasonRevenue.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name = "sr", last_update_date = datetime.date.today(), last_data_date = lastDate, notes="There is " + str(lastDateDataCnt) + " datas") updateManagement.save() json_obj = json.dumps({"name": updateManagement.name, "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": lastDate.strftime("%y-%m-%d"), "notes": "Update " + str(cnt) + " season revenue on " + str(year) + "-" + str(season)}) return HttpResponse(json_obj, content_type="application/json")
def update_season_revenue(request): if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") if season == 1: startMonth = 1 elif season == 2: startMonth = 4 elif season == 3: startMonth = 7 elif season == 4: startMonth = 10 else: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") firtMonthStockIds = MonthRevenue.objects.filter( year=year, month=startMonth).values_list('symbol', flat=True) secondMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth + 1).values_list('symbol', flat=True) thirdMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth + 2).values_list('symbol', flat=True) firstMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth) secondMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth + 1) thirdMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth + 2) date = datetime.date(year, startMonth, 1) lastYear, lastSeason = last_season(date) lastSeasonRevenues = SeasonRevenue.objects.filter(year=lastYear, season=lastSeason) symbols = list( set(firtMonthStockIds).intersection( set(secondMonthStockIds)).intersection(set(thirdMonthStockIds))) for symbol in symbols: print symbol revenue = SeasonRevenue() revenue.surrogate_key = symbol + '_' + str(year) + str(season).zfill(2) revenue.year = year revenue.season = season revenue.date = date revenue.symbol = symbol try: revenue.revenue = firstMonthRevenues.get(symbol=symbol).revenue +\ secondMonthRevenues.get(symbol=symbol).revenue +\ thirdMonthRevenues.get(symbol=symbol).revenue revenue.last_year_revenue = firstMonthRevenues.get(symbol=symbol).last_year_revenue +\ secondMonthRevenues.get(symbol=symbol).last_year_revenue +\ thirdMonthRevenues.get(symbol=symbol).last_year_revenue if revenue.last_year_revenue > 0: revenue.year_growth_rate = revenue.revenue / revenue.last_year_revenue * 100 - 100 if lastSeasonRevenues.filter(symbol=symbol): last_season_revenue = lastSeasonRevenues.get( symbol=symbol).revenue if last_season_revenue > 0: revenue.season_growth_rate = revenue.revenue / last_season_revenue * 100 - 100 revenue.acc_revenue = thirdMonthRevenues.get( symbol=symbol).acc_revenue revenue.acc_year_growth_rate = thirdMonthRevenues.get( symbol=symbol).acc_year_growth_rate revenue.save() except: pass cnt = SeasonRevenue.objects.filter(year=year, season=season).count() lastDate = SeasonRevenue.objects.all().aggregate(Max('date'))['date__max'] if lastDate == None: json_obj = json.dumps({"notes": "There is no data in SeasonRevenue"}) return HttpResponse(json_obj, content_type="application/json") lastDateDataCnt = SeasonRevenue.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name="sr", last_update_date=datetime.date.today(), last_data_date=lastDate, notes="There is " + str(lastDateDataCnt) + " datas") updateManagement.save() json_obj = json.dumps({ "name": updateManagement.name, "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": lastDate.strftime("%y-%m-%d"), "notes": "Update " + str(cnt) + " season revenue on " + str(year) + "-" + str(season) }) return HttpResponse(json_obj, content_type="application/json")