<commit_before>package controllers.admin;

import braingames.mvc.Controller
import brainflight.security.Secured
import models.security.Role
import views._
import models.task.Project
import play.api.data.Form._
import play.api.data.Form
import play.api.data.Forms._
import models.user.User
import play.api.i18n.Messages

object ProjectAdministration extends Controller with Secured {

  override val DefaultAccessRole = Role.Admin

  val projectForm = Form(tuple(
    "projectName" -> nonEmptyText(1, 100).verifying("project.nameAlreadyInUse", name => Project.findOneByName(name).isEmpty),
    "owner" -> nonEmptyText(1, 100).verifying("user.notFound", userId => User.findOneById(userId).isDefined)))

  def list = Authenticated { implicit request =>
    Ok(html.admin.project.projectList(Project.findAll, projectForm, User.findAll))
  }

  def delete(projectName: String) = Authenticated { implicit reuqest =>
    for{
      project <- Project.findOneByName(projectName) ?~ Messages("project.notFound")
    } yield {
      Project.remove(project)
      JsonOk(Messages("project.removed"))
    }
  }

  def create = Authenticated(parser = parse.urlFormEncoded) { implicit request =>
    projectForm.bindFromRequest.fold(
      formWithErrors => BadRequest(html.admin.project.projectList(Project.findAll, formWithErrors, User.findAll)),
      {
        case (name, ownerId) =>
          for {
            owner <- User.findOneById(ownerId) ?~ Messages("user.notFound")
          } yield {
            Project.insertOne(Project(name, owner._id))
            Redirect(routes.ProjectAdministration.list).flashing(
              FlashSuccess(Messages("project.createSuccess")))
          }
      })
  }<commit_msg>OX-413 reordered user list in project creation<commit_after>package controllers.admin;

import braingames.mvc.Controller
import brainflight.security.Secured
import models.security.Role
import views._
import models.task.Project
import play.api.data.Form._
import play.api.data.Form
import play.api.data.Forms._
import models.user.User
import play.api.i18n.Messages

object ProjectAdministration extends Controller with Secured {

  override val DefaultAccessRole = Role.Admin

  val projectForm = Form(tuple(
    "projectName" -> nonEmptyText(1, 100)
      .verifying("project.nameAlreadyInUse", name => Project.findOneByName(name).isEmpty),
    "owner" -> nonEmptyText(1, 100)
      .verifying("user.notFound", userId => User.findOneById(userId).isDefined)))

  def list = Authenticated { implicit request =>
    Ok(html.admin.project.projectList(
      Project.findAll,
      projectForm.fill("", request.user.id),
      User.findAll.sortBy(_.name)))
  }

  def delete(projectName: String) = Authenticated { implicit reuqest =>
    for {
      project <- Project.findOneByName(projectName) ?~ Messages("project.notFound")
    } yield {
      Project.remove(project)
      JsonOk(Messages("project.removed"))
    }
  }

  def create = Authenticated(parser = parse.urlFormEncoded) { implicit request =>
    projectForm.bindFromRequest.fold(
      formWithErrors => BadRequest(html.admin.project.projectList(Project.findAll, formWithErrors, User.findAll)),
      {
        case (name, ownerId) =>
          for {
            owner <- User.findOneById(ownerId) ?~ Messages("user.notFound")
          } yield {
            Project.insertOne(Project(name, owner._id))
            Redirect(routes.ProjectAdministration.list).flashing(
              FlashSuccess(Messages("project.createSuccess")))
          }
      })
  }
{'fraction_non_alphanumeric': 0.11719636776390466, 'fraction_numerical': 0.0065266742338251985, 'mean_word_length': 3.566062176165803, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '4773075', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1017, 'n_words': 188}