Browse Source

random: some code simplify

pull/4584/head
Unknwon 8 years ago
parent
commit
ab2197bc75
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 6
      models/action.go
  2. 8
      models/repo.go
  3. 62
      routes/repo/setting.go
  4. 33
      templates/repo/settings/options.tmpl

6
models/action.go

@ -73,10 +73,10 @@ func init() {
// it implemented interface base.Actioner so that can be used in template render. // it implemented interface base.Actioner so that can be used in template render.
type Action struct { type Action struct {
ID int64 ID int64
UserID int64 // Receiver user id. UserID int64 // Receiver user ID
OpType ActionType OpType ActionType
ActUserID int64 // Action user id. ActUserID int64 // Doer user OD
ActUserName string // Action user name. ActUserName string // Doer user name
ActAvatar string `xorm:"-"` ActAvatar string `xorm:"-"`
RepoID int64 `xorm:"INDEX"` RepoID int64 `xorm:"INDEX"`
RepoUserName string RepoUserName string

8
models/repo.go

@ -1339,14 +1339,14 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
return fmt.Errorf("getOwner: %v", err) return fmt.Errorf("getOwner: %v", err)
} }
if repo.Owner.IsOrganization() { if repo.Owner.IsOrganization() {
// Organization repository need to recalculate access table when visivility is changed. // Organization repository need to recalculate access table when visivility is changed
if err = repo.recalculateTeamAccesses(e, 0); err != nil { if err = repo.recalculateTeamAccesses(e, 0); err != nil {
return fmt.Errorf("recalculateTeamAccesses: %v", err) return fmt.Errorf("recalculateTeamAccesses: %v", err)
} }
} }
// Create/Remove git-daemon-export-ok for git-daemon... // Create/Remove git-daemon-export-ok for git-daemon
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`) daemonExportFile := path.Join(repo.RepoPath(), "git-daemon-export-ok")
if repo.IsPrivate && com.IsExist(daemonExportFile) { if repo.IsPrivate && com.IsExist(daemonExportFile) {
if err = os.Remove(daemonExportFile); err != nil { if err = os.Remove(daemonExportFile); err != nil {
log.Error(4, "Failed to remove %s: %v", daemonExportFile, err) log.Error(4, "Failed to remove %s: %v", daemonExportFile, err)
@ -1372,7 +1372,7 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
// Change visibility of generated actions // Change visibility of generated actions
if _, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&Action{IsPrivate: repo.IsPrivate}); err != nil { if _, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&Action{IsPrivate: repo.IsPrivate}); err != nil {
return fmt.Errorf("change action visibility of repository [id: %d]: %v", repo.ID, err) return fmt.Errorf("change action visibility of repository: %v", err)
} }
} }

62
routes/repo/setting.go

@ -32,21 +32,21 @@ const (
) )
func Settings(c *context.Context) { func Settings(c *context.Context) {
c.Data["Title"] = c.Tr("repo.settings") c.Title("repo.settings")
c.Data["PageIsSettingsOptions"] = true c.PageIs("SettingsOptions")
c.HTML(200, SETTINGS_OPTIONS) c.Success(SETTINGS_OPTIONS)
} }
func SettingsPost(c *context.Context, f form.RepoSetting) { func SettingsPost(c *context.Context, f form.RepoSetting) {
c.Data["Title"] = c.Tr("repo.settings") c.Title("repo.settings")
c.Data["PageIsSettingsOptions"] = true c.PageIs("SettingsOptions")
repo := c.Repo.Repository repo := c.Repo.Repository
switch c.Query("action") { switch c.Query("action") {
case "update": case "update":
if c.HasError() { if c.HasError() {
c.HTML(200, SETTINGS_OPTIONS) c.Success(SETTINGS_OPTIONS)
return return
} }
@ -57,7 +57,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
if repo.LowerName != strings.ToLower(newRepoName) { if repo.LowerName != strings.ToLower(newRepoName) {
isNameChanged = true isNameChanged = true
if err := models.ChangeRepositoryName(c.Repo.Owner, repo.Name, newRepoName); err != nil { if err := models.ChangeRepositoryName(c.Repo.Owner, repo.Name, newRepoName); err != nil {
c.Data["Err_RepoName"] = true c.FormErr("RepoName")
switch { switch {
case models.IsErrRepoAlreadyExist(err): case models.IsErrRepoAlreadyExist(err):
c.RenderWithErr(c.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &f) c.RenderWithErr(c.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &f)
@ -66,7 +66,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case models.IsErrNamePatternNotAllowed(err): case models.IsErrNamePatternNotAllowed(err):
c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &f) c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &f)
default: default:
c.Handle(500, "ChangeRepositoryName", err) c.ServerError("ChangeRepositoryName", err)
} }
return return
} }
@ -88,14 +88,14 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
visibilityChanged := repo.IsPrivate != f.Private visibilityChanged := repo.IsPrivate != f.Private
repo.IsPrivate = f.Private repo.IsPrivate = f.Private
if err := models.UpdateRepository(repo, visibilityChanged); err != nil { if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
c.Handle(500, "UpdateRepository", err) c.ServerError("UpdateRepository", err)
return return
} }
log.Trace("Repository basic settings updated: %s/%s", c.Repo.Owner.Name, repo.Name) log.Trace("Repository basic settings updated: %s/%s", c.Repo.Owner.Name, repo.Name)
if isNameChanged { if isNameChanged {
if err := models.RenameRepoAction(c.User, oldRepoName, repo); err != nil { if err := models.RenameRepoAction(c.User, oldRepoName, repo); err != nil {
log.Error(4, "RenameRepoAction: %v", err) log.Error(2, "RenameRepoAction: %v", err)
} }
} }
@ -104,7 +104,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case "mirror": case "mirror":
if !repo.IsMirror { if !repo.IsMirror {
c.Handle(404, "", nil) c.NotFound()
return return
} }
@ -113,12 +113,12 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
c.Repo.Mirror.Interval = f.Interval c.Repo.Mirror.Interval = f.Interval
c.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(f.Interval) * time.Hour) c.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(f.Interval) * time.Hour)
if err := models.UpdateMirror(c.Repo.Mirror); err != nil { if err := models.UpdateMirror(c.Repo.Mirror); err != nil {
c.Handle(500, "UpdateMirror", err) c.ServerError("UpdateMirror", err)
return return
} }
} }
if err := c.Repo.Mirror.SaveAddress(f.MirrorAddress); err != nil { if err := c.Repo.Mirror.SaveAddress(f.MirrorAddress); err != nil {
c.Handle(500, "SaveAddress", err) c.ServerError("SaveAddress", err)
return return
} }
@ -127,7 +127,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case "mirror-sync": case "mirror-sync":
if !repo.IsMirror { if !repo.IsMirror {
c.Handle(404, "", nil) c.NotFound()
return return
} }
@ -149,7 +149,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
repo.EnablePulls = f.EnablePulls repo.EnablePulls = f.EnablePulls
if err := models.UpdateRepository(repo, false); err != nil { if err := models.UpdateRepository(repo, false); err != nil {
c.Handle(500, "UpdateRepository", err) c.ServerError("UpdateRepository", err)
return return
} }
log.Trace("Repository advanced settings updated: %s/%s", c.Repo.Owner.Name, repo.Name) log.Trace("Repository advanced settings updated: %s/%s", c.Repo.Owner.Name, repo.Name)
@ -159,7 +159,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case "convert": case "convert":
if !c.Repo.IsOwner() { if !c.Repo.IsOwner() {
c.Error(404) c.NotFound()
return return
} }
if repo.Name != f.RepoName { if repo.Name != f.RepoName {
@ -169,22 +169,22 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
if c.Repo.Owner.IsOrganization() { if c.Repo.Owner.IsOrganization() {
if !c.Repo.Owner.IsOwnedBy(c.User.ID) { if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
c.Error(404) c.NotFound()
return return
} }
} }
if !repo.IsMirror { if !repo.IsMirror {
c.Error(404) c.NotFound()
return return
} }
repo.IsMirror = false repo.IsMirror = false
if _, err := models.CleanUpMigrateInfo(repo); err != nil { if _, err := models.CleanUpMigrateInfo(repo); err != nil {
c.Handle(500, "CleanUpMigrateInfo", err) c.ServerError("CleanUpMigrateInfo", err)
return return
} else if err = models.DeleteMirrorByRepoID(c.Repo.Repository.ID); err != nil { } else if err = models.DeleteMirrorByRepoID(c.Repo.Repository.ID); err != nil {
c.Handle(500, "DeleteMirrorByRepoID", err) c.ServerError("DeleteMirrorByRepoID", err)
return return
} }
log.Trace("Repository converted from mirror to regular: %s/%s", c.Repo.Owner.Name, repo.Name) log.Trace("Repository converted from mirror to regular: %s/%s", c.Repo.Owner.Name, repo.Name)
@ -193,7 +193,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case "transfer": case "transfer":
if !c.Repo.IsOwner() { if !c.Repo.IsOwner() {
c.Error(404) c.NotFound()
return return
} }
if repo.Name != f.RepoName { if repo.Name != f.RepoName {
@ -203,7 +203,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin { if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin {
if !c.Repo.Owner.IsOwnedBy(c.User.ID) { if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
c.Error(404) c.NotFound()
return return
} }
} }
@ -211,7 +211,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
newOwner := c.Query("new_owner_name") newOwner := c.Query("new_owner_name")
isExist, err := models.IsUserExist(0, newOwner) isExist, err := models.IsUserExist(0, newOwner)
if err != nil { if err != nil {
c.Handle(500, "IsUserExist", err) c.ServerError("IsUserExist", err)
return return
} else if !isExist { } else if !isExist {
c.RenderWithErr(c.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil) c.RenderWithErr(c.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
@ -222,7 +222,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
if models.IsErrRepoAlreadyExist(err) { if models.IsErrRepoAlreadyExist(err) {
c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil) c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
} else { } else {
c.Handle(500, "TransferOwnership", err) c.ServerError("TransferOwnership", err)
} }
return return
} }
@ -232,7 +232,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case "delete": case "delete":
if !c.Repo.IsOwner() { if !c.Repo.IsOwner() {
c.Error(404) c.NotFound()
return return
} }
if repo.Name != f.RepoName { if repo.Name != f.RepoName {
@ -242,13 +242,13 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin { if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin {
if !c.Repo.Owner.IsOwnedBy(c.User.ID) { if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
c.Error(404) c.NotFound()
return return
} }
} }
if err := models.DeleteRepository(c.Repo.Owner.ID, repo.ID); err != nil { if err := models.DeleteRepository(c.Repo.Owner.ID, repo.ID); err != nil {
c.Handle(500, "DeleteRepository", err) c.ServerError("DeleteRepository", err)
return return
} }
log.Trace("Repository deleted: %s/%s", c.Repo.Owner.Name, repo.Name) log.Trace("Repository deleted: %s/%s", c.Repo.Owner.Name, repo.Name)
@ -258,7 +258,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
case "delete-wiki": case "delete-wiki":
if !c.Repo.IsOwner() { if !c.Repo.IsOwner() {
c.Error(404) c.NotFound()
return return
} }
if repo.Name != f.RepoName { if repo.Name != f.RepoName {
@ -268,7 +268,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin { if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin {
if !c.Repo.Owner.IsOwnedBy(c.User.ID) { if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
c.Error(404) c.NotFound()
return return
} }
} }
@ -278,7 +278,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
repo.EnableWiki = false repo.EnableWiki = false
if err := models.UpdateRepository(repo, false); err != nil { if err := models.UpdateRepository(repo, false); err != nil {
c.Handle(500, "UpdateRepository", err) c.ServerError("UpdateRepository", err)
return return
} }
@ -286,7 +286,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
c.Redirect(c.Repo.RepoLink + "/settings") c.Redirect(c.Repo.RepoLink + "/settings")
default: default:
c.Handle(404, "", nil) c.NotFound()
} }
} }

33
templates/repo/settings/options.tmpl

@ -6,11 +6,11 @@
{{template "repo/settings/navbar" .}} {{template "repo/settings/navbar" .}}
<div class="twelve wide column content"> <div class="twelve wide column content">
{{template "base/alert" .}} {{template "base/alert" .}}
<h4 class="ui top attached header"> <div class="ui top attached header">
{{.i18n.Tr "repo.settings.basic_settings"}} {{.i18n.Tr "repo.settings.basic_settings"}}
</h4> </div>
<div class="ui attached segment"> <div class="ui attached segment">
<form class="ui form" action="{{.Link}}" method="post"> <form class="ui form" action="{{.Link}}" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="update"> <input type="hidden" name="action" value="update">
<div class="required field {{if .Err_RepoName}}error{{end}}"> <div class="required field {{if .Err_RepoName}}error{{end}}">
@ -43,11 +43,11 @@
</div> </div>
{{if .Repository.IsMirror}} {{if .Repository.IsMirror}}
<h4 class="ui top attached header"> <div class="ui top attached header">
{{.i18n.Tr "repo.settings.mirror_settings"}} {{.i18n.Tr "repo.settings.mirror_settings"}}
</h4> </div>
<div class="ui attached segment"> <div class="ui attached segment">
<form class="ui form" method="post"> <form class="ui form" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="mirror"> <input type="hidden" name="action" value="mirror">
<div class="inline field {{if .Err_EnablePrune}}error{{end}}"> <div class="inline field {{if .Err_EnablePrune}}error{{end}}">
@ -74,7 +74,7 @@
<div class="ui divider"></div> <div class="ui divider"></div>
<form class="ui form" method="post"> <form class="ui form" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="mirror-sync"> <input type="hidden" name="action" value="mirror-sync">
<div class="inline field"> <div class="inline field">
@ -88,11 +88,11 @@
</div> </div>
{{end}} {{end}}
<h4 class="ui top attached header"> <div class="ui top attached header">
{{.i18n.Tr "repo.settings.advanced_settings"}} {{.i18n.Tr "repo.settings.advanced_settings"}}
</h4> </div>
<div class="ui attached segment"> <div class="ui attached segment">
<form class="ui form" method="post"> <form class="ui form" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="advanced"> <input type="hidden" name="action" value="advanced">
@ -199,7 +199,6 @@
</div> </div>
{{end}} {{end}}
<div class="ui divider"></div>
<div class="field"> <div class="field">
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button> <button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>
</div> </div>
@ -207,9 +206,9 @@
</div> </div>
{{if .IsRepositoryOwner}} {{if .IsRepositoryOwner}}
<h4 class="ui top attached warning header"> <div class="ui top attached warning header">
{{.i18n.Tr "repo.settings.danger_zone"}} {{.i18n.Tr "repo.settings.danger_zone"}}
</h4> </div>
<div class="ui attached warning table danger segment"> <div class="ui attached warning table danger segment">
{{if .Repository.IsMirror}} {{if .Repository.IsMirror}}
<div class="item"> <div class="item">
@ -276,7 +275,7 @@
<div class="ui warning message text left"> <div class="ui warning message text left">
{{.i18n.Tr "repo.settings.convert_notices_1" | Safe}} {{.i18n.Tr "repo.settings.convert_notices_1" | Safe}}
</div> </div>
<form class="ui form" action="{{.Link}}" method="post"> <form class="ui form" action="{{.Link}}" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="convert"> <input type="hidden" name="action" value="convert">
<div class="field"> <div class="field">
@ -308,7 +307,7 @@
{{.i18n.Tr "repo.settings.transfer_notices_1" | Safe}} <br> {{.i18n.Tr "repo.settings.transfer_notices_1" | Safe}} <br>
{{.i18n.Tr "repo.settings.transfer_notices_2" | Safe}} {{.i18n.Tr "repo.settings.transfer_notices_2" | Safe}}
</div> </div>
<form class="ui form" action="{{.Link}}" method="post"> <form class="ui form" action="{{.Link}}" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="transfer"> <input type="hidden" name="action" value="transfer">
<div class="field"> <div class="field">
@ -346,7 +345,7 @@
{{.i18n.Tr "repo.settings.delete_notices_fork_1" | Safe}} {{.i18n.Tr "repo.settings.delete_notices_fork_1" | Safe}}
{{end}} {{end}}
</div> </div>
<form class="ui form" action="{{.Link}}" method="post"> <form class="ui form" action="{{.Link}}" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="delete"> <input type="hidden" name="action" value="delete">
<div class="field"> <div class="field">
@ -378,7 +377,7 @@
{{.i18n.Tr "repo.settings.delete_notices_1" | Safe}}<br> {{.i18n.Tr "repo.settings.delete_notices_1" | Safe}}<br>
{{.i18n.Tr "repo.settings.wiki_delete_notices_1" .Repository.Name | Safe}} {{.i18n.Tr "repo.settings.wiki_delete_notices_1" .Repository.Name | Safe}}
</div> </div>
<form class="ui form" action="{{.Link}}" method="post"> <form class="ui form" action="{{.Link}}" method="POST">
{{.CSRFTokenHTML}} {{.CSRFTokenHTML}}
<input type="hidden" name="action" value="delete-wiki"> <input type="hidden" name="action" value="delete-wiki">
<div class="field"> <div class="field">

Loading…
Cancel
Save