Skip to content

Commit b986b30

Browse files
authored
Add submission processing limit for users (#591)
* Implemented a check in the submissions controller to limit users to a maximum of 3 processing submissions for a specific exercise. * Added a method in the User model to count processing submissions for an exercise. * Introduced a corresponding method in the Submission model to facilitate this count.
1 parent 997431d commit b986b30

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

app/controllers/api/v8/core/exercises/submissions_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def create
5454
return respond_forbidden('Submissions for this exercise are no longer accepted.')
5555
end
5656

57+
# Check if user has too many processing submissions for this exercise
58+
processing_count = current_user.processing_submissions_count_for_exercise(@exercise.name, @course.id)
59+
if processing_count >= 3
60+
authorization_skip!
61+
return respond_forbidden("Too many submissions currently processing for this exercise. You have #{processing_count} submissions being processed. Please wait for them to complete before submitting again. All your submissions will eventually get processed and you will get your points.")
62+
end
63+
5764
file_contents = File.read(params[:submission][:file].tempfile.path)
5865

5966
errormsg = nil

app/models/submission.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ def self.unprocessed_count
116116
unprocessed.count
117117
end
118118

119+
# Count how many submissions a user has processing for a specific exercise
120+
def self.processing_count_for_user_and_exercise(user_id, exercise_name, course_id)
121+
where(
122+
user_id: user_id,
123+
exercise_name: exercise_name,
124+
course_id: course_id,
125+
processed: false
126+
).count
127+
end
128+
119129
# How many times at most should a submission be (successfully) sent to a sandbox
120130
def self.max_attempts_at_processing
121131
3

app/models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ def self.search(query)
280280
User.where('lower(email) LIKE ?', "%#{query.strip.downcase}%")
281281
end
282282

283+
# Count how many submissions this user has processing for a specific exercise
284+
def processing_submissions_count_for_exercise(exercise_name, course_id)
285+
Submission.processing_count_for_user_and_exercise(id, exercise_name, course_id)
286+
end
287+
283288
private
284289
def course_ids_arel
285290
courses = Course.arel_table

0 commit comments

Comments
 (0)