@permission_required('users.add_userban')
def ban_user(request, username):
 User = get_user_model()
 user = get_object_or_404(User, username=username)
 if (request.method == 'POST'):
  form = UserBanForm(data=request.POST)
  if form.is_valid():
   ban = UserBan(user=user, by=request.user, reason=form.cleaned_data['reason'], is_active=True)
   ban.save()
   return redirect(user)
 else:
  if user.active_ban:
   return redirect(user)
  form = UserBanForm()
 try:
  common_reasons = json.loads(config.COMMON_REASONS_TO_BAN_USERS)
 except (TypeError, ValueError):
  common_reasons = ['Spam']
 else:
  if (not common_reasons):
   common_reasons = ['Spam']
 return render(request, 'users/ban_user.html', {'form': form, 'detail_user': user, 'common_reasons': common_reasons})

@ permission_required ( 'users.add_userban' ) def ban_user ( request , username ) : User = get_user_model ( ) user = get_object_or_404 ( User , username = username ) if ( request . method == 'POST' ) : form = UserBanForm ( data = request . POST ) if form . is_valid ( ) : ban = UserBan ( user = user , by = request . user , reason = form . cleaned_data [ 'reason' ] , is_active = True ) ban . save ( ) return redirect ( user ) else : if user . active_ban : return redirect ( user ) form = UserBanForm ( ) try : common_reasons = json . loads ( config . COMMON_REASONS_TO_BAN_USERS ) except ( TypeError , ValueError ) : common_reasons = [ 'Spam' ] else : if ( not common_reasons ) : common_reasons = [ 'Spam' ] return render ( request , 'users/ban_user.html' , { 'form' : form , 'detail_user' : user , 'common_reasons' : common_reasons } )
ban a user .