Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migate Run Validators page to react #10652

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,75 +33,19 @@ def new_results
@results_validator.validate(@competition.id)
end

def check_results
with_results_validator
end

def check_competition_results
with_results_validator do
@competition = competition_from_params
@result_validation.competition_ids = @competition.id
end
end

def compute_validation_competitions
validation_form = ResultValidationForm.new(
competition_start_date: params[:start_date],
competition_end_date: params[:end_date],
competition_selection: ResultValidationForm::COMP_VALIDATION_ALL,
)

render json: {
competitions: validation_form.competitions,
}
end

def with_results_validator
@result_validation = ResultValidationForm.new(
competition_ids: params[:competition_ids] || "",
competition_start_date: params[:competition_start_date] || "",
competition_end_date: params[:competition_end_date] || "",
validator_classes: params[:validator_classes] || ResultValidationForm::ALL_VALIDATOR_NAMES.join(","),
competition_selection: params[:competition_selection] || ResultValidationForm::COMP_VALIDATION_MANUAL,
apply_fixes: params[:apply_fixes] || false,
)

# For this view, we just build an empty validator: the WRT will decide what
# to actually run (by default all validators will be selected).
@results_validator = ResultsValidators::CompetitionsResultsValidator.new(check_real_results: true)
yield if block_given?
end

def do_check_results
running_validators do
render :check_results
end
end

def do_check_competition_results
running_validators do
uniq_id = @result_validation.competitions.first
@competition = Competition.find(uniq_id)

render :check_competition_results
end
end

def running_validators
action_params = params.require(:result_validation_form)
.permit(:competition_ids, :validator_classes, :apply_fixes, :competition_selection, :competition_start_date, :competition_end_date)

@result_validation = ResultValidationForm.new(action_params)

if @result_validation.valid?
@results_validator = @result_validation.build_and_run
else
@results_validator = ResultsValidators::CompetitionsResultsValidator.new(check_real_results: true)
end

yield if block_given?
end

def clear_results_submission
# Just clear the "results_submitted_at" field to let the Delegate submit
# the results again. We don't actually want to clear InboxResult and InboxPerson.
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/api/v0/competitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def competition_index
include: serial_includes
end

def competition_count
start_date = params.require(:startDate)
end_date = params.require(:endDate)

count = Competition.where("start_date >= ? AND end_date <= ?", start_date, end_date).count
render json: { count: count }
end

def show
competition = competition_from_params

Expand Down
42 changes: 42 additions & 0 deletions app/controllers/panel_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,48 @@ def generate_db_token
}
end

private def validators_for_competition_ids(competition_ids)
validators = params.require(:selectedValidators).split(',').map(&:constantize)
apply_fix_when_possible = params.require(:applyFixWhenPossible)

results_validator = ResultsValidators::CompetitionsResultsValidator.new(
validators,
check_real_results: true,
apply_fixes: apply_fix_when_possible,
)
results_validator.validate(competition_ids)
Comment on lines +54 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As useful as constantize is, you gotta be careful with when and how you use it because it can potentially lead to vulnerabilities. In this context, I'm wondering: Why didn't you use the existing result_validation_form.rb that is also used in the previous running_validators hook?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't use the result_validation_form.rb because it was having too many things. I agree those were good, but I feel that if I forcefully use that model, it may not be so clean. So just took the above two lines from that.

I just now saw couple of validations from the model which are also relevant, I've now implemented those validations in the client side.

I understand the risk of constantize, I now see how the result_validation_form is doing it - I have now changed to a similar logic, so no more use of constantize.

Also, now I think technically result_validation_form.rb is not needed anymore as it was used to support the rails ERB code, and that code is no longer there. So I've removed result_validation_form.rb after making necessary changes.

render json: {
has_results: results_validator.has_results?,
validators: results_validator.validators,
infos: results_validator.infos,
errors: results_validator.errors,
warnings: results_validator.warnings,
}
end

private def competition_ids_in_range(range)
start_date = range[:startDate]
end_date = range[:endDate]
Competition.over
.not_cancelled
.where(start_date: start_date..)
.where(start_date: ..end_date)
.order(:start_date)
.ids
end

def validators_for_competition_list
competition_ids = params.require(:competitionIds).split(',')
validators_for_competition_ids(competition_ids)
end

def validators_for_competitions_in_range
range = JSON.parse(params.require(:competitionRange)).transform_keys(&:to_sym)
competition_ids = competition_ids_in_range(range)

validators_for_competition_ids(competition_ids)
end

def panel_page
panel_page_id = params.require(:id)
panel_with_panel_page = current_user.panels_with_access&.find { |panel| User.panel_list[panel][:pages].include?(panel_page_id) }
Expand Down
128 changes: 0 additions & 128 deletions app/models/result_validation_form.rb

This file was deleted.

Loading
Loading