基于全局变量值django中间件更新模型
update model based on global variable value django middleware
is_paid_member=False #Global variable
函数:
global is_paid_member
if is_paid_member == True:
with transaction.atomic():
client = ClientProfile.objects.get(user=request.user)
client.subscription = "Member"
client.save(update_fields=['subscription'])
我想根据全局值更新模型 variable.This 如果付款是 made.Where 函数必须更新模型 made.Where 我是否放置此函数以更新模型字段?
我在交易成功函数中将 is_paid_member 更改为 True
@csrf_exempt
def payment_success_member(request):
data = {}
global is_paid_member
is_paid_member=True
user=request.user
return render(request, 'payu_success.html',data)
这是支付功能:
@login_required(login_url='/account/login/')
def MemberPay(request):
data = {}
global is_paid_member
if is_paid_member == True:
with transaction.atomic():
client = ClientProfile.objects.get(user=request.user)
client.subscription = "Member"
client.save(update_fields=['subscription'])
txnid = get_transaction_id()
hash_ = generate_hash_member(request, txnid)
hash_string = get_hash_string_member(request, txnid)
# use constants file to store constant values.
# use test URL for testing
data["action"] = constants.PAYMENT_URL_TEST
amounts = Amount.objects.filter(subscription="Member")
data["amount"] =amounts[0].amount
data["productinfo"] = constants.PAID_FEE_PRODUCT_INFO
data["key"] = config.KEY
data["txnid"] = txnid
data["hash"] = hash_
data["hash_string"] = hash_string
data["firstname"] = request.user.username
data["email"] = request.user.email
data["subscription"] = "Free"
if (request.user.is_client == True):
data["phone"] = request.user.client_profile.mobile_number
else:
data["phone"] = request.user.trainer_profile.mobile_number
data["service_provider"] = constants.SERVICE_PROVIDER
data["furl"] =
request.build_absolute_uri(reverse("student:payment_failure_member"))
data["surl"] =
request.build_absolute_uri(reverse("student:payment_success_member"))
if request.user.client_profile.subscription=="Fellow":
data["subscription"]="Fellow"
if request.user.client_profile.subscription=="Member":
data["subscription"]="Member"
global is_paid_member
print("PAID: ",is_paid_member)
print(data["subscription"])
if is_paid_member==True:
with transaction.atomic():
client= ClientProfile.objects.get(user=request.user)
client.subscription="Member"
client.save(update_fields=['subscription'])
return render(request, "payu_payment_member.html", data)
在您的模型中创建一个 Boolean field
。
class Payment(models.Model):
....
....
is_paid_member = models.BooleanField(default=False)
....
现在,在您的回调函数中 payment_success_member()
使用 txn_id 获取支付模型对象并更新 is_paid_member
字段:
def payment_success_member(request):
.....
txn_id = request.data['txn_id']
payment_obj = Payment.object.get(txn_id=txn_id)
payment_obj.is_paid_member =True
payment_obj.save()
.....
is_paid_member=False #Global variable
函数:
global is_paid_member
if is_paid_member == True:
with transaction.atomic():
client = ClientProfile.objects.get(user=request.user)
client.subscription = "Member"
client.save(update_fields=['subscription'])
我想根据全局值更新模型 variable.This 如果付款是 made.Where 函数必须更新模型 made.Where 我是否放置此函数以更新模型字段?
我在交易成功函数中将 is_paid_member 更改为 True
@csrf_exempt
def payment_success_member(request):
data = {}
global is_paid_member
is_paid_member=True
user=request.user
return render(request, 'payu_success.html',data)
这是支付功能:
@login_required(login_url='/account/login/')
def MemberPay(request):
data = {}
global is_paid_member
if is_paid_member == True:
with transaction.atomic():
client = ClientProfile.objects.get(user=request.user)
client.subscription = "Member"
client.save(update_fields=['subscription'])
txnid = get_transaction_id()
hash_ = generate_hash_member(request, txnid)
hash_string = get_hash_string_member(request, txnid)
# use constants file to store constant values.
# use test URL for testing
data["action"] = constants.PAYMENT_URL_TEST
amounts = Amount.objects.filter(subscription="Member")
data["amount"] =amounts[0].amount
data["productinfo"] = constants.PAID_FEE_PRODUCT_INFO
data["key"] = config.KEY
data["txnid"] = txnid
data["hash"] = hash_
data["hash_string"] = hash_string
data["firstname"] = request.user.username
data["email"] = request.user.email
data["subscription"] = "Free"
if (request.user.is_client == True):
data["phone"] = request.user.client_profile.mobile_number
else:
data["phone"] = request.user.trainer_profile.mobile_number
data["service_provider"] = constants.SERVICE_PROVIDER
data["furl"] =
request.build_absolute_uri(reverse("student:payment_failure_member"))
data["surl"] =
request.build_absolute_uri(reverse("student:payment_success_member"))
if request.user.client_profile.subscription=="Fellow":
data["subscription"]="Fellow"
if request.user.client_profile.subscription=="Member":
data["subscription"]="Member"
global is_paid_member
print("PAID: ",is_paid_member)
print(data["subscription"])
if is_paid_member==True:
with transaction.atomic():
client= ClientProfile.objects.get(user=request.user)
client.subscription="Member"
client.save(update_fields=['subscription'])
return render(request, "payu_payment_member.html", data)
在您的模型中创建一个 Boolean field
。
class Payment(models.Model):
....
....
is_paid_member = models.BooleanField(default=False)
....
现在,在您的回调函数中 payment_success_member()
使用 txn_id 获取支付模型对象并更新 is_paid_member
字段:
def payment_success_member(request):
.....
txn_id = request.data['txn_id']
payment_obj = Payment.object.get(txn_id=txn_id)
payment_obj.is_paid_member =True
payment_obj.save()
.....