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 4.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package repo
  2. import (
  3. "sort"
  4. "code.gitea.io/gitea/models"
  5. "code.gitea.io/gitea/modules/auth"
  6. "code.gitea.io/gitea/modules/base"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  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 QueryDataSet(ctx *context.Context) []*models.Attachment {
  57. repo := ctx.Repo.Repository
  58. dataset, err := models.GetDatasetByRepo(repo)
  59. if err != nil {
  60. log.Error("zou not found dataset 1")
  61. ctx.NotFound("GetDatasetByRepo", err)
  62. return nil
  63. }
  64. if ctx.Query("type") == "" {
  65. log.Error("zou not found type 2")
  66. ctx.NotFound("type error", nil)
  67. return nil
  68. }
  69. err = models.GetDatasetAttachments(ctx.QueryInt("type"), ctx.IsSigned, ctx.User, dataset)
  70. if err != nil {
  71. ctx.ServerError("GetDatasetAttachments", err)
  72. return nil
  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. return attachments
  92. }
  93. func DatasetIndex(ctx *context.Context) {
  94. log.Info("dataset index 1")
  95. MustEnableDataset(ctx)
  96. repo := ctx.Repo.Repository
  97. dataset, err := models.GetDatasetByRepo(repo)
  98. if err != nil {
  99. log.Error("query dataset, not found repo.")
  100. ctx.NotFound("GetDatasetByRepo", err)
  101. return
  102. }
  103. if ctx.Query("type") == "" {
  104. log.Error("query dataset, not found param type")
  105. ctx.NotFound("type error", nil)
  106. return
  107. }
  108. err = models.GetDatasetAttachments(ctx.QueryInt("type"), ctx.IsSigned, ctx.User, dataset)
  109. if err != nil {
  110. ctx.ServerError("GetDatasetAttachments", err)
  111. return
  112. }
  113. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  114. ctx.Data["SortType"] = ctx.Query("sort")
  115. switch ctx.Query("sort") {
  116. case "newest":
  117. sort.Slice(attachments, func(i, j int) bool {
  118. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  119. })
  120. case "oldest":
  121. sort.Slice(attachments, func(i, j int) bool {
  122. return attachments[i].CreatedUnix < attachments[j].CreatedUnix
  123. })
  124. default:
  125. ctx.Data["SortType"] = "newest"
  126. sort.Slice(attachments, func(i, j int) bool {
  127. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  128. })
  129. }
  130. ctx.Data["PageIsDataset"] = true
  131. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  132. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  133. ctx.Data["dataset"] = dataset
  134. ctx.Data["Attachments"] = attachments
  135. ctx.Data["IsOwner"] = true
  136. ctx.Data["StoreType"] = setting.Attachment.StoreType
  137. ctx.Data["Type"] = ctx.QueryInt("type")
  138. renderAttachmentSettings(ctx)
  139. ctx.HTML(200, tplIndex)
  140. }
  141. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  142. ctx.Data["PageIsDataset"] = true
  143. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  144. rel, err := models.GetDatasetByID(form.ID)
  145. ctx.Data["dataset"] = rel
  146. if err != nil {
  147. ctx.ServerError("GetDataset", err)
  148. return
  149. }
  150. if ctx.HasError() {
  151. ctx.Data["Error"] = true
  152. ctx.HTML(200, tplIndex)
  153. return
  154. }
  155. rel.Title = form.Title
  156. rel.Description = form.Description
  157. rel.Category = form.Category
  158. rel.Task = form.Task
  159. rel.License = form.License
  160. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  161. ctx.Data["Error"] = true
  162. ctx.HTML(200, tplIndex)
  163. log.Error("%v", err)
  164. }
  165. ctx.Redirect(ctx.Repo.RepoLink + "/datasets?type=" + form.Type)
  166. }