From f2ba80c271b4c65ad1a9425c470083456183cc10 Mon Sep 17 00:00:00 2001 From: colorfulberry Date: Fri, 22 May 2020 14:32:07 +0800 Subject: [PATCH] feat: add dataset files --- models/attachment.go | 1 + models/dataset.go | 21 +++++++++++++++++++++ modules/auth/dataset.go | 1 + options/locale/locale_en-US.ini | 2 +- options/locale/locale_zh-CN.ini | 2 +- routers/dataset/dataset.go | 11 +++++++++++ 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index e4885d809..d985eaa7d 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -24,6 +24,7 @@ type Attachment struct { ID int64 `xorm:"pk autoincr"` UUID string `xorm:"uuid UNIQUE"` IssueID int64 `xorm:"INDEX"` + DatasetID int64 `xorm:"INDEX DEFAULT 0"` ReleaseID int64 `xorm:"INDEX"` UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added CommentID int64 diff --git a/models/dataset.go b/models/dataset.go index d03a38a79..0fdbea3e1 100644 --- a/models/dataset.go +++ b/models/dataset.go @@ -1,6 +1,8 @@ package models import ( + "fmt" + "code.gitea.io/gitea/modules/timeutil" ) @@ -29,3 +31,22 @@ func CreateDataset(dataset *Dataset) (err error) { return nil } + +// AddReleaseAttachments adds a release attachments +func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error) { + // Check attachments + attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs) + if err != nil { + return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err) + } + + for i := range attachments { + attachments[i].DatasetID = DatasetID + // No assign value could be 0, so ignore AllCols(). + if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil { + return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) + } + } + + return +} diff --git a/modules/auth/dataset.go b/modules/auth/dataset.go index eeed8b267..c894fdd28 100644 --- a/modules/auth/dataset.go +++ b/modules/auth/dataset.go @@ -13,6 +13,7 @@ type CreateDatasetForm struct { License string `binding:"Required;MaxSize(64)"` Task string `binding:"Required;MaxSize(64)"` ReleaseID int64 `xorm:"INDEX"` + Files []string } // Validate validates the fields diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a32f6b02c..c5530964a 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -621,7 +621,7 @@ email_notifications.disable = Disable Email Notifications email_notifications.submit = Set Email Preference [dataset] -title = Title +title = Name description = Description create_dataset = Create Dataset category = Category diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 58158ea77..5b2a6ed9e 100644 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -620,7 +620,7 @@ email_notifications.disable=停用邮件通知 email_notifications.submit=邮件通知设置 [dataset] -title = 主题 +title = 名称 description = 描述 create_dataset = 创建数据集 category = 分类 diff --git a/routers/dataset/dataset.go b/routers/dataset/dataset.go index d432c08d2..359189eda 100644 --- a/routers/dataset/dataset.go +++ b/routers/dataset/dataset.go @@ -72,6 +72,17 @@ func CreatePost(ctx *context.Context, form auth.CreateDatasetForm) { log.Error("%v", err) } + var attachmentUUIDs []string + if setting.Attachment.Enabled { + attachmentUUIDs = form.Files + } + + log.Error("%v", attachmentUUIDs) + + if err = models.AddDatasetAttachments(opts.ID, attachmentUUIDs); err != nil { + log.Error("%v", err) + } + if err == nil { log.Trace("Dataset created [%d]: %s/%s", opts.ID, ctxUser.Name, opts.Title) ctx.Redirect(setting.AppSubURL + "/datasets")