支持用户的删除

This commit is contained in:
virusdefender 2019-03-11 11:43:15 +08:00
parent 6ef16c8253
commit 1af50c0f4c

View File

@ -121,25 +121,15 @@ class UserAdminAPI(APIView):
Q(email__icontains=keyword)) Q(email__icontains=keyword))
return self.success(self.paginate_data(request, user, UserAdminSerializer)) return self.success(self.paginate_data(request, user, UserAdminSerializer))
def delete_one(self, user_id):
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
return f"User {user_id} does not exist"
if Submission.objects.filter(user_id=user_id).exists():
return f"Can't delete the user {user_id} as he/she has submissions"
user.delete()
@super_admin_required @super_admin_required
def delete(self, request): def delete(self, request):
id = request.GET.get("id") id = request.GET.get("id")
if not id: if not id:
return self.error("Invalid Parameter, id is required") return self.error("Invalid Parameter, id is required")
for user_id in id.split(","): ids = id.split(",")
if user_id: if str(request.user.id) in ids:
error = self.delete_one(user_id) return self.error("Current user can not be deleted")
if error: User.objects.filter(id__in=ids).delete()
return self.error(error)
return self.success() return self.success()