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

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