<commit_before>package controllers

import com.gilt.apidoc.v0.models.{GeneratorCreateForm, GeneratorUpdateForm, Generator, Visibility, User}
import models.MainTemplate
import play.api.data.Forms._
import play.api.data._
import play.api.mvc._

import scala.concurrent.Future

object Generators extends Controller {

  implicit val context = scala.concurrent.ExecutionContext.Implicits.global

  def redirect = Action { implicit request =>
    Redirect(routes.Generators.index)
  }

  def index() = Authenticated.async { implicit request =>
    request.api.Generators.get().recover {
      case ex: Exception => Seq.empty
    }.map { generators =>
      Ok(views.html.generators.index(request.mainTemplate().copy(
        title = Some("Generators"),
        generators = generators
      )))
    }
  }

  def getByKey(key: String) = Authenticated.async { implicit request =>
    for {
      generator <- request.api.Generators.getByKey(key)
    } yield {
      generator match {
        case None => Redirect(routes.Generators.index()).flashing("warning" -> s"Generator not found")
        case Some(g) => {
          Ok(views.html.generators.details(request.mainTemplate().copy(
            title = Some("Generator Details"),
            generators = Seq(g)
          )))
        }
      }
    }
  }

  def postUpdate(key: String) = Authenticated.async { implicit request =>
    val tpl = request.mainTemplate(Some("Add Generator"))
    val boundForm = generatorUpdateForm.bindFromRequest
    boundForm.fold (

      errors => Future {
        InternalServerError
      },

      valid => {
        request.api.Generators.putByKey(
          key,
          GeneratorUpdateForm(
            visibility = valid.visibility.map(Visibility(_)),
            enabled = valid.enabled
          )
        ).map(_ => Ok)
      }
    )
  }

  def create() = Authenticated { implicit request =>
    Ok(views.html.generators.form(
      request.mainTemplate(Some("Add Generator")),
      1,
      generatorCreateForm
    ))
  }

  def postCreate() = Authenticated.async { implicit request =>
    val tpl = request.mainTemplate(Some("Add Generator"))
    val boundForm = generatorCreateForm.bindFromRequest
    boundForm.fold (

      errors => Future {
        Ok(views.html.generators.form(tpl, 1, errors))
      },

      (valid: GeneratorCreateData) => {
        valid.details.headOption.map { _ =>
          val futures: List[Future[Generator]] = valid.details.filter(_.selected).map { detail =>
            request.api.Generators.post(GeneratorCreateForm(
              key = detail.key,
              uri = valid.uri,
              visibility = Visibility(detail.visibility)
            ))
          }
          Future.sequence(futures).map { d =>
            Redirect(routes.Generators.index()).flashing("success" -> s"Generator(s) added")
          }.recover {
            case response: com.gilt.apidoc.v0.errors.ErrorsResponse => {
              Ok(views.html.generators.form(tpl, 1, boundForm, response.errors.map(_.message)))
            }
          }
        }.getOrElse {
          val existingGenF = request.api.Generators.get()
          val newGenF = new com.gilt.apidoc.generator.v0.Client(valid.uri).generators.get()
          (for {
            existingGenerators <- existingGenF
            newGenerators <- newGenF
          } yield {
            val existingKeys = existingGenerators.filter(_.uri == valid.uri).map(_.key).toSet
            val d = newGenerators.toList.map(gen => GeneratorDetails(gen.key, Visibility.Public.toString, !existingKeys.contains(gen.key)))
            Ok(views.html.generators.form(tpl, 2, generatorCreateForm.fill(valid.copy(details = d))))
          }).recover {
            case response: com.gilt.apidoc.v0.errors.ErrorsResponse => {
              Ok(views.html.generators.form(tpl, 1, boundForm, response.errors.map(_.message)))
            }
          }
        }
      }
    )
  }

  def postRemove(key: String) = Authenticated.async { implicit request =>
    for {
      response <- request.api.Generators.deleteByKey(key)
    } yield {
      Redirect(routes.Generators.index()).flashing("success" -> s"Generator removed")
    }
  }<commit_msg>Prepend http:// by default to generator url from form<commit_after>package controllers

import com.gilt.apidoc.v0.models.{GeneratorCreateForm, GeneratorUpdateForm, Generator, Visibility, User}
import models.MainTemplate
import play.api.data.Forms._
import play.api.data._
import play.api.mvc._

import scala.concurrent.Future

object Generators extends Controller {

  implicit val context = scala.concurrent.ExecutionContext.Implicits.global

  def redirect = Action { implicit request =>
    Redirect(routes.Generators.index)
  }

  def index() = Authenticated.async { implicit request =>
    request.api.Generators.get().recover {
      case ex: Exception => Seq.empty
    }.map { generators =>
      Ok(views.html.generators.index(request.mainTemplate().copy(
        title = Some("Generators"),
        generators = generators
      )))
    }
  }

  def getByKey(key: String) = Authenticated.async { implicit request =>
    for {
      generator <- request.api.Generators.getByKey(key)
    } yield {
      generator match {
        case None => Redirect(routes.Generators.index()).flashing("warning" -> s"Generator not found")
        case Some(g) => {
          Ok(views.html.generators.details(request.mainTemplate().copy(
            title = Some("Generator Details"),
            generators = Seq(g)
          )))
        }
      }
    }
  }

  def postUpdate(key: String) = Authenticated.async { implicit request =>
    val tpl = request.mainTemplate(Some("Add Generator"))
    val boundForm = generatorUpdateForm.bindFromRequest
    boundForm.fold (

      errors => Future {
        println("ERRORS: " + errors)
        InternalServerError
      },

      valid => {
        request.api.Generators.putByKey(
          key,
          GeneratorUpdateForm(
            visibility = valid.visibility.map(Visibility(_)),
            enabled = valid.enabled
          )
        ).map(_ => Ok)
      }
    )
  }

  def create() = Authenticated { implicit request =>
    Ok(views.html.generators.form(
      request.mainTemplate(Some("Add Generator")),
      1,
      generatorCreateForm
    ))
  }

  def postCreate() = Authenticated.async { implicit request =>
    val tpl = request.mainTemplate(Some("Add Generator"))
    val boundForm = generatorCreateForm.bindFromRequest
    boundForm.fold (

      errors => Future {
        Ok(views.html.generators.form(tpl, 1, errors))
      },

      (valid: GeneratorCreateData) => {
        valid.details.headOption.map { _ =>
          val futures: List[Future[Generator]] = valid.details.filter(_.selected).map { detail =>
            request.api.Generators.post(GeneratorCreateForm(
              key = detail.key,
              uri = valid.uri,
              visibility = Visibility(detail.visibility)
            ))
          }
          Future.sequence(futures).map { d =>
            Redirect(routes.Generators.index()).flashing("success" -> s"Generator(s) added")
          }.recover {
            case response: com.gilt.apidoc.v0.errors.ErrorsResponse => {
              Ok(views.html.generators.form(tpl, 1, boundForm, response.errors.map(_.message)))
            }
          }
        }.getOrElse {
          val uri = if (valid.uri.toLowerCase.trim.startsWith("http")) {
            valid.uri.trim
          } else {
            "http://" + valid.uri.trim
          }

          val existingGenF = request.api.Generators.get()
          val newGenF = new com.gilt.apidoc.generator.v0.Client(uri).generators.get()
          (for {
            existingGenerators <- existingGenF
            newGenerators <- newGenF
          } yield {
            val existingKeys = existingGenerators.filter(_.uri == uri).map(_.key).toSet
            val d = newGenerators.toList.map(gen => GeneratorDetails(gen.key, Visibility.Public.toString, !existingKeys.contains(gen.key)))
            Ok(views.html.generators.form(tpl, 2, generatorCreateForm.fill(valid.copy(details = d))))
          }).recover {
            case response: com.gilt.apidoc.v0.errors.ErrorsResponse => {
              Ok(views.html.generators.form(tpl, 1, boundForm, response.errors.map(_.message)))
            }
          }
        }
      }
    )
  }

  def postRemove(key: String) = Authenticated.async { implicit request =>
    for {
      response <- request.api.Generators.deleteByKey(key)
    } yield {
      Redirect(routes.Generators.index()).flashing("success" -> s"Generator removed")
    }
  }
{'fraction_non_alphanumeric': 0.12189459020199675, 'fraction_numerical': 0.0020896215463199444, 'mean_word_length': 2.336560805577072, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, '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': '1065221', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2435, 'n_words': 432}