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

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