exit_person = User.objects.get(username=user)
userbool = User.objects.filter(username=user).exists()
if not userbool:
return JsonResponse({'msg': '输入的用户在系统中不存在,请检查后重新输入!'})
sql = "SELECT DISTINCT gtrevuser_id FROM `gerrit_reviewer_user` where user_id=%s" % int(exit_person.id)
cursor.execute(sql)
info = cursor.fetchall()
info_i = [int(list(i)[0]) for i in info]
re_list = []
for j in info_i:
sql1 = "SELECT *,COUNT(*) as count from (SELECT * from gerrit_reviewer_user where gtrevuser_id=%d) as a GROUP BY type_user " % j
cursor.execute(sql1)
info1 = cursor.fetchall()
info11 = [list(k) for k in info1]
if info11[0][4]==1 and info11[1][4]==1:
cursor.close()
db.close()
return JsonResponse({
'msg': "reviewers或watchers管理员只有一个,故只能替换不可删除!"
})
elif (info11[0][4]!=1 and (info11[1][4]==1 and int(info11[1][2])==int(exit_person.id))) or ((info11[0][4]==1 and int(info11[0][2])==int(exit_person.id)) and info11[1][4]!=1):
cursor.close()
db.close()
return JsonResponse({
'msg': "reviewers或watchers管理员只有一个,故只能替换不可删除!"
})
else:
Gerrit_Reviewer_User.objects.filter(user_id=int(exit_person.id)).delete()
cursor.close()
db.close()
return JsonResponse({
'msg': "删除成功!"
})
如果外键gtrevuser_id的type_user reviewers或者watchers 只有一个则只可替换不可删除 分为三种情况 reviewers和watchers 都为一那肯定只能替换 watchers不为一(大于2)reviewers为一,并且判断reviewers这条记录是否为传入的用户,若是则不可删除 watchers为一reviewers不为一(大于2),并且判断watchers这条记录是否为传入的用户,若是则不可删除
上面三种条件都不满足则说明传入的这个用户所在的gtrevuser_id的revuewers和watchers管理者都大于2则可删除
这个需求花了挺长时间,特此记录一下
|