admin.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. "time"
  10. "github.com/Unknwon/com"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. )
  14. type NoticeType int
  15. const (
  16. NOTICE_REPOSITORY NoticeType = iota + 1
  17. )
  18. // Notice represents a system notice for admin.
  19. type Notice struct {
  20. ID int64 `xorm:"pk autoincr"`
  21. Type NoticeType
  22. Description string `xorm:"TEXT"`
  23. Created time.Time `xorm:"CREATED"`
  24. }
  25. // TrStr returns a translation format string.
  26. func (n *Notice) TrStr() string {
  27. return "admin.notices.type_" + com.ToStr(n.Type)
  28. }
  29. // CreateNotice creates new system notice.
  30. func CreateNotice(tp NoticeType, desc string) error {
  31. n := &Notice{
  32. Type: tp,
  33. Description: desc,
  34. }
  35. _, err := x.Insert(n)
  36. return err
  37. }
  38. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  39. func CreateRepositoryNotice(desc string) error {
  40. return CreateNotice(NOTICE_REPOSITORY, desc)
  41. }
  42. // RemoveAllWithNotice removes all directories in given path and
  43. // creates a system notice when error occurs.
  44. func RemoveAllWithNotice(title, path string) {
  45. if err := os.RemoveAll(path); err != nil {
  46. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  47. log.Warn(desc)
  48. if err = CreateRepositoryNotice(desc); err != nil {
  49. log.Error(4, "CreateRepositoryNotice: %v", err)
  50. }
  51. }
  52. }
  53. // CountNotices returns number of notices.
  54. func CountNotices() int64 {
  55. count, _ := x.Count(new(Notice))
  56. return count
  57. }
  58. // Notices returns number of notices in given page.
  59. func Notices(page, pageSize int) ([]*Notice, error) {
  60. notices := make([]*Notice, 0, pageSize)
  61. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  62. }
  63. // DeleteNotice deletes a system notice by given ID.
  64. func DeleteNotice(id int64) error {
  65. _, err := x.Id(id).Delete(new(Notice))
  66. return err
  67. }
  68. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  69. func DeleteNotices(start, end int64) error {
  70. sess := x.Where("id >= ?", start)
  71. if end > 0 {
  72. sess.And("id <= ?", end)
  73. }
  74. _, err := sess.Delete(new(Notice))
  75. return err
  76. }
  77. // DeleteNoticesByIDs deletes notices by given IDs.
  78. func DeleteNoticesByIDs(ids []int64) error {
  79. if len(ids) == 0 {
  80. return nil
  81. }
  82. _, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  83. return err
  84. }