Skip to content

Commit 689cb32

Browse files
committed
fine tune
1 parent 2de3d6a commit 689cb32

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

modules/context/api.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ type APIContext struct {
3030

3131
Cache cache.Cache
3232

33-
Doer *user_model.User
33+
Doer *user_model.User // current signed-in user
3434
IsSigned bool
3535
IsBasicAuth bool
3636

37-
ContextUser *user_model.User
38-
Repo *Repository
39-
Org *APIOrganization
40-
Package *Package
37+
ContextUser *user_model.User // the user which is being visited, in most cases it differs from Doer
38+
39+
Repo *Repository
40+
Org *APIOrganization
41+
Package *Package
4142
}
4243

4344
// Currently, we have the following common fields in error response:

modules/context/base.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ type Base struct {
3434

3535
Resp ResponseWriter
3636
Req *http.Request
37-
Data middleware.ContextData // data used by MVC templates, it's also used to pass data between middlewares/handler
3837

38+
// Data is prepared by ContextDataStore middleware, this field only refers to the pre-created/prepared ContextData.
39+
// Although it's mainly used for MVC templates, sometimes it's also used to pass data between middlewares/handler
40+
Data middleware.ContextData
41+
42+
// Locale is mainly for Web context, although the API context also uses it in some cases: message response, form validation
3943
Locale translation.Locale
4044
}
4145

modules/context/context.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ type Context struct {
4545
Flash *middleware.Flash
4646
Session session.Store
4747

48-
Link string // current request URL (without query string)
49-
Doer *user_model.User
48+
Link string // current request URL (without query string)
49+
50+
Doer *user_model.User // current signed-in user
5051
IsSigned bool
5152
IsBasicAuth bool
5253

53-
ContextUser *user_model.User
54-
Repo *Repository
55-
Org *Organization
56-
Package *Package
54+
ContextUser *user_model.User // the user which is being visited, in most cases it differs from Doer
55+
56+
Repo *Repository
57+
Org *Organization
58+
Package *Package
5759
}
5860

5961
// TrHTMLEscapeArgs runs ".Locale.Tr()" but pre-escapes all arguments with html.EscapeString.
@@ -75,17 +77,17 @@ func GetContext(req *http.Request) *Context {
7577
return ctx
7678
}
7779

80+
// ValidateContext is a special context for form validation middleware. It may be different from other contexts.
7881
type ValidateContext struct {
7982
*Base
80-
Locale translation.Locale
8183
}
8284

8385
// GetValidateContext gets a context for middleware form validation
8486
func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
8587
if ctxAPI, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
86-
ctx = &ValidateContext{Base: ctxAPI.Base, Locale: ctxAPI.Base.Locale}
88+
ctx = &ValidateContext{Base: ctxAPI.Base}
8789
} else if ctxWeb, ok := req.Context().Value(contextKey).(*Context); ok {
88-
ctx = &ValidateContext{Base: ctxWeb.Base, Locale: ctxWeb.Base.Locale}
90+
ctx = &ValidateContext{Base: ctxWeb.Base}
8991
} else {
9092
panic("invalid context, expect either APIContext or Context")
9193
}
@@ -203,7 +205,7 @@ func (ctx *Context) HasError() bool {
203205
if !ok {
204206
return false
205207
}
206-
ctx.Flash.ErrorMsg = ctx.Data["ErrorMsg"].(string)
208+
ctx.Flash.ErrorMsg = ctx.GetErrMsg()
207209
ctx.Data["Flash"] = ctx.Flash
208210
return hasErr.(bool)
209211
}

0 commit comments

Comments
 (0)