From 5ba03a2755e7d84b4019c951ea59156500a1ea01 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Tue, 18 Aug 2015 20:41:37 -0500 Subject: [PATCH] * VRFS-3431 - better response when creating HFA request --- admin/app/admin/jam_track_hfa.rb | 22 +++++ db/manifest | 3 +- db/up/harry_fox_agency.sql | 32 +++++++ ruby/lib/jam_ruby.rb | 2 + ruby/lib/jam_ruby/jam_track_importer.rb | 2 +- ruby/lib/jam_ruby/lib/s3_manager_mixin.rb | 4 +- ruby/lib/jam_ruby/models/jam_track.rb | 11 ++- .../jam_ruby/models/jam_track_hfa_request.rb | 89 +++++++++++++++++++ .../models/jam_track_hfa_request_id.rb | 27 ++++++ .../models/jam_track_hfa_request_spec.rb | 22 +++++ 10 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 admin/app/admin/jam_track_hfa.rb create mode 100644 db/up/harry_fox_agency.sql create mode 100644 ruby/lib/jam_ruby/models/jam_track_hfa_request.rb create mode 100644 ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb create mode 100644 ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb diff --git a/admin/app/admin/jam_track_hfa.rb b/admin/app/admin/jam_track_hfa.rb new file mode 100644 index 000000000..972856fc1 --- /dev/null +++ b/admin/app/admin/jam_track_hfa.rb @@ -0,0 +1,22 @@ +ActiveAdmin.register_page "Harry Fox Request" do + menu :parent => 'JamTracks' + + page_action :create_request, :method => :post do + + name = params[:jam_ruby_jam_track_hfa_request][:name] + + request = JamTrackHfaRequest.create(name) + redirect_to admin_harry_fox_request_path, :notice => "Request created. Check Amazon S3 in the 'jamkazam' bucket; specifically #{request.request_csv_filename}" + end + + + content do + semantic_form_for JamTrackHfaRequest.new, :url => admin_harry_fox_request_create_request_path, :builder => ActiveAdmin::FormBuilder do |f| + f.inputs "New Harry Fox Licensing Request" do + f.input :name, :hint => "Some sort of name to help us remember what this request was for" + end + f.actions + end + + end +end diff --git a/db/manifest b/db/manifest index 9b059151a..cbb24506b 100755 --- a/db/manifest +++ b/db/manifest @@ -300,4 +300,5 @@ alter_band_profile_rate_defaults.sql repair_band_profile.sql jam_track_onboarding_enhancements.sql jam_track_name_drop_unique.sql -jam_track_searchability.sql \ No newline at end of file +jam_track_searchability.sql +harry_fox_agency.sql \ No newline at end of file diff --git a/db/up/harry_fox_agency.sql b/db/up/harry_fox_agency.sql new file mode 100644 index 000000000..dfc94be59 --- /dev/null +++ b/db/up/harry_fox_agency.sql @@ -0,0 +1,32 @@ +ALTER TABLE jam_tracks ADD COLUMN server_fixation_date DATE DEFAULT NOW(); +ALTER TABLE jam_tracks ADD COLUMN hfa_license_status BOOLEAN DEFAULT FALSE; +ALTER TABLE jam_tracks ADD COLUMN hfa_license_desired BOOLEAN DEFAULT TRUE; +ALTER TABLE jam_tracks ADD COLUMN alternative_license_status BOOLEAN DEFAULT FALSE; +ALTER TABLE jam_tracks ADD COLUMN hfa_license_number INTEGER; +ALTER TABLE jam_tracks ADD COLUMN hfa_song_code VARCHAR; +ALTER TABLE jam_tracks ADD COLUMN album_title VARCHAR; + +CREATE TABLE jam_track_hfa_requests ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + request_csv_filename VARCHAR, + response_csv_filename VARCHAR, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + approved_at TIMESTAMP +); + +CREATE TABLE jam_track_hfa_request_ids ( + id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(), + jam_track_id VARCHAR(64) NOT NULL REFERENCES jam_tracks(id) ON DELETE SET NULL, + jam_track_hfa_request_id INTEGER REFERENCES jam_track_hfa_requests(id) ON DELETE SET NULL, + request_id INTEGER, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE SEQUENCE jam_track_hfa_request_id_seq; +ALTER TABLE jam_track_hfa_request_ids ALTER COLUMN request_id SET DEFAULT nextval('jam_track_hfa_request_id_seq'::regclass); +ALTER SEQUENCE jam_track_hfa_request_id_seq OWNED BY jam_track_hfa_request_ids.request_id; + +ALTER TABLE ONLY jam_track_hfa_request_ids ALTER COLUMN request_id SET DEFAULT nextval('jam_track_hfa_request_id_seq'::regclass); \ No newline at end of file diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 7d7ffd4e6..16890a9d9 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -203,6 +203,8 @@ require "jam_ruby/models/email_batch_scheduled_sessions" require "jam_ruby/models/email_batch_set" require "jam_ruby/models/jam_track_licensor" require "jam_ruby/models/jam_track" +require "jam_ruby/models/jam_track_hfa_request" +require "jam_ruby/models/jam_track_hfa_request_id" require "jam_ruby/models/jam_track_track" require "jam_ruby/models/jam_track_right" require "jam_ruby/models/jam_track_tap_in" diff --git a/ruby/lib/jam_ruby/jam_track_importer.rb b/ruby/lib/jam_ruby/jam_track_importer.rb index 594741785..a68ef6cc2 100644 --- a/ruby/lib/jam_ruby/jam_track_importer.rb +++ b/ruby/lib/jam_ruby/jam_track_importer.rb @@ -444,7 +444,7 @@ module JamRuby if is_tency_storage? jam_track.vendor_id = metadata[:id] jam_track.licensor = JamTrackLicensor.find_by_name('Tency Music') - add_licensor_metadata('Tency Music', metalocation) + #add_licensor_metadata('Tency Music', metalocation) end else if !options[:resync_audio] diff --git a/ruby/lib/jam_ruby/lib/s3_manager_mixin.rb b/ruby/lib/jam_ruby/lib/s3_manager_mixin.rb index c294afe6d..b7759afc8 100644 --- a/ruby/lib/jam_ruby/lib/s3_manager_mixin.rb +++ b/ruby/lib/jam_ruby/lib/s3_manager_mixin.rb @@ -7,7 +7,9 @@ module JamRuby end module ClassMethods - + def s3_manager(options={:bucket => nil, :public => false}) + @s3_manager ||= S3Manager.new(options[:bucket] ? options[:bucket] : (options[:public] ? APP_CONFIG.aws_bucket_public : APP_CONFIG.aws_bucket), APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key) + end end def s3_manager(options={:bucket => nil, :public => false}) diff --git a/ruby/lib/jam_ruby/models/jam_track.rb b/ruby/lib/jam_ruby/models/jam_track.rb index 0bc4422b5..e42eac2e1 100644 --- a/ruby/lib/jam_ruby/models/jam_track.rb +++ b/ruby/lib/jam_ruby/models/jam_track.rb @@ -17,7 +17,8 @@ module JamRuby :original_artist, :songwriter, :publisher, :licensor, :licensor_id, :pro, :genres_jam_tracks_attributes, :sales_region, :price, :reproduction_royalty, :public_performance_royalty, :reproduction_royalty_amount, :licensor_royalty_amount, :pro_royalty_amount, :plan_code, :initial_play_silence, :jam_track_tracks_attributes, - :jam_track_tap_ins_attributes, :genre_ids, :version, :jmep_json, :jmep_text, :pro_ascap, :pro_bmi, :pro_sesac, :duration, as: :admin + :jam_track_tap_ins_attributes, :genre_ids, :version, :jmep_json, :jmep_text, :pro_ascap, :pro_bmi, :pro_sesac, :duration, + :server_fixation_date, :hfa_license_status, :hfa_license_desired, :alternative_license_status, :hfa_license_number, :hfa_song_code, :album_title, as: :admin validates :name, presence: true, length: {maximum: 200} validates :plan_code, presence: true, uniqueness: true, length: {maximum: 50 } @@ -37,7 +38,13 @@ module JamRuby validates :public_performance_royalty, inclusion: {in: [nil, true, false]} validates :reproduction_royalty, inclusion: {in: [nil, true, false]} validates :public_performance_royalty, inclusion: {in: [nil, true, false]} - validates :duration, numericality: {only_integer: true}, :allow_nil => true + validates :duration, numericality: {only_integer: true}, :allow_nil => true + validates :hfa_license_status, inclusion: {in: [true, false]} + validates :hfa_license_desired, inclusion: {in: [true, false]} + validates :alternative_license_status, inclusion: {in: [true, false]} + validates :hfa_license_number, numericality: {only_integer: true}, :allow_nil => true + validates :hfa_song_code, length: {maximum: 200} + validates :album_title, length: {maximum: 200} validates_format_of :reproduction_royalty_amount, with: /^\d+\.*\d{0,4}$/, :allow_blank => true validates_format_of :licensor_royalty_amount, with: /^\d+\.*\d{0,4}$/, :allow_blank => true diff --git a/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb b/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb new file mode 100644 index 000000000..3733a6574 --- /dev/null +++ b/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb @@ -0,0 +1,89 @@ +module JamRuby + class JamTrackHfaRequest < ActiveRecord::Base + include JamRuby::S3ManagerMixin + + + @@log = Logging.logger[JamTrackHfaRequest] + + attr_accessible :name, as: :admin + + validates :name, presence: true, length: {maximum: 200} + + def self.create(name) + request = nil + transaction do + request = JamTrackHfaRequest.new + request.name = name + request.save! + request.reload + + requests = [] + JamTrack.where(hfa_license_status: false).where(hfa_license_desired: true).where(alternative_license_status: false).each do |jam_track| + request_id = JamTrackHfaRequestId.new + request_id.jam_track = jam_track + request_id.jam_track_hfa_request = request + request_id.save + request_id.reload # to get back the request_id attribute + requests << request_id + end + + request_name = "JamKazam-#{request.id}-#{request.created_at.to_date.to_s}.csv" + Dir.mktmpdir do |tmp_dir| + out = File.join(tmp_dir, request_name) + + # Field 1 - HFA Agreement Code - Hardcode to "SSA". + # Field 2 - Manufacturer Number - Hardcode to "M18303". + # Field 3 - Transaction Date - Populate this field with the date that we generate this tab-delimited file, in the format YYYYMMDD - e.g. "20150813". + # Field 4 - License Request Number - This one is slightly more involved. Basically, according to HFA we need to generate a unique numeric ID for each JamTrack license request (as opposed to each unique JamTrack, as we might need to make more than one request per JamTrack if such requests were to fail in some cases). This unique numeric ID per request should start with the number 1, and increment by 1. So I guess this feature will need to remember which of these IDs are used on each run it makes so that it knows where to start on the next run. + # Field 7 - Total Playing Time - Minutes - We already have a JamTrack field for the duration of the JamTrack in seconds. We should keep that field, and keep using it as is. We need to use that field to populate this Field 7 and the next Field 8. So if the duration of the JamTrack in seconds were 90 seconds, then we should set Field 7 to "1" and Field 8 to "30" to signify a length of 1:30. + # Field 8 - Total Playing Time - Seconds - See note above on Field 7. + # Field 9 - Artist Name - Populate this field from the Artist Name field in the JamTrack record - e.g. "AC/DC". + # Field 10 - Song Title - Populate this field from the Song Name field in the JamTrack record - e.g. "Back In Black". + # Field 21 - Configuration Code - Hardcode to "SP". + # Field 22 - License Type - Hardcode to "G". + # Field 23 - Server Fixation Date - Set this to the approximate date that the JamTrack was uploaded to our servers, and format as YYYYMMDD - e.g. "20150813". I'm suggesting we update each JamTrack record with this date, just so that we have a record of this piece of data we submitted to HFA - even though HFA didn't seem at all clear about how this data is used or why it matters. + # Field 24 - Rate Code - Hardcode to "S". + # Field 37 - User Defined - Populate this field with our internal JamKazam unique JamTrack ID. This field value is supposed to be passed back to us from HFA in the processed output file, and we'll need this to associate the HFA License Number with our internal JamTrack ID. + # Field 38 - Track ID - Let's also populate this field with our internal JamKazam unique JamTrack ID, just like Field 37, just for fun. + + + CSV.open(out, "wb") do |csv| + requests.each do |request| + line = {} + line['1'] = 'SSA' + line['2'] = 'M18303' + line['3'] = Time.now.to_date.strftime('%Y%m%d') + line['4'] = request.request_id + line['7'] = request.jam_track.duration / 60 + line['8'] = request.jam_track.duration % 60 + line['9'] = request.jam_track.original_artist + line['10'] = request.jam_track.name + line['21'] = 'SP' + line['22'] = 'G' + line['23'] = request.jam_track.server_fixation_date.strftime('%Y%m%d') + line['24'] = 'S' + line['37'] = request.jam_track.id + line['38'] = request.jam_track.id + + entry = [] + 38.times do |i| + entry << line[(i + 1).to_s] + end + csv << entry + end + end + + upload_path = "harry_fox_requests/#{request_name}" + s3_manager.upload(upload_path, out, content_type: 'text/csv') + + request.request_csv_filename = upload_path + request.save! + end + + + request + end + end + end +end + diff --git a/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb b/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb new file mode 100644 index 000000000..81d365e33 --- /dev/null +++ b/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb @@ -0,0 +1,27 @@ +module JamRuby + class JamTrackHfaRequestId < ActiveRecord::Base + include JamRuby::S3ManagerMixin + + before_create(:remove_attribute) + + + + @@log = Logging.logger[JamTrackHfaRequestId] + + attr_accessible :name, as: :admin + + belongs_to :jam_track, class_name: "JamRuby::JamTrack" + belongs_to :jam_track_hfa_request, class_name: "JamRuby::JamTrackHfaRequest" + + validates :jam_track, presence: true + validates :jam_track_hfa_request, presence:true + + private + + def remove_attribute + @attributes.delete('request_id') + end + + end +end + diff --git a/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb b/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb new file mode 100644 index 000000000..9318b620a --- /dev/null +++ b/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe JamTrackHfaRequest do + include CarrierWave::Test::Matchers + include UsesTempFiles + + #let(:jamtrack1) {FactoryGirl.create(:jam_track, hfa_license_status: false, hfa_license_desired: true, alternative_license_status: false) + let(:jamtrack1) {FactoryGirl.create(:jam_track, duration: 90, server_fixation_date: Time.now.to_date ) } + + it "creates request" do + + jamtrack1.touch + JamTrackHfaRequest.create('request1') + + request = JamTrackHfaRequest.first + request.request_csv_filename.should_not be_nil + + request_id = JamTrackHfaRequestId.first + request_id.request_id.should_not be_nil + end +end +