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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 DatasetIndex(ctx *context.Context) {
  21. MustEnableDataset(ctx)
  22. repo := ctx.Repo.Repository
  23. dataset, err := models.GetDatasetByRepo(repo)
  24. if err != nil {
  25. ctx.NotFound("GetDatasetByRepo", err)
  26. return
  27. }
  28. err = models.GetDatasetAttachments(dataset)
  29. if err != nil {
  30. ctx.ServerError("GetDatasetAttachments", err)
  31. return
  32. }
  33. attachments := dataset.Attachments
  34. ctx.Data["SortType"] = ctx.Query("sort")
  35. switch ctx.Query("sort") {
  36. case "newest":
  37. sort.Slice(attachments, func(i, j int) bool {
  38. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  39. })
  40. case "oldest":
  41. sort.Slice(attachments, func(i, j int) bool {
  42. return attachments[i].CreatedUnix < attachments[j].CreatedUnix
  43. })
  44. default:
  45. ctx.Data["SortType"] = "newest"
  46. sort.Slice(attachments, func(i, j int) bool {
  47. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  48. })
  49. }
  50. ctx.Data["PageIsDataset"] = true
  51. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  52. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  53. ctx.Data["dataset"] = dataset
  54. ctx.Data["Attachments"] = attachments
  55. ctx.Data["IsOwner"] = true
  56. ctx.HTML(200, tplIndex)
  57. }
  58. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  59. ctx.Data["PageIsDataset"] = true
  60. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  61. rel, err := models.GetDatasetByID(form.ID)
  62. ctx.Data["dataset"] = rel
  63. if err != nil {
  64. ctx.ServerError("GetDataset", err)
  65. return
  66. }
  67. if ctx.HasError() {
  68. ctx.Data["Error"] = true
  69. ctx.HTML(200, tplIndex)
  70. return
  71. }
  72. rel.Title = form.Title
  73. rel.Description = form.Description
  74. rel.Category = form.Category
  75. rel.Task = form.Task
  76. rel.License = form.License
  77. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  78. ctx.Data["Error"] = true
  79. ctx.HTML(200, tplIndex)
  80. log.Error("%v", err)
  81. }
  82. ctx.Redirect(ctx.Repo.RepoLink + "/datasets")
  83. }