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

Fixes GH-201 - Assets do not move to "Active Assets" section when toggled "On" #202

Merged
merged 7 commits into from
May 17, 2014
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
2 changes: 1 addition & 1 deletion assets_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def is_active(asset, at_time=None):

if asset['is_enabled'] and asset['start_date'] and asset['end_date']:
at = at_time or get_time()
return asset['start_date'] < at and asset['end_date'] > at
return asset['start_date'] <= at <= asset['end_date']
return False


Expand Down
151 changes: 94 additions & 57 deletions static/coffee/specs/screenly-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,76 +1,113 @@

describe "Screenly Open Source", ->

it "should have a screenly object at its root", ->
expect(screenly).toBeDefined()

it "should have an instance of Assets on the screenly object", ->
expect(screenly.Assets).toBeDefined()
expect(screenly.Assets).toEqual jasmine.any(screenly.collections.Assets)
expect(screenly.ActiveAssets).toEqual jasmine.any(screenly.collections.ActiveAssets)
expect(screenly.InactiveAssets).toEqual jasmine.any(screenly.collections.InactiveAssets)

describe "Models", ->

it "should exist", ->
expect(screenly.models).toBeDefined()
it "should have a Screenly object at its root", ->
expect(Screenly).toBeDefined()

describe "Asset model", ->
it "should exist", ->
expect(screenly.models.Asset).toBeDefined()

describe "Collections", ->
describe "date_to", ->

it "should exist", ->
expect(screenly.collections).toBeDefined()
test_date = new Date(2014, 5, 6, 14, 20, 0, 0);
a_date = Screenly.date_to(test_date);

describe "Assets", ->
it "should exist", ->
expect(screenly.collections.Assets).toBeDefined()
expect(screenly.collections.ActiveAssets).toBeDefined()
expect(screenly.collections.InactiveAssets).toBeDefined()
it "should format date and time as 'MM/DD/YYYY hh:mm:ss A'", ->
expect(a_date.string()).toBe '06/06/2014 02:20:00 PM'

it "should format date as 'MM/a_date/YYYY'", ->
expect(a_date.date()).toBe '06/06/2014'

it "should format date as 'hh:mm:ss A'", ->
expect(a_date.time()).toBe '02:20 PM'

it "should use the Asset model", ->
assets = new screenly.collections.Assets()
expect(assets.model).toBe screenly.models.Asset

it "should populate ActiveAssets and InactiveAssets when fetched", ->
screenly.Assets.reset [
{name: "zacharytamas.com", mimetype:"webpage", is_active: true},
]
describe "Models", ->

# ActiveAssets should have one model now
expect(screenly.ActiveAssets.models.length).toEqual 1
describe "Asset model", ->
it "should exist", ->
expect(Screenly.Asset).toBeDefined()

start_date = new Date(2014, 4, 6, 14, 20, 0, 0);
end_date = new Date();
end_date.setMonth(end_date.getMonth() + 2)
asset = new Screenly.Asset({
asset_id: 2
duration: "8"
end_date: end_date
is_enabled: true
mimetype: 'webpage'
name: 'Test'
start_date: start_date
uri: 'http://www.screenlyapp.com'
})

it "should be active if enabled and date is in range", ->
expect(asset.active()).toBe true

it "should be inactive if disabled and date is in range", ->
asset.set 'is_enabled', false
expect(asset.active()).toBe false

it "should be inactive if enabled and date is out of range", ->
asset.set 'is_enabled', true
asset.set 'start_date', asset.get 'end_date'
expect(asset.active()).toBe false

it "should rollback to backup data if it exists", ->

asset.set 'start_date', start_date
asset.set 'end_date', end_date
asset.backup()

asset.set({
is_enabled: false
name: "Test 2"
start_date: new Date(2011, 4, 6, 14, 20, 0, 0)
end_date: new Date(2011, 4, 6, 14, 20, 0, 0)
uri: "http://www.wireload.net"
})

asset.rollback()

expect(asset.get 'is_enabled').toBe true
expect(asset.get 'name').toBe 'Test'
expect(asset.get 'start_date').toBe start_date
expect(asset.get 'uri').toBe "http://www.screenlyapp.com"

it "should erase backup date after rollback", ->
asset.set({
is_enabled: false
name: "Test 2"
start_date: new Date(2011, 4, 6, 14, 20, 0, 0)
end_date: new Date(2011, 4, 6, 14, 20, 0, 0)
uri: "http://www.wireload.net"
})

asset.rollback()

expect(asset.get 'is_enabled').toBe false
expect(asset.get 'name').toBe 'Test 2'
expect(asset.get('start_date').toISOString()).toBe (new Date(2011, 4, 6, 14, 20, 0, 0)).toISOString()
expect(asset.get 'uri').toBe "http://www.wireload.net"

# InactiveAssets should still be empty
expect(screenly.InactiveAssets.models.length).toEqual 0

# Now make the page inactive and confirm that ActiveAssets
# is empty (the previous information is wiped away on a
# new data load) and the InactiveAssets collection contains
# the new asset.
describe "Collections", ->

screenly.Assets.reset [
{name: "zacharytamas.com", mimetype:"webpage", is_active: false},
]
describe "Assets", ->
it "should exist", ->
expect(Screenly.Assets).toBeDefined()

# ActiveAssets should be empty now
expect(screenly.ActiveAssets.models.length).toEqual 0
it "should use the Asset model", ->
assets = new Screenly.Assets()
expect(assets.model).toBe Screenly.Asset

# InactiveAssets should have a model
expect(screenly.InactiveAssets.models.length).toEqual 1

screenly.Assets.reset [
{name: "zacharytamas.com", mimetype:"webpage", is_active: false},
{name: "Hacker News", mimetype: "webpage", is_active: true}
]
describe "Views", ->

# They should both have a model now
expect(screenly.ActiveAssets.models.length).toEqual 1
expect(screenly.InactiveAssets.models.length).toEqual 1
expect(screenly.Assets.models.length).toEqual 2
it "should have EditAssetView", ->
expect(Screenly.View.EditAssetView).toBeDefined()

describe "Views", ->
it "should have AssetRowView", ->
expect(Screenly.View.AssetRowView).toBeDefined()

it "should exist", ->
expect(screenly.views).toBeDefined()
it "should have AssetsView", ->
expect(Screenly.View.AssetsView).toBeDefined()
5 changes: 5 additions & 0 deletions static/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
jQuery(function() {
Screenly.app = new Screenly.App({
el: $('body')
});
});
36 changes: 28 additions & 8 deletions static/js/screenly-ose.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,38 @@ API.Asset = class Asset extends Backbone.Model
name: ''
mimetype: 'webpage'
uri: ''
is_active: false
start_date: now()
end_date: (moment().add 'days', 7).toDate()
duration: default_duration
is_enabled: 0
nocache: 0
active: =>
if @get('is_enabled') and @get('start_date') and @get('end_date')
at = now()
start_date = new Date(@get('start_date'));
end_date = new Date(@get('end_date'));
return start_date <= at <= end_date
else
return false

backup: =>
@backup_attributes = @toJSON()

rollback: =>
if @backup_attributes
@set @backup_attributes
@backup_attributes = undefined


API.Assets = class Assets extends Backbone.Collection
url: "/api/assets"
model: Asset


# Views
class EditAssetView extends Backbone.View
API.View = {};
API.View.EditAssetView = class EditAssetView extends Backbone.View
$f: (field) => @$ "[name='#{field}']" # get field element
$fv: (field, val...) => (@$f field).val val... # get or set filed value

Expand All @@ -59,7 +78,11 @@ class EditAssetView extends Backbone.View
(@$ 'input[name="nocache"]').prop 'checked', @model.get 'nocache'
(@$ '.modal-header .close').remove()
(@$el.children ":first").modal()

@model.backup()

@model.bind 'change', @render

@render()
@validate()
_.delay (=> (@$f 'uri').focus()), 300
Expand Down Expand Up @@ -179,7 +202,7 @@ class EditAssetView extends Backbone.View


cancel: (e) =>
@model.set @model.previousAttributes()
@model.rollback()
unless @edit then @model.destroy()
(@$el.children ":first").modal 'hide'

Expand Down Expand Up @@ -224,7 +247,7 @@ class EditAssetView extends Backbone.View
(@$ '.advanced-accordion').toggle has_nocache is on


class AssetRowView extends Backbone.View
API.View.AssetRowView = class AssetRowView extends Backbone.View
tagName: "tr"

initialize: (options) =>
Expand Down Expand Up @@ -296,7 +319,7 @@ class AssetRowView extends Backbone.View
no


class AssetsView extends Backbone.View
API.View.AssetsView = class AssetsView extends Backbone.View
initialize: (options) =>
@collection.bind event, @render for event in ('reset add remove sync'.split ' ')
@sorted = (@$ '#active-assets').sortable
Expand All @@ -312,7 +335,7 @@ class AssetsView extends Backbone.View
(@$ "##{which}-assets").html '' for which in ['active', 'inactive']

@collection.each (model) =>
which = if model.get 'is_active' then 'active' else 'inactive'
which = if model.active() then 'active' else 'inactive'
(@$ "##{which}-assets").append (new AssetRowView model: model).render()

for which in ['inactive', 'active']
Expand Down Expand Up @@ -347,6 +370,3 @@ API.App = class App extends Backbone.View
new Asset {}, {collection: API.assets}
no


jQuery -> API.app = new App el: $ 'body'

Loading