From ab2197bc75fc85089a4ff7f8d2ee46caf04507a4 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 11 Jun 2017 00:56:31 -0400 Subject: [PATCH] random: some code simplify --- models/action.go | 6 +-- models/repo.go | 8 ++-- routes/repo/setting.go | 62 ++++++++++++++-------------- templates/repo/settings/options.tmpl | 33 +++++++-------- 4 files changed, 54 insertions(+), 55 deletions(-) diff --git a/models/action.go b/models/action.go index 97fd379a3..1715c05ab 100644 --- a/models/action.go +++ b/models/action.go @@ -73,10 +73,10 @@ func init() { // it implemented interface base.Actioner so that can be used in template render. type Action struct { ID int64 - UserID int64 // Receiver user id. + UserID int64 // Receiver user ID OpType ActionType - ActUserID int64 // Action user id. - ActUserName string // Action user name. + ActUserID int64 // Doer user OD + ActUserName string // Doer user name ActAvatar string `xorm:"-"` RepoID int64 `xorm:"INDEX"` RepoUserName string diff --git a/models/repo.go b/models/repo.go index 22e15870d..cb1c96427 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1339,14 +1339,14 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e return fmt.Errorf("getOwner: %v", err) } 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 { return fmt.Errorf("recalculateTeamAccesses: %v", err) } } - // Create/Remove git-daemon-export-ok for git-daemon... - daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`) + // Create/Remove git-daemon-export-ok for git-daemon + daemonExportFile := path.Join(repo.RepoPath(), "git-daemon-export-ok") if repo.IsPrivate && com.IsExist(daemonExportFile) { if err = os.Remove(daemonExportFile); err != nil { 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 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) } } diff --git a/routes/repo/setting.go b/routes/repo/setting.go index 9168b04a2..71369320c 100644 --- a/routes/repo/setting.go +++ b/routes/repo/setting.go @@ -32,21 +32,21 @@ const ( ) func Settings(c *context.Context) { - c.Data["Title"] = c.Tr("repo.settings") - c.Data["PageIsSettingsOptions"] = true - c.HTML(200, SETTINGS_OPTIONS) + c.Title("repo.settings") + c.PageIs("SettingsOptions") + c.Success(SETTINGS_OPTIONS) } func SettingsPost(c *context.Context, f form.RepoSetting) { - c.Data["Title"] = c.Tr("repo.settings") - c.Data["PageIsSettingsOptions"] = true + c.Title("repo.settings") + c.PageIs("SettingsOptions") repo := c.Repo.Repository switch c.Query("action") { case "update": if c.HasError() { - c.HTML(200, SETTINGS_OPTIONS) + c.Success(SETTINGS_OPTIONS) return } @@ -57,7 +57,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { if repo.LowerName != strings.ToLower(newRepoName) { isNameChanged = true if err := models.ChangeRepositoryName(c.Repo.Owner, repo.Name, newRepoName); err != nil { - c.Data["Err_RepoName"] = true + c.FormErr("RepoName") switch { case models.IsErrRepoAlreadyExist(err): 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): c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &f) default: - c.Handle(500, "ChangeRepositoryName", err) + c.ServerError("ChangeRepositoryName", err) } return } @@ -88,14 +88,14 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { visibilityChanged := repo.IsPrivate != f.Private repo.IsPrivate = f.Private if err := models.UpdateRepository(repo, visibilityChanged); err != nil { - c.Handle(500, "UpdateRepository", err) + c.ServerError("UpdateRepository", err) return } log.Trace("Repository basic settings updated: %s/%s", c.Repo.Owner.Name, repo.Name) if isNameChanged { 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": if !repo.IsMirror { - c.Handle(404, "", nil) + c.NotFound() return } @@ -113,12 +113,12 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { c.Repo.Mirror.Interval = f.Interval c.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(f.Interval) * time.Hour) if err := models.UpdateMirror(c.Repo.Mirror); err != nil { - c.Handle(500, "UpdateMirror", err) + c.ServerError("UpdateMirror", err) return } } if err := c.Repo.Mirror.SaveAddress(f.MirrorAddress); err != nil { - c.Handle(500, "SaveAddress", err) + c.ServerError("SaveAddress", err) return } @@ -127,7 +127,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { case "mirror-sync": if !repo.IsMirror { - c.Handle(404, "", nil) + c.NotFound() return } @@ -149,7 +149,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { repo.EnablePulls = f.EnablePulls if err := models.UpdateRepository(repo, false); err != nil { - c.Handle(500, "UpdateRepository", err) + c.ServerError("UpdateRepository", err) return } 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": if !c.Repo.IsOwner() { - c.Error(404) + c.NotFound() return } 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.IsOwnedBy(c.User.ID) { - c.Error(404) + c.NotFound() return } } if !repo.IsMirror { - c.Error(404) + c.NotFound() return } repo.IsMirror = false if _, err := models.CleanUpMigrateInfo(repo); err != nil { - c.Handle(500, "CleanUpMigrateInfo", err) + c.ServerError("CleanUpMigrateInfo", err) return } else if err = models.DeleteMirrorByRepoID(c.Repo.Repository.ID); err != nil { - c.Handle(500, "DeleteMirrorByRepoID", err) + c.ServerError("DeleteMirrorByRepoID", err) return } 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": if !c.Repo.IsOwner() { - c.Error(404) + c.NotFound() return } 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.IsOwnedBy(c.User.ID) { - c.Error(404) + c.NotFound() return } } @@ -211,7 +211,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { newOwner := c.Query("new_owner_name") isExist, err := models.IsUserExist(0, newOwner) if err != nil { - c.Handle(500, "IsUserExist", err) + c.ServerError("IsUserExist", err) return } else if !isExist { 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) { c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil) } else { - c.Handle(500, "TransferOwnership", err) + c.ServerError("TransferOwnership", err) } return } @@ -232,7 +232,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { case "delete": if !c.Repo.IsOwner() { - c.Error(404) + c.NotFound() return } 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.IsOwnedBy(c.User.ID) { - c.Error(404) + c.NotFound() return } } if err := models.DeleteRepository(c.Repo.Owner.ID, repo.ID); err != nil { - c.Handle(500, "DeleteRepository", err) + c.ServerError("DeleteRepository", err) return } 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": if !c.Repo.IsOwner() { - c.Error(404) + c.NotFound() return } 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.IsOwnedBy(c.User.ID) { - c.Error(404) + c.NotFound() return } } @@ -278,7 +278,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { repo.EnableWiki = false if err := models.UpdateRepository(repo, false); err != nil { - c.Handle(500, "UpdateRepository", err) + c.ServerError("UpdateRepository", err) return } @@ -286,7 +286,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) { c.Redirect(c.Repo.RepoLink + "/settings") default: - c.Handle(404, "", nil) + c.NotFound() } } diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 91ffd0657..0e3256a9c 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -6,11 +6,11 @@ {{template "repo/settings/navbar" .}}
{{template "base/alert" .}} -

+
{{.i18n.Tr "repo.settings.basic_settings"}} -

+
-
+ {{.CSRFTokenHTML}}
@@ -43,11 +43,11 @@
{{if .Repository.IsMirror}} -

+
{{.i18n.Tr "repo.settings.mirror_settings"}} -

+
- + {{.CSRFTokenHTML}}
@@ -74,7 +74,7 @@
- + {{.CSRFTokenHTML}}
@@ -88,11 +88,11 @@
{{end}} -

+
{{.i18n.Tr "repo.settings.advanced_settings"}} -

+
- + {{.CSRFTokenHTML}} @@ -199,7 +199,6 @@
{{end}} -
@@ -207,9 +206,9 @@
{{if .IsRepositoryOwner}} -

+
{{.i18n.Tr "repo.settings.danger_zone"}} -

+
{{if .Repository.IsMirror}}
@@ -276,7 +275,7 @@
{{.i18n.Tr "repo.settings.convert_notices_1" | Safe}}
- + {{.CSRFTokenHTML}}
@@ -308,7 +307,7 @@ {{.i18n.Tr "repo.settings.transfer_notices_1" | Safe}}
{{.i18n.Tr "repo.settings.transfer_notices_2" | Safe}}
- + {{.CSRFTokenHTML}}
@@ -346,7 +345,7 @@ {{.i18n.Tr "repo.settings.delete_notices_fork_1" | Safe}} {{end}}
- + {{.CSRFTokenHTML}}
@@ -378,7 +377,7 @@ {{.i18n.Tr "repo.settings.delete_notices_1" | Safe}}
{{.i18n.Tr "repo.settings.wiki_delete_notices_1" .Repository.Name | Safe}}
- + {{.CSRFTokenHTML}}