package repo import ( "sort" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" ) const ( tplIndex base.TplName = "repo/datasets/index" ) // MustEnableDataset check if repository enable internal dataset func MustEnableDataset(ctx *context.Context) { if !ctx.Repo.CanRead(models.UnitTypeDatasets) { ctx.NotFound("MustEnableDataset", nil) return } } func filterPrivateAttachments(ctx *context.Context, list []*models.Attachment) []*models.Attachment { if ctx.Repo.CanWrite(models.UnitTypeDatasets) { return list } else { var publicList []*models.Attachment for _, attach := range list { if !attach.IsPrivate { publicList = append(publicList, attach) } } return publicList } } func DatasetIndex(ctx *context.Context) { MustEnableDataset(ctx) repo := ctx.Repo.Repository dataset, err := models.GetDatasetByRepo(repo) if err != nil { ctx.NotFound("GetDatasetByRepo", err) return } err = models.GetDatasetAttachments(ctx.QueryInt("type"), dataset) if err != nil { ctx.ServerError("GetDatasetAttachments", err) return } attachments := filterPrivateAttachments(ctx, dataset.Attachments) ctx.Data["SortType"] = ctx.Query("sort") switch ctx.Query("sort") { case "newest": sort.Slice(attachments, func(i, j int) bool { return attachments[i].CreatedUnix > attachments[j].CreatedUnix }) case "oldest": sort.Slice(attachments, func(i, j int) bool { return attachments[i].CreatedUnix < attachments[j].CreatedUnix }) default: ctx.Data["SortType"] = "newest" sort.Slice(attachments, func(i, j int) bool { return attachments[i].CreatedUnix > attachments[j].CreatedUnix }) } ctx.Data["PageIsDataset"] = true ctx.Data["Title"] = ctx.Tr("dataset.show_dataset") ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets" ctx.Data["dataset"] = dataset ctx.Data["Attachments"] = attachments ctx.Data["IsOwner"] = true ctx.Data["StoreType"] = setting.Attachment.StoreType ctx.Data["Type"] = ctx.QueryInt("type") renderAttachmentSettings(ctx) ctx.HTML(200, tplIndex) } func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) { ctx.Data["PageIsDataset"] = true ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset") rel, err := models.GetDatasetByID(form.ID) ctx.Data["dataset"] = rel if err != nil { ctx.ServerError("GetDataset", err) return } if ctx.HasError() { ctx.Data["Error"] = true ctx.HTML(200, tplIndex) return } rel.Title = form.Title rel.Description = form.Description rel.Category = form.Category rel.Task = form.Task rel.License = form.License if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil { ctx.Data["Error"] = true ctx.HTML(200, tplIndex) log.Error("%v", err) } ctx.Redirect(ctx.Repo.RepoLink + "/datasets") }