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

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package repo
  2. import (
  3. "sort"
  4. "code.gitea.io/gitea/modules/setting"
  5. "code.gitea.io/gitea/models"
  6. "code.gitea.io/gitea/modules/auth"
  7. "code.gitea.io/gitea/modules/base"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  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 DatasetIndex(ctx *context.Context) {
  35. MustEnableDataset(ctx)
  36. repo := ctx.Repo.Repository
  37. dataset, err := models.GetDatasetByRepo(repo)
  38. if err != nil {
  39. ctx.NotFound("GetDatasetByRepo", err)
  40. return
  41. }
  42. err = models.GetDatasetAttachments(ctx.QueryInt("type"), dataset)
  43. if err != nil {
  44. ctx.ServerError("GetDatasetAttachments", err)
  45. return
  46. }
  47. attachments := filterPrivateAttachments(ctx, dataset.Attachments)
  48. ctx.Data["SortType"] = ctx.Query("sort")
  49. switch ctx.Query("sort") {
  50. case "newest":
  51. sort.Slice(attachments, func(i, j int) bool {
  52. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  53. })
  54. case "oldest":
  55. sort.Slice(attachments, func(i, j int) bool {
  56. return attachments[i].CreatedUnix < attachments[j].CreatedUnix
  57. })
  58. default:
  59. ctx.Data["SortType"] = "newest"
  60. sort.Slice(attachments, func(i, j int) bool {
  61. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  62. })
  63. }
  64. ctx.Data["PageIsDataset"] = true
  65. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  66. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  67. ctx.Data["dataset"] = dataset
  68. ctx.Data["Attachments"] = attachments
  69. ctx.Data["IsOwner"] = true
  70. ctx.Data["StoreType"] = setting.Attachment.StoreType
  71. ctx.Data["Type"] = ctx.QueryInt("type")
  72. renderAttachmentSettings(ctx)
  73. ctx.HTML(200, tplIndex)
  74. }
  75. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  76. ctx.Data["PageIsDataset"] = true
  77. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  78. rel, err := models.GetDatasetByID(form.ID)
  79. ctx.Data["dataset"] = rel
  80. if err != nil {
  81. ctx.ServerError("GetDataset", err)
  82. return
  83. }
  84. if ctx.HasError() {
  85. ctx.Data["Error"] = true
  86. ctx.HTML(200, tplIndex)
  87. return
  88. }
  89. rel.Title = form.Title
  90. rel.Description = form.Description
  91. rel.Category = form.Category
  92. rel.Task = form.Task
  93. rel.License = form.License
  94. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  95. ctx.Data["Error"] = true
  96. ctx.HTML(200, tplIndex)
  97. log.Error("%v", err)
  98. }
  99. ctx.Redirect(ctx.Repo.RepoLink + "/datasets")
  100. }