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