Skip to content

Commit 7c2f16d

Browse files
committed
Add type filter to System Notices view
After go-gitea#10745 was merged, the number of System Notices increased obviously. Add the type filter is useful to help users to quickly check out the useful message they want. Signed-off-by: a1012112796 <[email protected]>
1 parent 6a4de37 commit 7c2f16d

File tree

7 files changed

+93
-22
lines changed

7 files changed

+93
-22
lines changed

models/admin.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ type NoticeType int
2020
const (
2121
//NoticeRepository type
2222
NoticeRepository NoticeType = iota + 1
23-
// NoticeTask type
24-
NoticeTask
23+
// NoticeTaskSuccess type
24+
NoticeTaskSuccess
25+
// NoticeTaskFail type
26+
NoticeTaskFail
2527
)
2628

2729
// Notice represents a system notice for admin.
@@ -59,6 +61,17 @@ func CreateRepositoryNotice(desc string, args ...interface{}) error {
5961
return createNotice(x, NoticeRepository, desc, args...)
6062
}
6163

64+
// CreateTaskNotice creates new system notice with type NoticeTaskSuccess or NoticeTaskFail.
65+
func CreateTaskNotice(isSuccess bool, desc string, args ...interface{}) (err error) {
66+
if isSuccess {
67+
err = createNotice(x, NoticeTaskSuccess, desc, args...)
68+
} else {
69+
err = createNotice(x, NoticeTaskFail, desc, args...)
70+
}
71+
72+
return
73+
}
74+
6275
// RemoveAllWithNotice removes all directories in given path and
6376
// creates a system notice when error occurs.
6477
func RemoveAllWithNotice(title, path string) {
@@ -76,18 +89,32 @@ func removeAllWithNotice(e Engine, title, path string) {
7689
}
7790

7891
// CountNotices returns number of notices.
79-
func CountNotices() int64 {
80-
count, _ := x.Count(new(Notice))
81-
return count
92+
func CountNotices(typ int) (count int64, err error) {
93+
if typ >= 1 {
94+
count, err = x.Where("type = ?", typ).Count(new(Notice))
95+
} else {
96+
count, err = x.Count(new(Notice))
97+
}
98+
return
8299
}
83100

84101
// Notices returns notices in given page.
85-
func Notices(page, pageSize int) ([]*Notice, error) {
86-
notices := make([]*Notice, 0, pageSize)
87-
return notices, x.
88-
Limit(pageSize, (page-1)*pageSize).
89-
Desc("id").
90-
Find(&notices)
102+
func Notices(typ, page, pageSize int) (notices []*Notice, err error) {
103+
notices = make([]*Notice, 0, pageSize)
104+
105+
if typ >= 1 {
106+
err = x.Where("type = ?", typ).
107+
Limit(pageSize, (page-1)*pageSize).
108+
Desc("id").
109+
Find(&notices)
110+
} else {
111+
err = x.
112+
Limit(pageSize, (page-1)*pageSize).
113+
Desc("id").
114+
Find(&notices)
115+
}
116+
117+
return notices, err
91118
}
92119

93120
// DeleteNotice deletes a system notice by given ID.

models/admin_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,35 @@ func TestCreateRepositoryNotice(t *testing.T) {
4646

4747
func TestCountNotices(t *testing.T) {
4848
assert.NoError(t, PrepareTestDatabase())
49-
assert.Equal(t, int64(3), CountNotices())
49+
num, err := CountNotices(0)
50+
assert.NoError(t, err)
51+
assert.Equal(t, int64(3), num)
52+
num, err = CountNotices(2)
53+
assert.NoError(t, err)
54+
assert.Equal(t, int64(1), num)
5055
}
5156

5257
func TestNotices(t *testing.T) {
5358
assert.NoError(t, PrepareTestDatabase())
5459

55-
notices, err := Notices(1, 2)
60+
notices, err := Notices(0, 1, 2)
5661
assert.NoError(t, err)
5762
if assert.Len(t, notices, 2) {
5863
assert.Equal(t, int64(3), notices[0].ID)
5964
assert.Equal(t, int64(2), notices[1].ID)
6065
}
6166

62-
notices, err = Notices(2, 2)
67+
notices, err = Notices(0, 2, 2)
6368
assert.NoError(t, err)
6469
if assert.Len(t, notices, 1) {
6570
assert.Equal(t, int64(1), notices[0].ID)
6671
}
72+
73+
notices, err = Notices(2, 1, 2)
74+
assert.NoError(t, err)
75+
if assert.Len(t, notices, 1) {
76+
assert.Equal(t, int64(3), notices[0].ID)
77+
}
6778
}
6879

6980
func TestDeleteNotice(t *testing.T) {

models/fixtures/notice.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
-
1212
id: 3
13-
type: 1 # NoticeRepository
13+
type: 2 # NoticeTaskSuccess
1414
description: description3

modules/cron/tasks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ func (t *Task) RunWithUser(doer *models.User, config Config) {
8888
if err := t.fun(ctx, doer, config); err != nil {
8989
if models.IsErrCancelled(err) {
9090
message := err.(models.ErrCancelled).Message
91-
if err := models.CreateNotice(models.NoticeTask, config.FormatMessage(t.Name, "aborted", doer, message)); err != nil {
91+
if err := models.CreateTaskNotice(false, config.FormatMessage(t.Name, "aborted", doer, message)); err != nil {
9292
log.Error("CreateNotice: %v", err)
9393
}
9494
return
9595
}
96-
if err := models.CreateNotice(models.NoticeTask, config.FormatMessage(t.Name, "error", doer, err)); err != nil {
96+
if err := models.CreateTaskNotice(false, config.FormatMessage(t.Name, "error", doer, err)); err != nil {
9797
log.Error("CreateNotice: %v", err)
9898
}
9999
return
100100
}
101-
if err := models.CreateNotice(models.NoticeTask, config.FormatMessage(t.Name, "finished", doer)); err != nil {
101+
if err := models.CreateTaskNotice(true, config.FormatMessage(t.Name, "finished", doer)); err != nil {
102102
log.Error("CreateNotice: %v", err)
103103
}
104104
})

options/locale/locale_en-US.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,10 @@ notices.delete_selected = Delete Selected
22882288
notices.delete_all = Delete All Notices
22892289
notices.type = Type
22902290
notices.type_1 = Repository
2291-
notices.type_2 = Task
2291+
notices.type_2 = Success Task
2292+
notices.type_3 = Fail Task
2293+
notices.type_all = All Type
2294+
notices.type_filter = Type Filter
22922295
notices.desc = Description
22932296
notices.op = Op.
22942297
notices.delete_success = The system notices have been deleted.

routers/admin/notice.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ func Notices(ctx *context.Context) {
2525
ctx.Data["PageIsAdmin"] = true
2626
ctx.Data["PageIsAdminNotices"] = true
2727

28-
total := models.CountNotices()
2928
page := ctx.QueryInt("page")
3029
if page <= 1 {
3130
page = 1
3231
}
3332

34-
notices, err := models.Notices(page, setting.UI.Admin.NoticePagingNum)
33+
typ := ctx.QueryInt("type")
34+
total, err := models.CountNotices(typ)
35+
if err != nil {
36+
ctx.ServerError("CountNotices", err)
37+
return
38+
}
39+
40+
notices, err := models.Notices(typ, page, setting.UI.Admin.NoticePagingNum)
3541
if err != nil {
3642
ctx.ServerError("Notices", err)
3743
return
@@ -40,7 +46,11 @@ func Notices(ctx *context.Context) {
4046

4147
ctx.Data["Total"] = total
4248

43-
ctx.Data["Page"] = context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
49+
p := context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
50+
ctx.Data["Page"] = p
51+
52+
ctx.Data["NoticesType"] = typ
53+
p.AddParam(ctx, "type", "NoticesType")
4454

4555
ctx.HTML(200, tplNotices)
4656
}

templates/admin/notice.tmpl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
{{template "base/alert" .}}
66
<h4 class="ui top attached header">
77
{{.i18n.Tr "admin.notices.system_notice_list"}} ({{.i18n.Tr "admin.total" .Total}})
8+
<div class="ui floating dropdown small teal button">
9+
<span class="text">
10+
{{.i18n.Tr "admin.notices.type_filter"}} :
11+
{{if eq .NoticesType 1}}
12+
{{.i18n.Tr "admin.notices.type_1"}}
13+
{{else if eq .NoticesType 2}}
14+
{{.i18n.Tr "admin.notices.type_2"}}
15+
{{else if eq .NoticesType 3}}
16+
{{.i18n.Tr "admin.notices.type_3"}}
17+
{{else}}
18+
{{.i18n.Tr "admin.notices.type_all"}}
19+
{{end}}
20+
</span>
21+
<div class="menu">
22+
<a class="item" href="{{$.Link}}?type=1">{{.i18n.Tr "admin.notices.type_1"}}</a>
23+
<a class="item" href="{{$.Link}}?type=2">{{.i18n.Tr "admin.notices.type_2"}}</a>
24+
<a class="item" href="{{$.Link}}?type=3">{{.i18n.Tr "admin.notices.type_3"}}</a>
25+
<a class="item" href="{{$.Link}}">{{.i18n.Tr "admin.notices.type_all"}}</a>
26+
</div>
27+
</div>
828
</h4>
929
<div class="ui attached table segment">
1030
<table id="notice-table" class="ui very basic select selectable table">

0 commit comments

Comments
 (0)