def validate(self, data): for k in ('source_region', 'dest_region'): v = data[k] if v not in RegionInfo.region_names(): raise serializers.ValidationError( "{0} filed value {1} is not in region_list".format(k, v)) return data
def check_region(self): region = self.request.GET.get('region', None) if region is not None: if region in RegionInfo.region_names(): if region == 'aws-bj-1' and self.tenant.region != 'aws-bj-1': raise Http404 self.response_region = region else: raise Http404 try: t_region, created = TenantRegionInfo.objects.get_or_create( tenant_id=self.tenant.tenant_id, region_name=self.response_region) self.tenant_region = t_region except Exception, e: logger.error(e)
def post(self, request, *args, **kwargs): result = {} try: tenant_id = self.tenant.tenant_id region_name = request.POST["region_name"] buy_memory = request.POST["buy_memory"] buy_disk = request.POST["buy_disk"] buy_net = 0 buy_period = request.POST["buy_period"] action = request.POST["action"] regions = RegionInfo.region_names() period = int(buy_period) pay_model = "month" if period > 0 and regions.index(region_name) >= 0: feerule = settings.REGION_FEE_RULE[region_name] logger.debug(feerule) one1 = float(feerule["memory_money"]) * int(buy_memory) one2 = float(feerule["disk_money"]) * int(buy_disk) one3 = float(feerule["net_money"]) * int(buy_net) onehour = one1 + one2 buy_money = 0 tmp_perod = period if period >= 12: pay_model = "year" buy_money = onehour * 24 * period * 1.5 * 30 + one3 tmp_perod = period / 12 else: buy_money = onehour * 24 * period * 2 * 30 + one3 needTotalMoney = round(buy_money, 2) logger.debug(needTotalMoney) tenant = Tenants.objects.get(tenant_id=tenant_id) if tenant.balance > needTotalMoney: tenant.balance = tenant.balance - Decimal(needTotalMoney) tenant.save() logger.debug(tenant_id + "cost money" + str(needTotalMoney)) TenantConsume(tenant_id=tenant_id, total_memory=int(buy_memory) * int(buy_period), cost_money=needTotalMoney, payed_money=needTotalMoney, pay_status='payed').save() statTime = datetime.datetime.now() + datetime.timedelta( hours=1) endTime = datetime.datetime.now() + relativedelta( months=int(buy_period)) if action == "append": trpms = TenantRegionPayModel.objects.filter( tenant_id=tenant_id).order_by('-ID')[:1] if len(trpms) > 0: trpm = trpms[0] statTime = trpm.buy_end_time + datetime.timedelta( hours=1) endTime = trpm.buy_end_time + relativedelta( months=int(buy_period)) start_time = statTime.strftime("%Y-%m-%d %H:00:00") end_time = endTime.strftime("%Y-%m-%d %H:00:00") data = {} data["tenant_id"] = tenant_id data["region_name"] = region_name data["pay_model"] = pay_model data["buy_period"] = tmp_perod data["buy_memory"] = buy_memory data["buy_disk"] = buy_disk data["buy_net"] = buy_net data["buy_start_time"] = start_time data["buy_end_time"] = end_time data["buy_money"] = buy_money data["create_time"] = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") TenantRegionPayModel(**data).save() result["status"] = "success" monitorhook.buyPayModelMonitor(self.tenant, self.user, 'buy_pay_model') else: result["status"] = "nomoney" else: result["status"] = "par" except Exception as e: logger.exception(e) result["status"] = "failure" return JsonResponse(result, status=200)