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.4 kB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package repo
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/auth"
  5. "code.gitea.io/gitea/modules/base"
  6. "code.gitea.io/gitea/modules/context"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/setting"
  9. "sort"
  10. )
  11. const (
  12. tplIndex base.TplName = "repo/datasets/index"
  13. )
  14. // MustEnableDataset check if repository enable internal dataset
  15. func MustEnableDataset(ctx *context.Context) {
  16. if !ctx.Repo.CanRead(models.UnitTypeDatasets) {
  17. ctx.NotFound("MustEnableDataset", nil)
  18. return
  19. }
  20. }
  21. func newFilterPrivateAttachments(ctx *context.Context, list []*models.Attachment, repo *models.Repository) []*models.Attachment {
  22. if ctx.Repo.CanWrite(models.UnitTypeDatasets) {
  23. log.Info("can write.")
  24. return list
  25. } else {
  26. var publicList []*models.Attachment
  27. for _, attach := range list {
  28. if !attach.IsPrivate {
  29. publicList = append(publicList, attach)
  30. } else {
  31. if repo.Owner == nil {
  32. repo.GetOwner()
  33. }
  34. if repo.Owner.IsOrganization() {
  35. if repo.Owner.IsUserPartOfOrg(ctx.User.ID) {
  36. log.Info("user is member of org.")
  37. publicList = append(publicList, attach)
  38. }
  39. }
  40. }
  41. }
  42. return publicList
  43. }
  44. }
  45. func DatasetIndex(ctx *context.Context) {
  46. MustEnableDataset(ctx)
  47. repo := ctx.Repo.Repository
  48. dataset, err := models.GetDatasetByRepo(repo)
  49. if err != nil {
  50. ctx.NotFound("GetDatasetByRepo", err)
  51. return
  52. }
  53. if ctx.Query("type") == "" {
  54. log.Error("not found param type")
  55. ctx.NotFound("type error", nil)
  56. return
  57. }
  58. err = models.GetDatasetAttachments(ctx.QueryInt("type"), dataset)
  59. if err != nil {
  60. ctx.ServerError("GetDatasetAttachments", err)
  61. return
  62. }
  63. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  64. ctx.Data["SortType"] = ctx.Query("sort")
  65. switch ctx.Query("sort") {
  66. case "newest":
  67. sort.Slice(attachments, func(i, j int) bool {
  68. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  69. })
  70. case "oldest":
  71. sort.Slice(attachments, func(i, j int) bool {
  72. return attachments[i].CreatedUnix < attachments[j].CreatedUnix
  73. })
  74. default:
  75. ctx.Data["SortType"] = "newest"
  76. sort.Slice(attachments, func(i, j int) bool {
  77. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  78. })
  79. }
  80. ctx.Data["PageIsDataset"] = true
  81. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  82. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  83. ctx.Data["dataset"] = dataset
  84. ctx.Data["Attachments"] = attachments
  85. ctx.Data["IsOwner"] = true
  86. ctx.Data["StoreType"] = setting.Attachment.StoreType
  87. ctx.Data["Type"] = ctx.QueryInt("type")
  88. renderAttachmentSettings(ctx)
  89. ctx.HTML(200, tplIndex)
  90. }
  91. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  92. ctx.Data["PageIsDataset"] = true
  93. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  94. rel, err := models.GetDatasetByID(form.ID)
  95. ctx.Data["dataset"] = rel
  96. if err != nil {
  97. ctx.ServerError("GetDataset", err)
  98. return
  99. }
  100. if ctx.HasError() {
  101. ctx.Data["Error"] = true
  102. ctx.HTML(200, tplIndex)
  103. return
  104. }
  105. rel.Title = form.Title
  106. rel.Description = form.Description
  107. rel.Category = form.Category
  108. rel.Task = form.Task
  109. rel.License = form.License
  110. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  111. ctx.Data["Error"] = true
  112. ctx.HTML(200, tplIndex)
  113. log.Error("%v", err)
  114. }
  115. ctx.Redirect(ctx.Repo.RepoLink + "/datasets?type=" + form.Type)
  116. }