Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type filter to System Notices view #11909

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions models/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ type NoticeType int
const (
//NoticeRepository type
NoticeRepository NoticeType = iota + 1
// NoticeTask type
NoticeTask
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like we have a new column named status but not put all in type

Copy link
Member Author

@a1012112796 a1012112796 Jun 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure whether it's a good idea because it will result in big changs and it's not usefull for NoticeRepository now. 🤔

// NoticeTaskSuccess type
NoticeTaskSuccess
// NoticeTaskFail type
NoticeTaskFail
)

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

// CreateTaskNotice creates new system notice with type NoticeTaskSuccess or NoticeTaskFail.
func CreateTaskNotice(isSuccess bool, desc string, args ...interface{}) (err error) {
if isSuccess {
err = createNotice(x, NoticeTaskSuccess, desc, args...)
} else {
err = createNotice(x, NoticeTaskFail, desc, args...)
}

return
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
}

// RemoveAllWithNotice removes all directories in given path and
// creates a system notice when error occurs.
func RemoveAllWithNotice(title, path string) {
Expand All @@ -76,18 +89,32 @@ func removeAllWithNotice(e Engine, title, path string) {
}

// CountNotices returns number of notices.
func CountNotices() int64 {
count, _ := x.Count(new(Notice))
return count
func CountNotices(typ int) (count int64, err error) {
if typ >= 1 {
count, err = x.Where("type = ?", typ).Count(new(Notice))
} else {
count, err = x.Count(new(Notice))
}
return
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
}

// Notices returns notices in given page.
func Notices(page, pageSize int) ([]*Notice, error) {
notices := make([]*Notice, 0, pageSize)
return notices, x.
Limit(pageSize, (page-1)*pageSize).
Desc("id").
Find(&notices)
func Notices(typ, page, pageSize int) (notices []*Notice, err error) {
notices = make([]*Notice, 0, pageSize)

if typ >= 1 {
err = x.Where("type = ?", typ).
Limit(pageSize, (page-1)*pageSize).
Desc("id").
Find(&notices)
} else {
err = x.
Limit(pageSize, (page-1)*pageSize).
Desc("id").
Find(&notices)
}

return notices, err
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
}

// DeleteNotice deletes a system notice by given ID.
Expand Down
17 changes: 14 additions & 3 deletions models/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,35 @@ func TestCreateRepositoryNotice(t *testing.T) {

func TestCountNotices(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.Equal(t, int64(3), CountNotices())
num, err := CountNotices(0)
assert.NoError(t, err)
assert.Equal(t, int64(3), num)
num, err = CountNotices(2)
assert.NoError(t, err)
assert.Equal(t, int64(1), num)
}

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

notices, err := Notices(1, 2)
notices, err := Notices(0, 1, 2)
assert.NoError(t, err)
if assert.Len(t, notices, 2) {
assert.Equal(t, int64(3), notices[0].ID)
assert.Equal(t, int64(2), notices[1].ID)
}

notices, err = Notices(2, 2)
notices, err = Notices(0, 2, 2)
assert.NoError(t, err)
if assert.Len(t, notices, 1) {
assert.Equal(t, int64(1), notices[0].ID)
}

notices, err = Notices(2, 1, 2)
assert.NoError(t, err)
if assert.Len(t, notices, 1) {
assert.Equal(t, int64(3), notices[0].ID)
}
}

func TestDeleteNotice(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion models/fixtures/notice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

-
id: 3
type: 1 # NoticeRepository
type: 2 # NoticeTaskSuccess
description: description3
6 changes: 3 additions & 3 deletions modules/cron/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ func (t *Task) RunWithUser(doer *models.User, config Config) {
if err := t.fun(ctx, doer, config); err != nil {
if models.IsErrCancelled(err) {
message := err.(models.ErrCancelled).Message
if err := models.CreateNotice(models.NoticeTask, config.FormatMessage(t.Name, "aborted", doer, message)); err != nil {
if err := models.CreateTaskNotice(false, config.FormatMessage(t.Name, "aborted", doer, message)); err != nil {
log.Error("CreateNotice: %v", err)
}
return
}
if err := models.CreateNotice(models.NoticeTask, config.FormatMessage(t.Name, "error", doer, err)); err != nil {
if err := models.CreateTaskNotice(false, config.FormatMessage(t.Name, "error", doer, err)); err != nil {
log.Error("CreateNotice: %v", err)
}
return
}
if err := models.CreateNotice(models.NoticeTask, config.FormatMessage(t.Name, "finished", doer)); err != nil {
if err := models.CreateTaskNotice(true, config.FormatMessage(t.Name, "finished", doer)); err != nil {
log.Error("CreateNotice: %v", err)
}
})
Expand Down
5 changes: 4 additions & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,10 @@ notices.delete_selected = Delete Selected
notices.delete_all = Delete All Notices
notices.type = Type
notices.type_1 = Repository
notices.type_2 = Task
notices.type_2 = Success Task
notices.type_3 = Fail Task
notices.type_all = All Type
notices.type_filter = Type Filter
notices.desc = Description
notices.op = Op.
notices.delete_success = The system notices have been deleted.
Expand Down
16 changes: 13 additions & 3 deletions routers/admin/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ func Notices(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminNotices"] = true

total := models.CountNotices()
page := ctx.QueryInt("page")
if page <= 1 {
page = 1
}

notices, err := models.Notices(page, setting.UI.Admin.NoticePagingNum)
typ := ctx.QueryInt("type")
total, err := models.CountNotices(typ)
if err != nil {
ctx.ServerError("CountNotices", err)
return
}

notices, err := models.Notices(typ, page, setting.UI.Admin.NoticePagingNum)
if err != nil {
ctx.ServerError("Notices", err)
return
Expand All @@ -40,7 +46,11 @@ func Notices(ctx *context.Context) {

ctx.Data["Total"] = total

ctx.Data["Page"] = context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
p := context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
ctx.Data["Page"] = p

ctx.Data["NoticesType"] = typ
p.AddParam(ctx, "type", "NoticesType")

ctx.HTML(200, tplNotices)
}
Expand Down
20 changes: 20 additions & 0 deletions templates/admin/notice.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
{{template "base/alert" .}}
<h4 class="ui top attached header">
{{.i18n.Tr "admin.notices.system_notice_list"}} ({{.i18n.Tr "admin.total" .Total}})
<div class="ui floating dropdown small teal button">
<span class="text">
{{.i18n.Tr "admin.notices.type_filter"}} :
{{if eq .NoticesType 1}}
{{.i18n.Tr "admin.notices.type_1"}}
{{else if eq .NoticesType 2}}
{{.i18n.Tr "admin.notices.type_2"}}
{{else if eq .NoticesType 3}}
{{.i18n.Tr "admin.notices.type_3"}}
{{else}}
{{.i18n.Tr "admin.notices.type_all"}}
{{end}}
</span>
<div class="menu">
<a class="item" href="{{$.Link}}?type=1">{{.i18n.Tr "admin.notices.type_1"}}</a>
<a class="item" href="{{$.Link}}?type=2">{{.i18n.Tr "admin.notices.type_2"}}</a>
<a class="item" href="{{$.Link}}?type=3">{{.i18n.Tr "admin.notices.type_3"}}</a>
<a class="item" href="{{$.Link}}">{{.i18n.Tr "admin.notices.type_all"}}</a>
</div>
</div>
</h4>
<div class="ui attached table segment">
<table id="notice-table" class="ui very basic select selectable table">
Expand Down