From 93f8f9252304d4e8028e13dd95e4cdf2e3178594 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 18 May 2014 18:07:04 -0400 Subject: [PATCH] Finish create new label --- cmd/web.go | 1 + gogs.go | 2 +- models/issue.go | 47 ++++++++++++++++++++++++++++++--------- models/login.go | 5 ++--- models/models.go | 2 +- modules/auth/repo.go | 25 +++++++++++++++++++++ routers/repo/issue.go | 33 ++++++++++++++++++++++++++- templates/issue/list.tmpl | 14 +++++++----- 8 files changed, 106 insertions(+), 23 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 2eeca9fa3..3335c5396 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -186,6 +186,7 @@ func runWeb(*cli.Context) { r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) r.Post("/issues/:index/assignee", repo.UpdateAssignee) r.Post("/issues/:index/milestone", repo.UpdateIssueMilestone) + r.Post("/issues/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel) r.Get("/issues/milestones", repo.Milestones) r.Get("/issues/milestones/new", repo.NewMilestone) r.Post("/issues/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost) diff --git a/gogs.go b/gogs.go index a21d8eae0..ca00fb508 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/base" ) -const APP_VER = "0.3.5.0516 Alpha" +const APP_VER = "0.3.5.0518 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/issue.go b/models/issue.go index d3eb89c35..62cc9363b 100644 --- a/models/issue.go +++ b/models/issue.go @@ -400,17 +400,6 @@ func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error { return nil } -// Label represents a label of repository for issues. -type Label struct { - Id int64 - RepoId int64 `xorm:"INDEX"` - Name string - Color string - NumIssues int - NumClosedIssues int - NumOpenIssues int `xorm:"-"` -} - // _____ .__.__ __ // / \ |__| | ____ _______/ |_ ____ ____ ____ // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \ @@ -622,6 +611,42 @@ func DeleteMilestone(m *Milestone) (err error) { return sess.Commit() } +// .____ ___. .__ +// | | _____ \_ |__ ____ | | +// | | \__ \ | __ \_/ __ \| | +// | |___ / __ \| \_\ \ ___/| |__ +// |_______ (____ /___ /\___ >____/ +// \/ \/ \/ \/ + +// Label represents a label of repository for issues. +type Label struct { + Id int64 + RepoId int64 `xorm:"INDEX"` + Name string + Color string `xorm:"VARCHAR(7)"` + NumIssues int + NumClosedIssues int + NumOpenIssues int `xorm:"-"` +} + +// CalOpenIssues calculates the open issues of label. +func (m *Label) CalOpenIssues() { + m.NumOpenIssues = m.NumIssues - m.NumClosedIssues +} + +// NewLabel creates new label of repository. +func NewLabel(l *Label) error { + _, err := orm.Insert(l) + return err +} + +// GetLabels returns a list of labels of given repository ID. +func GetLabels(repoId int64) ([]*Label, error) { + labels := make([]*Label, 0, 10) + err := orm.Where("repo_id=?", repoId).Find(&labels) + return labels, err +} + // _________ __ // \_ ___ \ ____ _____ _____ ____ _____/ |_ // / \ \/ / _ \ / \ / \_/ __ \ / \ __\ diff --git a/models/login.go b/models/login.go index ed5a8425a..3efef2f78 100644 --- a/models/login.go +++ b/models/login.go @@ -185,9 +185,8 @@ func LoginUser(uname, passwd string) (*User, error) { } else { if !has { var sources []LoginSource - cond := &LoginSource{IsActived: true, AllowAutoRegister: true} - err = orm.UseBool().Find(&sources, cond) - if err != nil { + if err = orm.UseBool().Find(&sources, + &LoginSource{IsActived: true, AllowAutoRegister: true}); err != nil { return nil, err } diff --git a/models/models.go b/models/models.go index 3adec1a56..841b10639 100644 --- a/models/models.go +++ b/models/models.go @@ -35,7 +35,7 @@ func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), new(Mirror), new(Release), new(LoginSource), new(Webhook), new(IssueUser), - new(Milestone)) + new(Milestone), new(Label)) } func LoadModelsConfig() { diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 92ca839f8..82cd07861 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -171,6 +171,31 @@ func (f *CreateMilestoneForm) Validate(errors *binding.Errors, req *http.Request validate(errors, data, f) } +// .____ ___. .__ +// | | _____ \_ |__ ____ | | +// | | \__ \ | __ \_/ __ \| | +// | |___ / __ \| \_\ \ ___/| |__ +// |_______ (____ /___ /\___ >____/ +// \/ \/ \/ \/ + +type CreateLabelForm struct { + Title string `form:"title" binding:"Required;MaxSize(50)"` + Color string `form:"color" binding:"Required;Size(7)"` +} + +func (f *CreateLabelForm) Name(field string) string { + names := map[string]string{ + "Title": "Label name", + "Color": "Label color", + } + return names[field] +} + +func (f *CreateLabelForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errors, data, f) +} + // __________ .__ // \______ \ ____ | | ____ _____ ______ ____ // | _// __ \| | _/ __ \\__ \ / ___// __ \ diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 83b84e289..6ff72c19f 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -63,7 +63,16 @@ func Issues(ctx *middleware.Context) { } mid = mile.Id } - fmt.Println(mid) + + labels, err := models.GetLabels(ctx.Repo.Repository.Id) + if err != nil { + ctx.Handle(500, "issue.Issues(GetLabels): %v", err) + return + } + for _, l := range labels { + l.CalOpenIssues() + } + ctx.Data["Labels"] = labels page, _ := base.StrTo(ctx.Query("page")).Int() @@ -591,6 +600,28 @@ func Comment(ctx *middleware.Context, params martini.Params) { ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index)) } +func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) { + if ctx.HasError() { + Issues(ctx) + return + } + + l := &models.Label{ + RepoId: ctx.Repo.Repository.Id, + Name: form.Title, + Color: form.Color, + } + if err := models.NewLabel(l); err != nil { + ctx.Handle(500, "issue.NewLabel(NewLabel)", err) + return + } + ctx.Redirect(ctx.Repo.RepoLink + "/issues") +} + +func UpdateLabel(ctx *middleware.Context, params martini.Params) { + +} + func Milestones(ctx *middleware.Context) { ctx.Data["Title"] = "Milestones" ctx.Data["IsRepoToolbarIssues"] = true diff --git a/templates/issue/list.tmpl b/templates/issue/list.tmpl index fd67d4213..480e047b8 100644 --- a/templates/issue/list.tmpl +++ b/templates/issue/list.tmpl @@ -16,17 +16,18 @@

Label


-
+ + {{.CsrfTokenHtml}}
New Label
- - + +
@@ -36,6 +37,7 @@
+ {{template "base/alert" .}}