From 0a13387c1c73a635e0d1a521fa0ff283dc7931b3 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 1 Apr 2024 15:24:34 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=95=B0=E6=8D=AE=E9=9B=86=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 1 + app/models/project.rb | 1 + app/models/project_dataset.rb | 21 +++++++++++++++++++ app/models/project_unit.rb | 2 +- .../20240401030707_create_project_datasets.rb | 11 ++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 app/models/project_dataset.rb create mode 100644 db/migrate/20240401030707_create_project_datasets.rb diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index a4e369c1f..eb242475e 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -21,6 +21,7 @@ class ProjectsController < ApplicationController menu.append(menu_hash_by_name("issues")) if @project.has_menu_permission("issues") menu.append(menu_hash_by_name("pulls")) if @project.has_menu_permission("pulls") && @project.forge? menu.append(menu_hash_by_name("devops")) if @project.has_menu_permission("devops") && @project.forge? + menu.append(menu_hash_by_name("dataset")) if @project.has_menu_permission("dataset") && @project.forge? menu.append(menu_hash_by_name("versions")) if @project.has_menu_permission("versions") menu.append(menu_hash_by_name("wiki")) if @project.has_menu_permission("wiki") && @project.forge? menu.append(menu_hash_by_name("services")) if @project.has_menu_permission("services") && @project.forge? && (current_user.admin? || @project.member?(current_user.id)) diff --git a/app/models/project.rb b/app/models/project.rb index 34e981508..c2702fa01 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -137,6 +137,7 @@ class Project < ApplicationRecord has_many :project_topics, through: :project_topic_ralates has_many :commit_logs, dependent: :destroy has_many :daily_project_statistics, dependent: :destroy + has_one :project_dataset, dependent: :destroy after_create :incre_user_statistic, :incre_platform_statistic after_save :check_project_members before_save :set_invite_code, :reset_unmember_followed, :set_recommend_and_is_pinned, :reset_cache_data diff --git a/app/models/project_dataset.rb b/app/models/project_dataset.rb new file mode 100644 index 000000000..1de935529 --- /dev/null +++ b/app/models/project_dataset.rb @@ -0,0 +1,21 @@ +# == Schema Information +# +# Table name: project_datasets +# +# id :integer not null, primary key +# title :string(255) +# description :text(65535) +# project_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_project_datasets_on_project_id (project_id) +# + +class ProjectDataset < ApplicationRecord + + belongs_to :project + +end diff --git a/app/models/project_unit.rb b/app/models/project_unit.rb index 93e3668eb..8cf4ed6ab 100644 --- a/app/models/project_unit.rb +++ b/app/models/project_unit.rb @@ -17,7 +17,7 @@ class ProjectUnit < ApplicationRecord belongs_to :project - enum unit_type: {code: 1, issues: 2, pulls: 3, wiki:4, devops: 5, versions: 6, resources: 7, services: 8} + enum unit_type: {code: 1, issues: 2, pulls: 3, wiki:4, devops: 5, versions: 6, resources: 7, services: 8, dataset: 9} validates :unit_type, uniqueness: { scope: :project_id} diff --git a/db/migrate/20240401030707_create_project_datasets.rb b/db/migrate/20240401030707_create_project_datasets.rb new file mode 100644 index 000000000..7a2cfffcc --- /dev/null +++ b/db/migrate/20240401030707_create_project_datasets.rb @@ -0,0 +1,11 @@ +class CreateProjectDatasets < ActiveRecord::Migration[5.2] + def change + create_table :project_datasets do |t| + t.string :title + t.text :description + t.references :project + + t.timestamps + end + end +end From dbfbee78faf15a43dc8baa24201681541a4278d9 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 1 Apr 2024 17:23:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E9=83=A8=E5=88=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/projects/datasets_controller.rb | 38 +++++++++++++++++++ app/controllers/attachments_controller.rb | 3 ++ app/models/project_dataset.rb | 3 +- .../api/v1/attachments/_detail.json.jbuilder | 11 ++++++ .../attachments/_simple_detail.json.jbuilder | 5 ++- .../v1/projects/datasets/show.json.jbuilder | 4 ++ config/routes/api.rb | 1 + 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 app/controllers/api/v1/projects/datasets_controller.rb create mode 100644 app/views/api/v1/attachments/_detail.json.jbuilder create mode 100644 app/views/api/v1/projects/datasets/show.json.jbuilder diff --git a/app/controllers/api/v1/projects/datasets_controller.rb b/app/controllers/api/v1/projects/datasets_controller.rb new file mode 100644 index 000000000..a3d3a54b3 --- /dev/null +++ b/app/controllers/api/v1/projects/datasets_controller.rb @@ -0,0 +1,38 @@ +class Api::V1::Projects::DatasetsController < Api::V1::BaseController + before_action :require_public_and_member_above + before_action :find_dataset, only: [:update, :show] + + def create + return render_error('该项目下已存在数据集!') if @project.project_dataset.present? + @project_dataset = ProjectDataset.new(dataset_params.merge!(project_id: @project.id)) + if @project_dataset.save! + render_ok + else + render_error('创建数据集失败!') + end + end + + def update + @project_dataset.attributes = dataset_params + if @project_dataset.save! + render_ok + else + render_error("更新数据集失败!") + end + end + + def show + @attachments = @project_dataset.attachments.includes(:author) + end + + private + def dataset_params + params.permit(:title, :description) + end + + def find_dataset + @project_dataset = @project.project_dataset + return render_not_found unless @project_dataset.present? + end + +end \ No newline at end of file diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index ecc4760b5..46810007b 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -95,6 +95,9 @@ class AttachmentsController < ApplicationController @attachment.disk_directory = month_folder @attachment.cloud_url = remote_path @attachment.uuid = SecureRandom.uuid + @attachment.description = params[:description] + @attachment.container_id = params[:container_id] + @attachment.container_type = params[:container_type] @attachment.save! else logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" diff --git a/app/models/project_dataset.rb b/app/models/project_dataset.rb index 1de935529..a7e3f4c30 100644 --- a/app/models/project_dataset.rb +++ b/app/models/project_dataset.rb @@ -17,5 +17,6 @@ class ProjectDataset < ApplicationRecord belongs_to :project - + has_many :attachments, as: :container, dependent: :destroy + end diff --git a/app/views/api/v1/attachments/_detail.json.jbuilder b/app/views/api/v1/attachments/_detail.json.jbuilder new file mode 100644 index 000000000..7998812f4 --- /dev/null +++ b/app/views/api/v1/attachments/_detail.json.jbuilder @@ -0,0 +1,11 @@ +json.id attachment.uuid +json.title attachment.title +json.description attachment.description +json.filesize number_to_human_size(attachment.filesize) +json.is_pdf attachment.is_pdf? +json.url attachment.is_pdf? ? download_url(attachment,disposition:"inline") : download_url(attachment) +json.created_on attachment.created_on.strftime("%Y-%m-%d %H:%M:%S") +json.content_type attachment.content_type +json.creator do + json.partial! "api/v1/users/simple_user", locals: {user: attachment.author} +end \ No newline at end of file diff --git a/app/views/api/v1/attachments/_simple_detail.json.jbuilder b/app/views/api/v1/attachments/_simple_detail.json.jbuilder index 3d56eb82f..78e727035 100644 --- a/app/views/api/v1/attachments/_simple_detail.json.jbuilder +++ b/app/views/api/v1/attachments/_simple_detail.json.jbuilder @@ -1,7 +1,8 @@ -json.id attachment.id +json.id attachment.uuid json.title attachment.title +json.description attachment.description json.filesize number_to_human_size(attachment.filesize) json.is_pdf attachment.is_pdf? json.url attachment.is_pdf? ? download_url(attachment,disposition:"inline") : download_url(attachment) -json.created_on attachment.created_on.strftime("%Y-%m-%d %H:%M") +json.created_on attachment.created_on.strftime("%Y-%m-%d %H:%M:%S") json.content_type attachment.content_type diff --git a/app/views/api/v1/projects/datasets/show.json.jbuilder b/app/views/api/v1/projects/datasets/show.json.jbuilder new file mode 100644 index 000000000..fe4c74dcf --- /dev/null +++ b/app/views/api/v1/projects/datasets/show.json.jbuilder @@ -0,0 +1,4 @@ +json.(@project_dataset, :id, :title, :description) +json.attachments @attachments do |at| + json.partial! "api/v1/attachments/detail", locals: {attachment: at} +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index dde85fe3c..ed1ea9b42 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -78,6 +78,7 @@ defaults format: :json do # projects文件夹下的 scope module: :projects do + resource :dataset, only: [:create, :update, :show] resources :actions, module: 'actions' do collection do post :disable From 47c4af8ea10e7f223e26ea3c7311a9014191f0ed Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 3 Apr 2024 10:15:11 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E6=96=B0=E5=AD=97=E6=AE=B5license=5Fid?= =?UTF-8?q?=E5=92=8Cpaper=5Fcontent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/projects/datasets_controller.rb | 2 +- app/models/project_dataset.rb | 16 ++++++++++------ .../api/v1/projects/datasets/show.json.jbuilder | 3 ++- ...cense_and_paper_content_to_project_dataset.rb | 6 ++++++ 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20240403015938_add_license_and_paper_content_to_project_dataset.rb diff --git a/app/controllers/api/v1/projects/datasets_controller.rb b/app/controllers/api/v1/projects/datasets_controller.rb index a3d3a54b3..d890d1373 100644 --- a/app/controllers/api/v1/projects/datasets_controller.rb +++ b/app/controllers/api/v1/projects/datasets_controller.rb @@ -27,7 +27,7 @@ class Api::V1::Projects::DatasetsController < Api::V1::BaseController private def dataset_params - params.permit(:title, :description) + params.permit(:title, :description, :license_id, :paper_content) end def find_dataset diff --git a/app/models/project_dataset.rb b/app/models/project_dataset.rb index a7e3f4c30..ae4bb5789 100644 --- a/app/models/project_dataset.rb +++ b/app/models/project_dataset.rb @@ -2,21 +2,25 @@ # # Table name: project_datasets # -# id :integer not null, primary key -# title :string(255) -# description :text(65535) -# project_id :integer -# created_at :datetime not null -# updated_at :datetime not null +# id :integer not null, primary key +# title :string(255) +# description :text(65535) +# project_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# license_id :integer +# paper_content :text(65535) # # Indexes # +# index_project_datasets_on_license_id (license_id) # index_project_datasets_on_project_id (project_id) # class ProjectDataset < ApplicationRecord belongs_to :project + belongs_to :license, optional: true has_many :attachments, as: :container, dependent: :destroy end diff --git a/app/views/api/v1/projects/datasets/show.json.jbuilder b/app/views/api/v1/projects/datasets/show.json.jbuilder index fe4c74dcf..ac9f2d777 100644 --- a/app/views/api/v1/projects/datasets/show.json.jbuilder +++ b/app/views/api/v1/projects/datasets/show.json.jbuilder @@ -1,4 +1,5 @@ -json.(@project_dataset, :id, :title, :description) +json.(@project_dataset, :id, :title, :description, :license_id, :paper_content) +json.license_name @project_dataset&.license&.name json.attachments @attachments do |at| json.partial! "api/v1/attachments/detail", locals: {attachment: at} end \ No newline at end of file diff --git a/db/migrate/20240403015938_add_license_and_paper_content_to_project_dataset.rb b/db/migrate/20240403015938_add_license_and_paper_content_to_project_dataset.rb new file mode 100644 index 000000000..4a2a72a2d --- /dev/null +++ b/db/migrate/20240403015938_add_license_and_paper_content_to_project_dataset.rb @@ -0,0 +1,6 @@ +class AddLicenseAndPaperContentToProjectDataset < ActiveRecord::Migration[5.2] + def change + add_reference :project_datasets, :license + add_column :project_datasets, :paper_content, :text + end +end From 8ac95921954f3d265df072aee88ac4a80d77d94c Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 3 Apr 2024 15:34:49 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E8=AF=A6=E6=83=85has=5Fdataset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/projects_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index b86226454..93e2bde76 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -67,6 +67,7 @@ module ProjectsHelper jianmu_devops_url: jianmu_devops_url, cloud_ide_saas_url: cloud_ide_saas_url(user), open_blockchain: Site.has_blockchain? && project.use_blockchain, + has_dataset: project.project_dataset.present?, ignore_id: project.ignore_id }).compact From 7d560032b00aee10ef101911f0b21e6e112f6be0 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 3 Apr 2024 16:33:07 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E6=96=87=E4=BB=B6=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/projects/datasets_controller.rb | 2 +- app/views/api/v1/projects/datasets/show.json.jbuilder | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/projects/datasets_controller.rb b/app/controllers/api/v1/projects/datasets_controller.rb index d890d1373..b8cdf780c 100644 --- a/app/controllers/api/v1/projects/datasets_controller.rb +++ b/app/controllers/api/v1/projects/datasets_controller.rb @@ -22,7 +22,7 @@ class Api::V1::Projects::DatasetsController < Api::V1::BaseController end def show - @attachments = @project_dataset.attachments.includes(:author) + @attachments = kaminari_paginate(@project_dataset.attachments.includes(:author)) end private diff --git a/app/views/api/v1/projects/datasets/show.json.jbuilder b/app/views/api/v1/projects/datasets/show.json.jbuilder index ac9f2d777..6b18d8015 100644 --- a/app/views/api/v1/projects/datasets/show.json.jbuilder +++ b/app/views/api/v1/projects/datasets/show.json.jbuilder @@ -1,5 +1,6 @@ json.(@project_dataset, :id, :title, :description, :license_id, :paper_content) json.license_name @project_dataset&.license&.name +json.attachment_total_count @attachments.total_count json.attachments @attachments do |at| json.partial! "api/v1/attachments/detail", locals: {attachment: at} end \ No newline at end of file From 7440a79cc4ec91722074767915b4ec8c6787af63 Mon Sep 17 00:00:00 2001 From: yystopf Date: Sun, 7 Apr 2024 14:28:42 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9Aunit=E4=B8=8D?= =?UTF-8?q?=E5=8C=85=E5=90=ABdataset=E6=AD=A3=E7=A1=AE=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=AD=97=E6=AE=B5=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/base_controller.rb | 5 +++++ .../api/v1/projects/datasets_controller.rb | 14 +++++++++++++- app/forms/projects/datasets/create_form.rb | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 app/forms/projects/datasets/create_form.rb diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index bcb0c4e86..ea2266390 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -55,6 +55,11 @@ class Api::V1::BaseController < ApplicationController return render_forbidden if !current_user.admin? && !@project.operator?(current_user) && !(@project.fork_project.present? && @project.fork_project.operator?(current_user)) end + def require_member_above + @project = load_project + return render_forbidden if !current_user.admin? && !@project.member?(current_user) + end + # 具有对仓库的访问权限 def require_public_and_member_above @project = load_project diff --git a/app/controllers/api/v1/projects/datasets_controller.rb b/app/controllers/api/v1/projects/datasets_controller.rb index b8cdf780c..0065d529e 100644 --- a/app/controllers/api/v1/projects/datasets_controller.rb +++ b/app/controllers/api/v1/projects/datasets_controller.rb @@ -1,8 +1,10 @@ class Api::V1::Projects::DatasetsController < Api::V1::BaseController - before_action :require_public_and_member_above + before_action :require_member_above before_action :find_dataset, only: [:update, :show] + before_action :check_menu_authorize def create + ::Projects::Datasets::CreateForm.new(dataset_params).validate! return render_error('该项目下已存在数据集!') if @project.project_dataset.present? @project_dataset = ProjectDataset.new(dataset_params.merge!(project_id: @project.id)) if @project_dataset.save! @@ -10,15 +12,22 @@ class Api::V1::Projects::DatasetsController < Api::V1::BaseController else render_error('创建数据集失败!') end + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) end def update + ::Projects::Datasets::CreateForm.new(dataset_params).validate! @project_dataset.attributes = dataset_params if @project_dataset.save! render_ok else render_error("更新数据集失败!") end + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) end def show @@ -35,4 +44,7 @@ class Api::V1::Projects::DatasetsController < Api::V1::BaseController return render_not_found unless @project_dataset.present? end + def check_menu_authorize + return render_not_found unless @project.has_menu_permission("dataset") + end end \ No newline at end of file diff --git a/app/forms/projects/datasets/create_form.rb b/app/forms/projects/datasets/create_form.rb new file mode 100644 index 000000000..c812ee17e --- /dev/null +++ b/app/forms/projects/datasets/create_form.rb @@ -0,0 +1,15 @@ +class Projects::Datasets::CreateForm < BaseForm + attr_accessor :title, :description, :license_id, :paper_content + + + validates :title, presence: true, length: { maximum: 100 } + validates :description, presence: true, length: { maximum: 500 } + validates :paper_content, length: { maximum: 500 } + + validate :check_license + + def check_license + raise "license_id值无效. " if license_id && License.find_by(id: license_id).blank? + end + +end \ No newline at end of file From 03c9df3f92e6df887decf7dc503cf1dfad7b8904 Mon Sep 17 00:00:00 2001 From: yystopf Date: Sun, 7 Apr 2024 14:38:52 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9A=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E8=8F=9C=E5=8D=95=E7=9A=84=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/projects/datasets_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/projects/datasets_controller.rb b/app/controllers/api/v1/projects/datasets_controller.rb index 0065d529e..8690ba529 100644 --- a/app/controllers/api/v1/projects/datasets_controller.rb +++ b/app/controllers/api/v1/projects/datasets_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Projects::DatasetsController < Api::V1::BaseController - before_action :require_member_above + before_action :require_public_and_member_above, only: [:show] + before_action :require_member_above, only: [:create, :update] before_action :find_dataset, only: [:update, :show] before_action :check_menu_authorize