gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package admin
  5. import (
  6. "net/http"
  7. "strconv"
  8. "code.gitea.io/gitea/models/db"
  9. system_model "code.gitea.io/gitea/models/system"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/templates"
  13. "code.gitea.io/gitea/services/context"
  14. )
  15. const (
  16. tplNotices templates.TplName = "admin/notice"
  17. )
  18. // Notices show notices for admin
  19. func Notices(ctx *context.Context) {
  20. ctx.Data["Title"] = ctx.Tr("admin.notices")
  21. ctx.Data["PageIsAdminNotices"] = true
  22. total := system_model.CountNotices(ctx)
  23. page := max(ctx.FormInt("page"), 1)
  24. notices, err := system_model.Notices(ctx, page, setting.UI.Admin.NoticePagingNum)
  25. if err != nil {
  26. ctx.ServerError("Notices", err)
  27. return
  28. }
  29. ctx.Data["Notices"] = notices
  30. ctx.Data["Total"] = total
  31. ctx.Data["Page"] = context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
  32. ctx.HTML(http.StatusOK, tplNotices)
  33. }
  34. // DeleteNotices delete the specific notices
  35. func DeleteNotices(ctx *context.Context) {
  36. strs := ctx.FormStrings("ids[]")
  37. ids := make([]int64, 0, len(strs))
  38. for i := range strs {
  39. id, _ := strconv.ParseInt(strs[i], 10, 64)
  40. if id > 0 {
  41. ids = append(ids, id)
  42. }
  43. }
  44. if err := db.DeleteByIDs[system_model.Notice](ctx, ids...); err != nil {
  45. ctx.Flash.Error("DeleteNoticesByIDs: " + err.Error())
  46. ctx.Status(http.StatusInternalServerError)
  47. } else {
  48. ctx.Flash.Success(ctx.Tr("admin.notices.delete_success"))
  49. ctx.Status(http.StatusOK)
  50. }
  51. }
  52. // EmptyNotices delete all the notices
  53. func EmptyNotices(ctx *context.Context) {
  54. if err := system_model.DeleteNotices(ctx, 0, 0); err != nil {
  55. ctx.ServerError("DeleteNotices", err)
  56. return
  57. }
  58. log.Trace("System notices deleted by admin (%s): [start: %d]", ctx.Doer.Name, 0)
  59. ctx.Flash.Success(ctx.Tr("admin.notices.delete_success"))
  60. ctx.Redirect(setting.AppSubURL + "/-/admin/notices")
  61. }