<commit_before>package controllers.admin

import play.api.mvc.Controller
import play.api.mvc.Action
import brainflight.security.Secured
import views.html
import models.User
import models.Role
import models.Task
import play.api.libs.json.Json
import models.TaskSelectionAlgorithm

object TaskHandling extends Controller with Secured {
  // TODO remove comment in production
  // override val DefaultAccessRole = Role( "admin" )

  def testAlgorithm = Authenticated(parser = parse.urlFormEncoded){ implicit request =>
    request.body.get("code").flatMap(_.headOption) match {
      case Some(code) =>
        if (TaskSelectionAlgorithm.isValidAlgorithm(code))
          Ok
        else
          BadRequest("Invalid code")
      case _ =>
        BadRequest("Missing parameters.")
    }
  }

  def index = Authenticated { implicit request =>
    Ok(html.admin.taskSelectionAlgorithm(request.user, TaskSelectionAlgorithm.findAll))
  }

  def submitAlgorithm = Authenticated(parser = parse.urlFormEncoded) { implicit request =>
    (for {
      code <- request.body.get("code").flatMap(_.headOption)
      use <- request.body.get("use").flatMap(_.headOption)
    } yield {
      if (TaskSelectionAlgorithm.isValidAlgorithm(code)) {
        val alg = TaskSelectionAlgorithm(code)
        TaskSelectionAlgorithm.insert(alg)
        if (use == "1")
          TaskSelectionAlgorithm.use(alg)
        Ok
      } else
        BadRequest("Invalid task selection algorithm.")
    }) getOrElse BadRequest("Missing parameters.")

  }

  def useAlgorithm(id: String) = Authenticated { implicit request =>
    TaskSelectionAlgorithm.findOneById(id) match {
      case Some(alg) => 
        TaskSelectionAlgorithm.use(alg)
        Ok
      case _ =>
        BadRequest("Algorithm not found.")
    }
  }

  def listTasks = Authenticated { implicit request =>
    Ok(Json.toJson(Task.findAll))
  }

  def listAlgorithms = Authenticated { implicit request =>
    Ok(Json.toJson(TaskSelectionAlgorithm.findAll))
  }<commit_msg>OX-148 changed http result on failure to 422<commit_after>package controllers.admin

import play.api.mvc.Controller
import play.api.mvc.Action
import brainflight.security.Secured
import views.html
import models.User
import models.Role
import models.Task
import play.api.libs.json.Json
import models.TaskSelectionAlgorithm

object TaskHandling extends Controller with Secured {
  // TODO remove comment in production
  // override val DefaultAccessRole = Role( "admin" )

  def testAlgorithm = Authenticated(parser = parse.urlFormEncoded){ implicit request =>
    request.body.get("code").flatMap(_.headOption) match {
      case Some(code) =>
        if (TaskSelectionAlgorithm.isValidAlgorithm(code))
          Ok
        else
          BadRequest("Invalid code")
      case _ =>
        BadRequest("Missing parameters.")
    }
  }

  def index = Authenticated { implicit request =>
    Ok(html.admin.taskSelectionAlgorithm(request.user, TaskSelectionAlgorithm.findAll))
  }

  def submitAlgorithm = Authenticated(parser = parse.urlFormEncoded) { implicit request =>
    (for {
      code <- request.body.get("code").flatMap(_.headOption)
      use <- request.body.get("use").flatMap(_.headOption)
    } yield {
      if (TaskSelectionAlgorithm.isValidAlgorithm(code)) {
        val alg = TaskSelectionAlgorithm(code)
        TaskSelectionAlgorithm.insert(alg)
        if (use == "1")
          TaskSelectionAlgorithm.use(alg)
        Ok
      } else
        (new Status(422))("Invalid task selection algorithm.")
    }) getOrElse BadRequest("Missing parameters.")

  }

  def useAlgorithm(id: String) = Authenticated { implicit request =>
    TaskSelectionAlgorithm.findOneById(id) match {
      case Some(alg) => 
        TaskSelectionAlgorithm.use(alg)
        Ok
      case _ =>
        BadRequest("Algorithm not found.")
    }
  }

  def listTasks = Authenticated { implicit request =>
    Ok(Json.toJson(Task.findAll))
  }

  def listAlgorithms = Authenticated { implicit request =>
    Ok(Json.toJson(TaskSelectionAlgorithm.findAll))
  }
{'fraction_non_alphanumeric': 0.1029121421520237, 'fraction_numerical': 0.0027147087857847976, 'mean_word_length': 3.2663157894736843, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7646199', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1098, 'n_words': 250}