Browse Source

Update repo.Create

pull/31/head
Unknown 11 years ago
parent
commit
76ce6f9848
  1. 2
      gogs.go
  2. 1
      modules/auth/form.go
  3. 53
      modules/auth/repo.go
  4. 32
      routers/repo/repo.go
  5. 4
      routers/user/user.go
  6. 12
      templates/repo/create.tmpl
  7. 4
      templates/repo/created.tmpl
  8. 2
      web.go

2
gogs.go

@ -19,7 +19,7 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.0.2.0307" const APP_VER = "0.0.2.0308"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

1
modules/auth/form.go

@ -17,6 +17,7 @@ import (
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
) )
// Web form interface.
type Form interface { type Form interface {
Name(field string) string Name(field string) string
} }

53
modules/auth/repo.go

@ -0,0 +1,53 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package auth
import (
"net/http"
"reflect"
"github.com/codegangsta/martini"
"github.com/gogits/binding"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
)
type CreateRepoForm struct {
UserId int64 `form:"userId"`
RepoName string `form:"repo" binding:"Required"`
Visibility string `form:"visibility"`
Description string `form:"desc" binding:"MaxSize(100)"`
Language string `form:"language"`
InitReadme string `form:"initReadme"`
}
func (f *CreateRepoForm) Name(field string) string {
names := map[string]string{
"RepoName": "Repository name",
"Description": "Description",
}
return names[field]
}
func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
if req.Method == "GET" || errors.Count() == 0 {
return
}
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
data["HasError"] = true
AssignForm(f, data)
if len(errors.Overall) > 0 {
for _, err := range errors.Overall {
log.Error("CreateRepoForm.Validate: %v", err)
}
return
}
validate(errors, data, f)
}

32
routers/repo/repo.go

@ -7,7 +7,6 @@ package repo
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
"github.com/martini-contrib/sessions" "github.com/martini-contrib/sessions"
@ -18,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
) )
func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
data["Title"] = "Create repository" data["Title"] = "Create repository"
if req.Method == "GET" { if req.Method == "GET" {
@ -26,30 +25,37 @@ func Create(req *http.Request, r render.Render, data base.TmplData, session sess
return return
} }
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
r.HTML(200, "repo/create", data)
return
}
// TODO: access check // TODO: access check
id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64) user, err := models.GetUserById(form.UserId)
if err == nil { if err != nil {
var u *models.User if err.Error() == models.ErrUserNotExist.Error() {
u, err = models.GetUserById(id) data["HasError"] = true
if u == nil { data["ErrorMsg"] = "User does not exist"
err = models.ErrUserNotExist auth.AssignForm(form, data)
r.HTML(200, "repo/create", data)
return
} }
if err == nil {
_, err = models.CreateRepository(u, req.FormValue("name"))
} }
if err == nil { if err == nil {
data["RepoName"] = u.Name + "/" + req.FormValue("name") // TODO: init description and readme
if _, err = models.CreateRepository(user, form.RepoName); err == nil {
data["RepoName"] = user.Name + "/" + form.RepoName
r.HTML(200, "repo/created", data) r.HTML(200, "repo/created", data)
fmt.Println("good!!!!")
return return
} }
} }
if err != nil {
data["ErrorMsg"] = err data["ErrorMsg"] = err
log.Error("repo.Create: %v", err) log.Error("repo.Create: %v", err)
r.HTML(200, "base/error", data) r.HTML(200, "base/error", data)
}
} }
func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {

4
routers/user/user.go

@ -64,8 +64,8 @@ func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render
} }
data["ErrorMsg"] = err data["ErrorMsg"] = err
log.Error("user.SignIn: %v", data) log.Error("user.SignIn: %v", err)
r.HTML(200, "base/error", nil) r.HTML(200, "base/error", data)
return return
} }

12
templates/repo/create.tmpl

@ -3,6 +3,7 @@
<div class="container" id="gogs-body"> <div class="container" id="gogs-body">
<form action="/repo/create" method="post" class="form-horizontal gogs-card" id="gogs-repo-create"> <form action="/repo/create" method="post" class="form-horizontal gogs-card" id="gogs-repo-create">
<h3>Create New Repository</h3> <h3>Create New Repository</h3>
<div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div>
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label> <label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
@ -11,10 +12,10 @@
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group {{if .Err_RepoName}}has-error has-feedback{{end}}">
<label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label> <label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
<input name="repo" type="text" class="form-control" placeholder="Type your repository name" required="required"> <input name="repo" type="text" class="form-control" placeholder="Type your repository name" value="{{.repo}}" required="required">
<span class="help-block">Great repository names are short and memorable. </span> <span class="help-block">Great repository names are short and memorable. </span>
</div> </div>
</div> </div>
@ -23,13 +24,14 @@
<label class="col-md-2 control-label">Visibility<strong class="text-danger">*</strong></label> <label class="col-md-2 control-label">Visibility<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
<p class="form-control-static">Public</p> <p class="form-control-static">Public</p>
<input type="hidden" value="public" name="visibility"/>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group {{if .Err_Description}}has-error has-feedback{{end}}">
<label class="col-md-2 control-label">Description</label> <label class="col-md-2 control-label">Description</label>
<div class="col-md-8"> <div class="col-md-8">
<textarea name="desc" class="form-control" placeholder="Type your repository name"></textarea> <textarea name="desc" class="form-control" placeholder="Type your repository description">{{.desc}}</textarea>
</div> </div>
</div> </div>
@ -51,7 +53,7 @@
<div class="col-md-8 col-md-offset-2"> <div class="col-md-8 col-md-offset-2">
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" value="true" name="init-md"> <input type="checkbox" value="" name="initReadme">
<strong>Initialize this repository with a README</strong> <strong>Initialize this repository with a README</strong>
</label> </label>
</div> </div>

4
templates/repo/created.tmpl

@ -1,7 +1,7 @@
{{template "base/head" .}} {{template "base/head" .}}
{{template "base/navbar" .}} {{template "base/navbar" .}}
<div class="container"> <div class="container" id="gogs-body">
<div class="col-md-offset-4 col-md-3"> <div class="col-md-offset-4 col-md-3">
Created successfully! Created successfully!
</div> </div>
</div> </div>

2
web.go

@ -69,7 +69,7 @@ func runWeb(*cli.Context) {
m.Any("/user/publickey/add", auth.SignInRequire(true), user.AddPublicKey) m.Any("/user/publickey/add", auth.SignInRequire(true), user.AddPublicKey)
m.Any("/user/publickey/list", auth.SignInRequire(true), user.ListPublicKey) m.Any("/user/publickey/list", auth.SignInRequire(true), user.ListPublicKey)
m.Any("/repo/create", auth.SignInRequire(true), repo.Create) m.Any("/repo/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete) m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete)
m.Any("/repo/list", auth.SignInRequire(false), repo.List) m.Any("/repo/list", auth.SignInRequire(false), repo.List)

Loading…
Cancel
Save