Skip to content
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

Scheduling Drag and Drop Fixes #386

Merged
merged 8 commits into from
Jul 22, 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
20 changes: 10 additions & 10 deletions app/assets/javascripts/osem-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var Schedule = {
schedule_id = schedule_id_param;
},
remove: function(element) {
var e = $("#" + element);
var event_schedule_id = e.attr("event_schedule_id");
if(event_schedule_id != null){
var my_url = url + '/' + event_schedule_id;
var e = $("#" + element);
var event_schedule_id = parseInt(e.attr("event_schedule_id"));
if(event_schedule_id > 0) {
var my_url = `${url}/${event_schedule_id}`;
var success_callback = function(data) {
e.attr("event_schedule_id", null);
e.appendTo($(".unscheduled-events"));
Expand All @@ -41,18 +41,18 @@ var Schedule = {
},
add: function (previous_parent, new_parent, event) {
event.appendTo(new_parent);
var event_schedule_id = event.attr("event_schedule_id");
// Event Schedule Id could be an empty string.
var event_schedule_id = parseInt(event.attr("event_schedule_id"));
var my_url = url;
var type = 'POST';
var params = { event_schedule: {
room_id: new_parent.attr("room_id"),
start_time: (new_parent.attr("date") + ' ' + new_parent.attr("hour"))
}};
if(event_schedule_id != null){
if (event_schedule_id > 0) {
type = 'PUT';
my_url += ('/' + event_schedule_id);
}
else{
my_url += `/${event_schedule_id}`;
} else {
params['event_schedule']['event_id'] = event.attr("event_id");
params['event_schedule']['schedule_id'] = schedule_id;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ $(document).ready( function() {
$('.event-item').each(function() {

var eventTimeStr = $(this).data('time');

if (eventTimeStr) {
var eventTime = new Date(eventTimeStr);
var diff = Math.abs(eventTime - now);
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/admin/commercials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def aggregate_errors(errors)
end

def commercial_params
params.require(:commercial).permit(:title, :url).tap do |params|
params[:url] = Commercial.generate_snap_embed(params[:url]) if params[:url]
end
params.require(:commercial).permit(:title, :url)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/admin/venue_commercials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def destroy
end

def render_commercial
result = Commercial.render_from_url(params[:url])
result = Commercial.render_from_url(params[:url], params[:title])
if result[:error]
render plain: result[:error], status: :bad_request
else
Expand Down
46 changes: 12 additions & 34 deletions app/models/commercial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,26 @@ class Commercial < ApplicationRecord

validate :valid_url

def self.render_from_url(url)
def self.render_from_url(url, title = nil)
register_provider
url = Commercial.generate_snap_embed(url)
begin
resource = OEmbed::Providers.get(url, maxwidth: 560, maxheight: 315)
{ html: resource.html.html_safe }
rescue StandardError
{ html: EmbeddableURL.new(url).render_embed.html_safe }
{ html: EmbeddableURL.new(url, title).render_embed.html_safe }
# { error: exception.message }
end
end

def self.generate_snap_embed(url)
return url unless url

uri = URI.parse(url)
if uri.host == 'snap.berkeley.edu' && uri.path == '/project'
args = URI.decode_www_form(uri.query).to_h
else
return url
end
if args.key?('username') && args.key?('projectname')
new_query = URI.encode_www_form({
'projectname' => args['projectname'],
'username' => args['username'],
'showTitle' => 'true',
'showAuthor' => 'true',
'editButton' => 'true',
'pauseButton' => 'true'
})
new_uri = URI::HTTPS.build(
host: uri.host,
path: '/embed',
query: new_query
)
return new_uri.to_s
end
url
end

def self.iframe_fallback(url)
"<iframe width=560 height=315 frameborder=0 allowfullscreen=true src=\"#{url}\"></iframe>".html_safe
# TODO: Is this necessary?
def self.iframe_fallback(url, title)
iframe = <<~HTML
<iframe width=560 height=315 frameborder=0 allowfullscreen=true
title="#{title || 'Embedded Media for Event'}"
src="#{url}">
</iframe>
HTML
iframe.html_safe
end

def self.read_file(file)
Expand Down Expand Up @@ -109,7 +87,7 @@ def self.read_file(file)
def valid_url
return unless url

result = Commercial.render_from_url(url)
result = Commercial.render_from_url(url, title)
errors.add(:base, result[:error]) if result[:error]
end

Expand Down
41 changes: 33 additions & 8 deletions app/services/embeddable_url.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Transform a URL to a version that allows iframes

class EmbeddableURL
attr_accessor :url
attr_accessor :url, :title

DEFAULT_FRAME_ATTRS = 'width=560 height=315 frameborder=0 allowfullscreen'.freeze

Expand All @@ -11,15 +11,19 @@ class EmbeddableURL
/dropbox\.com/ => :dropbox
}.freeze

def initialize(url)
self.url = url
def initialize(url, title)
# Do some normalizing so that URIs parse correctly.
if url
self.url = url.strip
end
self.title = title
end

def render_embed
return render_dropbox if url.include?('dropbox.com')

# TODO-A11Y: Set an iframe title
"<iframe #{DEFAULT_FRAME_ATTRS} src='#{iframe_url}'></iframe>"
"<iframe #{DEFAULT_FRAME_ATTRS} src='#{iframe_url}' #{iframe_title}></iframe>"
end

def iframe_url
Expand Down Expand Up @@ -66,9 +70,30 @@ def dropbox(url)

def snap(url)
uri = URI.parse(url)
query = CGI.parse(uri.query)
username = query['username'][0] || query['user'][0]
project = query['projectname'][0] || query['project'][0]
"https://snap.berkeley.edu/embed?projectname=#{project}&username=#{username}&showTitle=true&showAuthor=true&editButton=true&pauseButton=true"
return url if uri.query.blank?

args = URI.decode_www_form(uri.query).to_h
username = args['username'] || args['user']
projectname = args['projectname'] || args['project']

return url if username.blank? || projectname.blank?

query = URI.encode_www_form({
'projectname' => projectname,
'username' => username,
'showTitle' => 'true',
'showAuthor' => 'true',
'editButton' => 'true',
'pauseButton' => 'true'
})
URI::HTTPS.build(host: uri.host, path: '/embed', query: query).to_s
end

def iframe_title
if title
"title='#{title} Embedded Media'"
else
'title="Embedded Media for Presentation"'
end
end
end
2 changes: 1 addition & 1 deletion app/views/shared/_media_item.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- elsif commercial.commercial_type == 'YouTube'
%iframe{width: '560', height: '315', frameborder: '0', allowfullscreen: 'true', src: "https://www.youtube.com/embed/#{commercial.commercial_id}?rel=0"}
- elsif commercial.url
= Commercial.render_from_url(commercial.url)[:html]
= Commercial.render_from_url(commercial.url, commercial.title)[:html]
- if commercial.url
= link_to('Open in a new tab', commercial.url, target: '_blank', class: 'btn btn-info btn-xs')
%br

This file was deleted.

5 changes: 5 additions & 0 deletions db/migrate/20240722182333_add_currency_to_ticket_purchases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCurrencyToTicketPurchases < ActiveRecord::Migration[7.0]
def change
add_column :ticket_purchases, :currency, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240722183008_add_currency_to_payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCurrencyToPayments < ActiveRecord::Migration[7.0]
def change
add_column :payments, :currency, :string
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_04_22_200831) do
ActiveRecord::Schema[7.0].define(version: 2024_07_22_183008) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
Expand Down Expand Up @@ -328,9 +328,9 @@
t.integer "status", default: 0, null: false
t.integer "user_id", null: false
t.integer "conference_id", null: false
t.string "currency"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.string "currency"
end

create_table "physical_tickets", force: :cascade do |t|
Expand Down Expand Up @@ -545,8 +545,8 @@
t.integer "payment_id"
t.integer "week"
t.float "amount_paid", default: 0.0
t.string "currency"
t.integer "amount_paid_cents", default: 0
t.string "currency"
end

create_table "ticket_scannings", force: :cascade do |t|
Expand Down
20 changes: 20 additions & 0 deletions lib/tasks/migrate_currency.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

namespace :data do
namespace :migrate do
desc 'Update Currency of past ticket purchases'
task :update_ticket_purchase_currency do
TicketPurchase.where(currency: nil).update_all(currency: 'USD')
TicketPurchase.find_each do |purchase|
converted_amount = CurrencyConversion.convert_currency(
purchase.conference,
purchase.ticket.price_cents,
purchase.price_currency,
purchase.currency
)

purchase.update_column(:amount_paid_cents, converted_amount)
end
end
end
end
1 change: 0 additions & 1 deletion spec/factories/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# id :bigint not null, primary key
# amount :integer
# authorization_code :string
# currency :string
# last4 :string
# status :integer default("unpaid"), not null
# created_at :datetime not null
Expand Down
7 changes: 0 additions & 7 deletions spec/models/commercial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,4 @@
commercial = build(:conference_commercial)
expect(commercial.valid?).to be true
end

it 'parses snap url' do
url = 'https://snap.berkeley.edu/project?username=avi_shor&projectname=stamps'
transformed_url = Commercial.generate_snap_embed(url)
expected_url = 'https://snap.berkeley.edu/embed?projectname=stamps&username=avi_shor&showTitle=true&showAuthor=true&editButton=true&pauseButton=true'
expect(transformed_url).to eq expected_url
end
end
16 changes: 12 additions & 4 deletions spec/services/embeddable_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@
describe '#iframe_url' do
it 'returns the original url if no transformations apply' do
url = 'https://example.com'
expect(EmbeddableURL.new(url).iframe_url).to eq url
expect(EmbeddableURL.new(url, 'title').iframe_url).to eq url
end

it 'transforms a Google Drive URL' do
url = EmbeddableURL.new('https://docs.google.com/presentation/d/1eGbEQtcOPW2N2P5rKfBVfSo2zn4C307Sh6C7vpJsruE/edit#slide=id.g1088c029399_0_47').iframe_url
url = EmbeddableURL.new('https://docs.google.com/presentation/d/1eGbEQtcOPW2N2P5rKfBVfSo2zn4C307Sh6C7vpJsruE/edit#slide=id.g1088c029399_0_47', 'title').iframe_url
expect(url).to include '/embed'
expect(url).not_to include('/edit')
end

it 'transforms a Dropbox URL' do
url = EmbeddableURL.new('https://www.dropbox.com/scl/fi/49gkp6ghfnxgqex64zvzd/Guzdial-SnapCon23.pdf?rlkey=ecwvmcmfscqtwfq21l3kzqcul&dl=1').iframe_url
url = EmbeddableURL.new('https://www.dropbox.com/scl/fi/49gkp6ghfnxgqex64zvzd/Guzdial-SnapCon23.pdf?rlkey=ecwvmcmfscqtwfq21l3kzqcul&dl=1', 'title').iframe_url
expect(url).to include('dl=0')
expect(url).not_to include('raw=')
end

it 'transforms a Snap! Project URL' do
url = EmbeddableURL.new('https://snap.berkeley.edu/project?username=jedi_force&projectname=Autograder%2dlite').iframe_url
url = EmbeddableURL.new('https://snap.berkeley.edu/project?username=jedi_force&projectname=Autograder%2dlite', 'title').iframe_url
expect(url).to include('/embed')
end
end

# it 'parses snap url' do
# url = 'https://snap.berkeley.edu/project?username=avi_shor&projectname=stamps'
# transformed_url = Commercial.generate_snap_embed(url)
# expected_url = 'https://snap.berkeley.edu/embed?projectname=stamps&username=avi_shor&showTitle=true&showAuthor=true&editButton=true&pauseButton=true'
# expect(transformed_url).to eq expected_url
# end
# TODO: Test ifram generation, snap-embedding
end
Loading