8000 Set creator/collection/license/tags during upload by Floppy · Pull Request #2637 · manyfold3d/manyfold · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Set creator/collection/license/tags during upload #2637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions app/controllers/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ModelsController < ApplicationController
include Permittable

before_action :get_model, except: [:bulk_edit, :bulk_update, :index, :new, :create]
before_action :get_creators_and_collections, only: [:new, :edit, :bulk_edit]

after_action :verify_policy_scoped, only: [:bulk_edit, :bulk_update]

def index
Expand Down Expand Up @@ -56,8 +58,6 @@ def new
end

def edit
@creators = Creator.all
@collections = Collection.all
@model.links.build if @model.links.empty? # populate empty link
@model.caber_relations.build if @model.caber_relations.empty?
end
Expand All @@ -72,7 +72,15 @@ def create
end

uploads.each do |upload|
ProcessUploadedFileJob.perform_later(library.id, upload["response"]["body"], owner: current_user)
ProcessUploadedFileJob.perform_later(
library.id,
upload["response"]["body"],
owner: current_user,
creator_id: params[:creator_id],
collection_id: params[:collection_id],
license: params[:license],
tags: params[:add_tags]
)
end

redirect_to libraries_path, notice: t(".success")
Expand Down Expand Up @@ -115,8 +123,6 @@ def scan

def bulk_edit
authorize Model
@creators = policy_scope(Creator)
@collections = policy_scope(Collection)
@models = filtered_models @filters
@remove_tags, _unused = generate_tag_list(@models)
@add_tags = ActsAsTaggableOn::Tag.where.not(id: @remove_tags.pluck(:id))
Expand Down Expand Up @@ -190,4 +196,9 @@ def get_model
authorize @model
@title = @model.name
end

def get_creators_and_collections
@creators = policy_scope(Creator)
@collections = policy_scope(Collection)
end
end
18 changes: 12 additions & 6 deletions app/jobs/process_uploaded_file_job.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
class ProcessUploadedFileJob < ApplicationJob
queue_as :default

def perform(library_id, uploaded_file, owner: nil)
def perform(library_id, uploaded_file, owner: nil, creator_id: nil, collection_id: nil, tags: nil, license: nil)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method perform has 31 lines of code (exceeds 25 allowed). Consider refactoring.

# Find library
library = Library.find(library_id)
return if library.nil?
# Attach cached upload file
attacher = Shrine::Attacher.new
attacher.attach_cached(uploaded_file)
file = attacher.file
# Generate model name
model_path = File.basename(file.original_filename, ".*")
model_name = model_path.humanize.tr("+", " ").titleize
temp_path = File.basename(file.original_filename, ".*")
data = {
name: temp_path.humanize.tr("+", " ").titleize,
path: temp_path,
creator_id: creator_id,
collection_id: collection_id,
tag_list: tags,
license: license
}.compact
# Create model
model = library.models.create(name: model_name, path: "#{model_path}##{SecureRandom.hex(4)}")
model = library.models.create!(data)
model.grant_permission_to "own", owner
model.update! path: "#{model_path}##{model.id}" # Set to proper ID after saving
model.update! organize: true
# Handle different file types
begin
case File.extname(file.original_filename).delete(".").downcase
Expand Down
28 changes: 28 additions & 0 deletions app/views/models/_bulk_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="row mb-3">
<%= form.label :creator_id, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<div class="input-group">
<%= form.collection_select :creator_id, @creators, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
<%= link_to t("creators.general.new"), new_creator_path, class: "btn btn-outline-secondary col-auto" if policy(:creator).new? %>
</div>
</div>
</div>

<div class="row mb-3">
<%= form.label :collection_id, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<div class="input-group">
<%= form.collection_select :collection_id, @collections, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
<%= link_to t("collections.general.new"), new_collection_path, class: "btn btn-outline-secondary col-auto" if policy(:collection).new? %>
</div>
</div>
</div>

<div class="row mb-3">
<%= form.label :license, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<%= form.select :license, license_select_options, {include_blank: true}, {class: "form-control col-auto form-select"} %>
</div>
</div>

<%= render "tags_edit", form: form, name: :add_tags, value: "", label: translate(".add_tags"), tags: @add_tags || [] %>
32 changes: 3 additions & 29 deletions app/views/models/bulk_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,14 @@

<p class="lead"><%= t ".form_subtitle" %></p>

<div class="row mb-3">
<%= form.label :creator_id, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<div class="input-group">
<%= form.collection_select :creator_id, @creators, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
<%= link_to t("creators.general.new"), new_creator_path, class: "btn btn-outline-secondary col-auto" if policy(:creator).new? %>
</div>
</div>
</div>

<div class="row mb-3">
<%= form.label :new_library_id, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<%= form.collection_select :new_library_id, Library.all, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
</div>
</div>

<div class="row mb-3">
<%= form.label :license, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<%= form.select :license, license_select_options, {include_blank: true}, {class: "form-control col-auto form-select"} %>
</div>
</div>
<%= render "bulk_fields", form: form %>

<%= render "tags_edit", form: form, name: :add_tags, value: "", label: translate(".add_tags"), tags: @add_tags || [] %>
<%= render "tags_edit", form: form, name: :remove_tags, value: "", label: translate(".remove_tags"), tags: @remove_tags || [] %>

<div class="row mb-3">
<%= form.label :collection_id, class: "col-sm-2 col-form-label" %>
<%= form.label :new_library_id, class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<div class="input-group">
<%= form.collection_select :collection_id, @collections, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
<%= link_to t("collections.general.new"), new_collection_path, class: "btn btn-outline-secondary col-auto" if policy(:collection).new? %>
</div>
<%= form.collection_select :new_library_id, Library.all, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
</div>
</div>

Expand Down
2 changes: 2 additions & 0 deletions app/views/models/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
</div>
</div>

<%= render "bulk_fields", form: form %>

<div class="row mb-3 input-group">
<div class='offset-sm-2 col-sm-10 ps-0'>
<%= submit_tag translate(".submit"), class: "btn btn-primary", disabled: true %>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/models/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
de:
models:
bulk_edit:
add_tags: Tags hinzufügen
description: 'Wähle die zu ändernden Modelle aus:'
form_subtitle: 'Wähle die Änderungen aus, die du vornehmen möchtest:'
needs_organizing: Muss organisiert werden
Expand All @@ -11,6 +10,8 @@ de:
select_all: Alle Modelle auswählen
submit: Ausgewählte Modelle aktualisieren
title: Modelle massenweise bearbeiten
bulk_fields:
add_tags: Tags hinzufügen
bulk_update:
success: Modelle erfolgreich aktualisiert.
create:
Expand Down
3 changes: 2 additions & 1 deletion config/locales/models/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
en:
models:
bulk_edit:
add_tags: Add tags
description: 'Select models to change:'
form_subtitle: 'Select changes to make:'
needs_organizing: Needs organizing
Expand All @@ -11,6 +10,8 @@ en:
select_all: Select all models
submit: Update Selected Models
title: Bulk Edit Models
bulk_fields:
add_tags: Add tags
bulk_update:
success: Models updated successfully.
create:
Expand Down
3 changes: 2 additions & 1 deletion config/locales/models/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
fr:
models:
bulk_edit:
add_tags: Ajouter des étiquettes
description: 'Sélectionner les modèles à modifier :'
form_subtitle: 'Sélectionnez les modifications à apporter :'
needs_organizing: Besoin d'organisation
Expand All @@ -11,6 +10,8 @@ fr:
select_all: Sélectionner tous les modèles
submit: Mettre à jour les modèles sélectionnés
title:
bulk_fields:
add_tags: Ajouter des étiquettes
bulk_update:
success: Les modèles ont été mis à jour avec succès.
create:
Expand Down
3 changes: 2 additions & 1 deletion config/locales/models/pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pl:
models:
bulk_edit:
add_tags: Dodaj tagi
description: 'Wybierz modele do zmiany:'
form_subtitle: 'Wybierz zmiany do wprowadzenia:'
needs_organizing: Wymaga uporządkowania
Expand All @@ -11,6 +10,8 @@ pl:
select_all: Zaznacz wszystkie modele
submit: Aktualizuj zaznaczone modele
title: Edytuj zbiorczo modele
bulk_fields:
add_tags: Dodaj tagi
bulk_update:
success: Modele zaktualizowane pomyślnie.
create:
Expand Down
27 changes: 27 additions & 0 deletions spec/jobs/process_uploaded_file_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@
job.perform(library.id, file, owner: uploader)
expect(Model.last.permitted_users.with_permission(:own)).to include uploader
end

it "Stores creator if provided" do
creator = create(:creator)
job.perform(library.id, file, creator_id: creator.id)
expect(Model.last.creator).to eq creator
end

it "Stores collection if provided" do
collection = create(:collection)
job.perform(library.id, file, collection_id: collection.id)
expect(Model.last.collection).to eq collection
end

it "Stores license if provided" do
job.perform(library.id, file, license: "CC-BY-NC-SA-4.0")
expect(Model.last.license).to eq "CC-BY-NC-SA-4.0"
end

it "Stores tags if provided" do
job.perform(library.id, file, tags: "tag1, tag2, tag3")
expect(Model.last.tag_list).to eq ["tag1", "tag2", "tag3"]
end

it "sets path using auto-organize" do
job.perform(library.id, file, tags: "tag1")
expect(Model.last.path).to eq "tag1/test#1"
end
end

context "when errors occur during processing" do
Expand Down
Loading
0