<commit_before>/*
 * Copyright (c) 2013 <NAME> | <NAME> | <NAME>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package teams

import (
	"bytes"
	"html/template"
	"net/http"
	"fmt"

	"appengine"	

	"github.com/santiaago/purple-wing/helpers"
	templateshlp "github.com/santiaago/purple-wing/helpers/templates"
	"github.com/santiaago/purple-wing/helpers/auth"
	"github.com/santiaago/purple-wing/helpers/handlers"
	teamrelshlp "github.com/santiaago/purple-wing/helpers/teamrels"
	tournamentrelshlp "github.com/santiaago/purple-wing/helpers/tournamentrels"
	
	teammdl "github.com/santiaago/purple-wing/models/team"
	usermdl "github.com/santiaago/purple-wing/models/user"
	searchmdl "github.com/santiaago/purple-wing/models/search"
	teaminvidmdl "github.com/santiaago/purple-wing/models/teamInvertedIndex"
	teamrelmdl "github.com/santiaago/purple-wing/models/teamrel"
	tournamentteamrelmdl "github.com/santiaago/purple-wing/models/tournamentteamrel"
)

type NewForm struct {
	Name string
	Private bool
	Error string
}

func Index(w http.ResponseWriter, r *http.Request){
	c := appengine.NewContext(r)
	
	funcs := template.FuncMap{
		"Teams": func() bool {return true},
	}
	
	t := template.Must(template.New("tmpl_team_index").
		ParseFiles("templates/team/index.html"))
	if r.Method == "GET"{
		teams := teammdl.FindAll(r)
		indexData := struct { 
			Teams []*teammdl.Team
			TeamInputSearch string
		}{
			teams,
			"",
		}
		var buf bytes.Buffer
		err := t.ExecuteTemplate(&buf,"tmpl_team_index", indexData)
		index := buf.Bytes()
		
		if err != nil{
			c.Errorf("pw: error in parse template team_index: %v", err)
		}

		err = templateshlp.Render(w, r, index, &funcs, "renderTeamIndex")
		if err != nil{
			c.Errorf("pw: error when calling Render from helpers: %v", err)
		}
	}else if r.Method == "POST"{
		query := r.FormValue("TeamInputSearch")
		words := helpers.SetOfStrings(query)
		ids := teaminvidmdl.GetIndexes(r,words)
		c.Infof("pw: search:%v Ids:%v",query, ids)
		//result := searchmdl.Score(r, query, ids)
		searchmdl.Score(r, query, ids)

		teams := teammdl.FindAll(r)
		indexData := struct { 
			Teams []*teammdl.Team
			TeamInputSearch string
		}{
			teams,
			"q:"+r.FormValue("TeamInputSearch"),
		}
		var buf bytes.Buffer
		err := t.ExecuteTemplate(&buf,"tmpl_team_index", indexData)
		index := buf.Bytes()
		
		if err != nil{
			c.Errorf("pw: error in parse template team_index: %v", err)
		}

		err = templateshlp.Render(w, r, index, &funcs, "renderTeamIndex")
		if err != nil{
			c.Errorf("pw: error when calling Render from helpers: %v", err)
		}
	}
}

func New(w http.ResponseWriter, r *http.Request){
	c := appengine.NewContext(r)
	
	funcs := template.FuncMap{}
	
	t := template.Must(template.New("tmpl_team_new").
		ParseFiles("templates/team/new.html"))
	
	var form NewForm
	if r.Method == "GET" {
		form.Name = ""
		form.Error = ""
	} else if r.Method == "POST" {
		form.Name = r.FormValue("Name")
		form.Private = (r.FormValue("Visibility") == "Private")
		
		if len(form.Name) <= 0 {
			form.Error = "'Name' field cannot be empty"
		} else if t := teammdl.Find(r, "KeyName", helpers.TrimLower(form.Name)); t != nil {
			form.Error = "That team name already exists."
		} else {
			team := teammdl.Create(r, form.Name, auth.CurrentUser(r).Id, form.Private)
			// join the team
			teamrelmdl.Create(r, team.Id, auth.CurrentUser(r).Id)
			// redirect to the newly created team page
			http.Redirect(w, r, "/m/teams/" + fmt.Sprintf("%d", team.Id), http.StatusFound)
		}
	}
	
	var buf bytes.Buffer
	err := t.ExecuteTemplate(&buf,"tmpl_team_new", form)
	edit := buf.Bytes()

	if err != nil{
		c.Errorf("pw: error in parse template team_new: %v", err)
	}

	err = templateshlp.Render(w, r, edit, &funcs, "renderTeamNew")
	if err != nil{
		c.Errorf("pw: error when calling Render from helpers: %v", err)
	}
}

func Show(w http.ResponseWriter, r *http.Request){
	c := appengine.NewContext(r)
	
	intID, err := handlers.PermalinkID(r,3)
	if err != nil{
		http.Redirect(w,r, "/m/teams/", http.StatusFound)
	}
	
	if r.Method == "POST" && r.FormValue("Action") == "delete" {
		// delete all team-user relationships
		for _, player := range teamrelshlp.Players(r, intID) {
			if err := teamrelmdl.Destroy(r, intID, player.Id); err !=nil {
				c.Errorf("pw: error when trying to destroy team relationship: %v", err)
			}
		}
		// delete all tournament-team relationships
		for _, tournament := range tournamentrelshlp.Tournaments(r, intID) {
			if err := tournamentteamrelmdl.Destroy(r, tournament.Id, intID); err !=nil {
				c.Errorf("pw: error when trying to destroy team relationship: %v", err)
			}
		}
		// delete the team
		teammdl.Destroy(r, intID)
		
		http.Redirect(w, r, "/m/teams", http.StatusFound)
	}
	
	funcs := template.FuncMap{
		"Joined": func() bool { return teammdl.Joined(r, intID, auth.CurrentUser(r).Id) },
		"IsTeamAdmin": func() bool { return teammdl.IsTeamAdmin(r, intID, auth.CurrentUser(r).Id) },
	}
	
	t := template.Must(template.New("tmpl_team_show").
		Funcs(funcs).
		ParseFiles("templates/team/show.html",
		"templates/team/players.html"))

	var team *teammdl.Team
	team, err = teammdl.ById(r, intID)
	<commit_msg>tournament-team relationship is not deleted when team is deleted<commit_after>/*
 * Copyright (c) 2013 <NAME> | <NAME> | <NAME>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package teams

import (
	"bytes"
	"html/template"
	"net/http"
	"fmt"

	"appengine"	

	"github.com/santiaago/purple-wing/helpers"
	templateshlp "github.com/santiaago/purple-wing/helpers/templates"
	"github.com/santiaago/purple-wing/helpers/auth"
	"github.com/santiaago/purple-wing/helpers/handlers"
	teamrelshlp "github.com/santiaago/purple-wing/helpers/teamrels"
	tournamentrelshlp "github.com/santiaago/purple-wing/helpers/tournamentrels"
	
	teammdl "github.com/santiaago/purple-wing/models/team"
	usermdl "github.com/santiaago/purple-wing/models/user"
	searchmdl "github.com/santiaago/purple-wing/models/search"
	teaminvidmdl "github.com/santiaago/purple-wing/models/teamInvertedIndex"
	teamrelmdl "github.com/santiaago/purple-wing/models/teamrel"
	tournamentteamrelmdl "github.com/santiaago/purple-wing/models/tournamentteamrel"
)

type NewForm struct {
	Name string
	Private bool
	Error string
}

func Index(w http.ResponseWriter, r *http.Request){
	c := appengine.NewContext(r)
	
	funcs := template.FuncMap{
		"Teams": func() bool {return true},
	}
	
	t := template.Must(template.New("tmpl_team_index").
		ParseFiles("templates/team/index.html"))
	if r.Method == "GET"{
		teams := teammdl.FindAll(r)
		indexData := struct { 
			Teams []*teammdl.Team
			TeamInputSearch string
		}{
			teams,
			"",
		}
		var buf bytes.Buffer
		err := t.ExecuteTemplate(&buf,"tmpl_team_index", indexData)
		index := buf.Bytes()
		
		if err != nil{
			c.Errorf("pw: error in parse template team_index: %v", err)
		}

		err = templateshlp.Render(w, r, index, &funcs, "renderTeamIndex")
		if err != nil{
			c.Errorf("pw: error when calling Render from helpers: %v", err)
		}
	}else if r.Method == "POST"{
		query := r.FormValue("TeamInputSearch")
		words := helpers.SetOfStrings(query)
		ids := teaminvidmdl.GetIndexes(r,words)
		c.Infof("pw: search:%v Ids:%v",query, ids)
		//result := searchmdl.Score(r, query, ids)
		searchmdl.Score(r, query, ids)

		teams := teammdl.FindAll(r)
		indexData := struct { 
			Teams []*teammdl.Team
			TeamInputSearch string
		}{
			teams,
			"q:"+r.FormValue("TeamInputSearch"),
		}
		var buf bytes.Buffer
		err := t.ExecuteTemplate(&buf,"tmpl_team_index", indexData)
		index := buf.Bytes()
		
		if err != nil{
			c.Errorf("pw: error in parse template team_index: %v", err)
		}

		err = templateshlp.Render(w, r, index, &funcs, "renderTeamIndex")
		if err != nil{
			c.Errorf("pw: error when calling Render from helpers: %v", err)
		}
	}
}

func New(w http.ResponseWriter, r *http.Request){
	c := appengine.NewContext(r)
	
	funcs := template.FuncMap{}
	
	t := template.Must(template.New("tmpl_team_new").
		ParseFiles("templates/team/new.html"))
	
	var form NewForm
	if r.Method == "GET" {
		form.Name = ""
		form.Error = ""
	} else if r.Method == "POST" {
		form.Name = r.FormValue("Name")
		form.Private = (r.FormValue("Visibility") == "Private")
		
		if len(form.Name) <= 0 {
			form.Error = "'Name' field cannot be empty"
		} else if t := teammdl.Find(r, "KeyName", helpers.TrimLower(form.Name)); t != nil {
			form.Error = "That team name already exists."
		} else {
			team := teammdl.Create(r, form.Name, auth.CurrentUser(r).Id, form.Private)
			// join the team
			teamrelmdl.Create(r, team.Id, auth.CurrentUser(r).Id)
			// redirect to the newly created team page
			http.Redirect(w, r, "/m/teams/" + fmt.Sprintf("%d", team.Id), http.StatusFound)
		}
	}
	
	var buf bytes.Buffer
	err := t.ExecuteTemplate(&buf,"tmpl_team_new", form)
	edit := buf.Bytes()

	if err != nil{
		c.Errorf("pw: error in parse template team_new: %v", err)
	}

	err = templateshlp.Render(w, r, edit, &funcs, "renderTeamNew")
	if err != nil{
		c.Errorf("pw: error when calling Render from helpers: %v", err)
	}
}

func Show(w http.ResponseWriter, r *http.Request){
	c := appengine.NewContext(r)
	
	intID, err := handlers.PermalinkID(r,3)
	if err != nil{
		http.Redirect(w,r, "/m/teams/", http.StatusFound)
	}
	
	if r.Method == "POST" && r.FormValue("Action") == "delete" {
		// delete all team-user relationships
		for _, player := range teamrelshlp.Players(r, intID) {
			if err := teamrelmdl.Destroy(r, intID, player.Id); err !=nil {
				c.Errorf("pw: error when trying to destroy team relationship: %v", err)
			}
		}
		// delete all tournament-team relationships
		for _, tournament := range tournamentrelshlp.Teams(r, intID) {
			if err := tournamentteamrelmdl.Destroy(r, tournament.Id, intID); err !=nil {
				c.Errorf("pw: error when trying to destroy team relationship: %v", err)
			}
		}
		// delete the team
		teammdl.Destroy(r, intID)
		
		http.Redirect(w, r, "/m/teams", http.StatusFound)
	}
	
	funcs := template.FuncMap{
		"Joined": func() bool { return teammdl.Joined(r, intID, auth.CurrentUser(r).Id) },
		"IsTeamAdmin": func() bool { return teammdl.IsTeamAdmin(r, intID, auth.CurrentUser(r).Id) },
	}
	
	t := template.Must(template.New("tmpl_team_show").
		Funcs(funcs).
		ParseFiles("templates/team/show.html",
		"templates/team/players.html"))

	var team *teammdl.Team
	team, err = teammdl.ById(r, intID)
	
{'fraction_non_alphanumeric': 0.14336886993603412, 'fraction_numerical': 0.001023454157782516, 'mean_word_length': 3.9770797962648556, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 9, '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': '6581662', 'n_tokens_mistral': 4494, 'n_tokens_neox': 4134, 'n_words': 1038}