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

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