You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset.go 3.7 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package admin
  2. import (
  3. "code.gitea.io/gitea/modules/notification"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const (
  14. tplDatasets base.TplName = "admin/dataset/list"
  15. )
  16. func Datasets(ctx *context.Context) {
  17. ctx.Data["Title"] = ctx.Tr("admin.datasets")
  18. ctx.Data["PageIsAdmin"] = true
  19. ctx.Data["PageIsAdminDatasets"] = true
  20. page := ctx.QueryInt("page")
  21. if page <= 0 {
  22. page = 1
  23. }
  24. var (
  25. datasets []*models.Dataset
  26. count int64
  27. err error
  28. orderBy models.SearchOrderBy
  29. )
  30. ctx.Data["SortType"] = ctx.Query("sort")
  31. switch ctx.Query("sort") {
  32. case "newest":
  33. orderBy = models.SearchOrderByNewest
  34. case "oldest":
  35. orderBy = models.SearchOrderByOldest
  36. case "recentupdate":
  37. orderBy = models.SearchOrderByRecentUpdated
  38. case "leastupdate":
  39. orderBy = models.SearchOrderByLeastUpdated
  40. case "reversealphabetically":
  41. orderBy = models.SearchOrderByAlphabeticallyReverse
  42. case "alphabetically":
  43. orderBy = models.SearchOrderByAlphabetically
  44. case "reversesize":
  45. orderBy = models.SearchOrderBySizeReverse
  46. case "size":
  47. orderBy = models.SearchOrderBySize
  48. case "downloadtimes":
  49. orderBy = models.SearchOrderByDownloadTimes
  50. case "moststars":
  51. orderBy = models.SearchOrderByStarsReverse
  52. case "feweststars":
  53. orderBy = models.SearchOrderByStars
  54. case "mostforks":
  55. orderBy = models.SearchOrderByForksReverse
  56. case "fewestforks":
  57. orderBy = models.SearchOrderByForks
  58. case "mostusecount":
  59. orderBy = models.SearchOrderByUseCountReverse
  60. case "fewestusecount":
  61. orderBy = models.SearchOrderByUseCount
  62. default:
  63. ctx.Data["SortType"] = "recentupdate"
  64. orderBy = models.SearchOrderByRecentUpdated
  65. }
  66. keyword := strings.Trim(ctx.Query("q"), " ")
  67. datasets, count, err = models.SearchDataset(&models.SearchDatasetOptions{
  68. ListOptions: models.ListOptions{
  69. Page: page,
  70. PageSize: setting.UI.ExplorePagingNum,
  71. },
  72. Keyword: keyword,
  73. RecommendOnly: ctx.QueryBool("recommend"),
  74. CloudBrainType: -1,
  75. SearchOrderBy: orderBy,
  76. })
  77. if err != nil {
  78. ctx.ServerError("SearchDataset", err)
  79. return
  80. }
  81. ctx.Data["Keyword"] = keyword
  82. ctx.Data["Total"] = count
  83. ctx.Data["Datasets"] = datasets
  84. ctx.Data["Recommend"] = ctx.QueryBool("recommend")
  85. pager := context.NewPagination(int(count), setting.UI.ExplorePagingNum, page, 5)
  86. pager.SetDefaultParams(ctx)
  87. ctx.Data["Page"] = pager
  88. ctx.HTML(200, tplDatasets)
  89. }
  90. func DatasetAction(ctx *context.Context) {
  91. var err error
  92. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  93. switch ctx.Params(":action") {
  94. case "recommend":
  95. err = models.RecommendDataset(datasetId, true)
  96. case "unrecommend":
  97. err = models.RecommendDataset(datasetId, false)
  98. }
  99. if err != nil {
  100. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("repo.star_fail", ctx.Params(":action"))))
  101. } else {
  102. d, _ := models.GetDatasetByID(datasetId)
  103. notification.NotifyDatasetRecommend(ctx.User, d, ctx.Params(":action"))
  104. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  105. }
  106. }
  107. func DeleteDataset(ctx *context.Context) {
  108. dataset, err := models.GetDatasetByID(ctx.QueryInt64("id"))
  109. if err != nil {
  110. ctx.ServerError("GetDatasetByID", err)
  111. return
  112. }
  113. if err := models.DeleteDataset(dataset.ID, ctx.User.ID); err != nil {
  114. ctx.ServerError("DeleteDataset", err)
  115. return
  116. }
  117. log.Trace("DeleteDataset deleted: %s", dataset.ID)
  118. ctx.Flash.Success(ctx.Tr("dataset.settings.deletion_success"))
  119. ctx.JSON(200, map[string]interface{}{
  120. "redirect": setting.AppSubURL + "/admin/datasets?page=" + ctx.Query("page") + "&sort=" + ctx.Query("sort"),
  121. })
  122. }