admin.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "os/exec"
  9. "strings"
  10. "time"
  11. "github.com/Unknwon/com"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. type NoticeType int
  17. const (
  18. NOTICE_REPOSITORY NoticeType = iota + 1
  19. )
  20. // Notice represents a system notice for admin.
  21. type Notice struct {
  22. ID int64 `xorm:"pk autoincr"`
  23. Type NoticeType
  24. Description string `xorm:"TEXT"`
  25. Created time.Time `xorm:"CREATED"`
  26. }
  27. // TrStr returns a translation format string.
  28. func (n *Notice) TrStr() string {
  29. return "admin.notices.type_" + com.ToStr(n.Type)
  30. }
  31. // CreateNotice creates new system notice.
  32. func CreateNotice(tp NoticeType, desc string) error {
  33. n := &Notice{
  34. Type: tp,
  35. Description: desc,
  36. }
  37. _, err := x.Insert(n)
  38. return err
  39. }
  40. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  41. func CreateRepositoryNotice(desc string) error {
  42. return CreateNotice(NOTICE_REPOSITORY, desc)
  43. }
  44. // RemoveAllWithNotice removes all directories in given path and
  45. // creates a system notice when error occurs.
  46. func RemoveAllWithNotice(title, path string) {
  47. var err error
  48. if setting.IsWindows {
  49. err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
  50. } else {
  51. err = os.RemoveAll(path)
  52. }
  53. if err != nil {
  54. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  55. log.Warn(desc)
  56. if err = CreateRepositoryNotice(desc); err != nil {
  57. log.Error(4, "CreateRepositoryNotice: %v", err)
  58. }
  59. }
  60. }
  61. // CountNotices returns number of notices.
  62. func CountNotices() int64 {
  63. count, _ := x.Count(new(Notice))
  64. return count
  65. }
  66. // Notices returns number of notices in given page.
  67. func Notices(page, pageSize int) ([]*Notice, error) {
  68. notices := make([]*Notice, 0, pageSize)
  69. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  70. }
  71. // DeleteNotice deletes a system notice by given ID.
  72. func DeleteNotice(id int64) error {
  73. _, err := x.Id(id).Delete(new(Notice))
  74. return err
  75. }
  76. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  77. func DeleteNotices(start, end int64) error {
  78. sess := x.Where("id >= ?", start)
  79. if end > 0 {
  80. sess.And("id <= ?", end)
  81. }
  82. _, err := sess.Delete(new(Notice))
  83. return err
  84. }
  85. // DeleteNoticesByIDs deletes notices by given IDs.
  86. func DeleteNoticesByIDs(ids []int64) error {
  87. if len(ids) == 0 {
  88. return nil
  89. }
  90. _, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  91. return err
  92. }