前言
Django在开发管理系统的过程中,给客户用的前台配合hAdmin很快就搞好了。默认后台平时自己用,也没那么多讲究。这次要给别人用。就需要考虑权限,还有一些样式啥的。需要花点心思美化一下。
过程
限制后台的一些功能,记录只能由前端上传,禁止后台编辑和修改。加载自定义js,通过js提交数据
admin.py
代码语言:javascript复制编辑项目目录下的admin.py
class FundRecordAdmin(admin.ModelAdmin):
#后台展示字段
list_display = ['id', 'show_card_transid', 'show_card_number', 'record_type', 'amount', 'currency', 'pos_amount', 'pos_currency', 'status', 'auth_at', 'operate']
readonly_fields = [field.name for field in FundRecord._meta.fields]
#搜索字段
search_fields=['card__card_number', 'card__card_transid']
#禁用删除
def has_delete_permission(self, request, obj=None):
return False
#禁止后台新增
def has_add_permission(self, request):
return False
#卡片标识
@admin.display(description='transid', ordering='id')
def show_card_transid(self, obj):
return obj.card.card_transid
#卡号
@admin.display(description='卡号', ordering='id')
def show_card_number(self, obj):
return obj.card.card_number
# 定义一些操作示例
@admin.display(description='操作', ordering='id')
def operate(self, obj):
if obj.status == 0:
#拒绝按钮
raras = "'{}','{}'".format(obj.id, '2')
refuse_btn = "拒绝"
#通过
aaras = "'{}','{}'".format(obj.id, '1')
allow_btn = "通过"
html_str = "" allow_btn refuse_btn ""
return format_html(html_str, r_a=raras, a_a=aaras)
else:
return ""
class Media:
js = ('js/fundrecord.js',)
#注册
admin.site.register(FundRecord, FundRecordAdmin)
fundrecord.js
代码语言:javascript复制调用js,给自定义按钮。然后通过接口传递数据。
//卡片额度处理函数
//fundrecordmanage函数传入申请aid和sta两个参数
//fid 申请ID
//sta 1已完成,2未通过
function fundrecordmanage(fid, sta){
var url = "/record/fundman/?fid=" fid "&sta=" sta;
var Http = new XMLHttpRequest();
Http.open("GET", url, false);
Http.send(null);
//console.log(Http.responseText);
}
view.py
代码语言:javascript复制定义供js使用的接口。包括给卡片充值,提现。接口只允许管理员使用,验证登录。采用事物保证数据的一致性。用messages提示。
@user_passes_test(lambda u: u.is_superuser)
@login_required
@transaction.atomic
def fundrecord_manage(request):
"""
卡片额度变更处理函数
"""
app_id = request.GET.get('fid')
status = request.GET.get('sta')
fundrecord = FundRecord.objects.filter(id=app_id,status=0).first()
if fundrecord:
if status == '1':
#卡片充值
if fundrecord.record_type == 1:
try:
with transaction.atomic():
#修改记录状态
fundrecord.status = int(status)
fundrecord.save()
fundrecord.card.limit_amount = fundrecord.amount
fundrecord.card.remaining_amount = fundrecord.amount
fundrecord.card.note=""
fundrecord.card.save()
#通知
messages.success(request, '通过')
except Exception as e:
logger.debug(e)
#卡片提现
elif fundrecord.record_type == 3:
try:
with transaction.atomic():
#修改记录状态
fundrecord.status = int(status)
fundrecord.save()
#卡片说明
fundrecord.card.note=""
fundrecord.card.save()
#账号余额变动
# 创建一条退款记录,将预扣款返回账号
balance_record_a = BalanceRecord.objects.create(
owner=fundrecord.card.owner,
record_type=2,
amount=fundrecord.amount,
status=1,
)
fundrecord.card.owner.userprofile.usd_amount = fundrecord.amount
fundrecord.card.owner.userprofile.save()
#扣除提现手续费
service_fee = fundrecord.amount*Decimal(0.01)
if service_fee < 1:
service_fee = Decimal(1)
#扣除手续费
balance_record_b = BalanceRecord.objects.create(
owner=fundrecord.card.owner,
record_type=8,
amount=service_fee,
status=1,
)
#账号余额变动
balance_record_b.owner.userprofile.usd_amount -= service_fee
balance_record_b.owner.userprofile.save()
#通知
messages.success(request, '通过')
except Exception as e:
logger.debug(e)
elif status == '2':
#卡片充值
if fundrecord.record_type == 1:
try:
with transaction.atomic():
# 创建一条退款记录,将预扣款返回账号
balance_record_a = BalanceRecord.objects.create(
owner=fundrecord.card.owner,
record_type=2,
amount=fundrecord.amount,
status=1,
)
fundrecord.card.owner.userprofile.usd_amount = fundrecord.amount
fundrecord.card.owner.userprofile.save()
#修改记录状态
fundrecord.status = int(status)
fundrecord.save()
#卡片说明
fundrecord.card.note=""
fundrecord.card.save()
#通知
messages.warning(request, '未通过')
except Exception as e:
logger.debug(e)
#卡片提现
elif fundrecord.record_type == 3:
try:
with transaction.atomic():
#额度恢复
fundrecord.card.limit_amount = fundrecord.amount
fundrecord.card.remaining_amount = fundrecord.amount
#清理锁定
fundrecord.card.note=""
fundrecord.card.save()
#改变本条记录状态
fundrecord.status = int(status)
fundrecord.save()
except Exception as e:
logger.debug(e)
return HttpResponse('ok')