From c5ae8f4d6e1a0f6663e221066c0f9db9fa243d62 Mon Sep 17 00:00:00 2001 From: Vidir Valberg Gudmundsson Date: Sat, 15 Jul 2017 17:58:47 +0200 Subject: [PATCH] Revamp schedule code and fix #128. --- src/program/consumers.py | 8 +- src/program/models.py | 3 +- .../static/js/event_instance_websocket.js | 117 +++++++++++------- 3 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/program/consumers.py b/src/program/consumers.py index 6faa81e7..9d141e87 100644 --- a/src/program/consumers.py +++ b/src/program/consumers.py @@ -23,7 +23,7 @@ class ScheduleConsumer(JsonWebsocketConsumer): camp.get_days('camp') )) event_instances_query_set = EventInstance.objects.filter(event__camp=camp) - event_instances = list(map(lambda x: x.to_json(), event_instances_query_set)) + event_instances = list(map(lambda x: x.to_json(user=message.user), event_instances_query_set)) self.send({ "accept": True, "event_instances": event_instances, @@ -38,12 +38,6 @@ class ScheduleConsumer(JsonWebsocketConsumer): action = content.get('action') data = {} - if action == 'get_event_instance': - event_instance_id = content.get('event_instance_id') - event_instance = EventInstance.objects.get(id=event_instance_id) - data['action'] = 'event_instance' - data['event_instance'] = event_instance.to_json(user=message.user) - if action == 'favorite': event_instance_id = content.get('event_instance_id') event_instance = EventInstance.objects.get(id=event_instance_id) diff --git a/src/program/models.py b/src/program/models.py index 65d237d0..f761c016 100644 --- a/src/program/models.py +++ b/src/program/models.py @@ -542,7 +542,8 @@ class EventInstance(CampRelatedModel): 'location': self.location.slug, 'location_icon': self.location.icon, 'timeslots': self.timeslots, - 'video_recording': self.event.video_recording + 'video_recording': self.event.video_recording, + 'video_url': self.event.video_url, } if user and user.is_authenticated: diff --git a/src/program/static/js/event_instance_websocket.js b/src/program/static/js/event_instance_websocket.js index 8c7af723..1b01322a 100644 --- a/src/program/static/js/event_instance_websocket.js +++ b/src/program/static/js/event_instance_websocket.js @@ -30,45 +30,6 @@ function toggleFavoriteButton(button) { function setup_websocket() { webSocketBridge.connect('/schedule/'); webSocketBridge.listen(function(payload, stream) { - if(payload['action'] == 'event_instance') { - event_instance_id = payload['event_instance']['id']; - modal = modals[event_instance_id]; - modal_title = modal.getElementsByClassName('modal-title')[0]; - modal_title.innerHTML = payload['event_instance']['title'] - modal_body_content = modal.getElementsByClassName('modal-body-content')[0]; - modal_body_content.innerHTML = payload['event_instance']['abstract']; - more_button = modal.getElementsByClassName('more-button')[0]; - more_button.setAttribute('href', payload['event_instance']['url']); - - favorite_button = modal.getElementsByClassName('favorite-button')[0]; - if(payload['event_instance']['is_favorited'] !== undefined) { - favorite_button.setAttribute('data-state', payload['event_instance']['is_favorited']) - toggleFavoriteButton(favorite_button); - } else { - favorite_button.remove(); - } - - speakers_div = modal.getElementsByClassName('speakers')[0]; - speakers_div.innerHTML = ""; - speakers = payload['event_instance']['speakers']; - for(speaker_id in speakers) { - var speaker = speakers[speaker_id]; - var speaker_li = document.createElement('li'); - var speaker_a = document.createElement('a'); - speaker_a.setAttribute('href', speaker['url']); - speaker_a.appendChild(document.createTextNode(speaker['name'])); - speaker_li.appendChild(speaker_a); - speakers_div.appendChild(speaker_li); - } - - video_recording_element = modal.getElementsByClassName('video-recording')[0]; - if(payload['event_instance']['video_recording'] == true) { - video_recording_element.innerHTML = 'This event will be recorded!'; - } else { - video_recording_element.parentElement.remove(); - } - - } if(payload['action'] == 'init') { EVENT_INSTANCES = payload['event_instances']; DAYS = payload['days']; @@ -299,7 +260,13 @@ function render_event_instance(event_instance) { icon_element.classList.add('fa'); icon_element.classList.add('pull-right'); - if(event_instance['video_recording'] == true) { + if(event_instance['video_url'] != undefined) { + video_url_element = document.createElement('i'); + video_url_element.classList.add('fa-film'); + video_url_element.classList.add('fa'); + video_url_element.classList.add('pull-right'); + element.appendChild(video_url_element); + } else if(event_instance['video_recording'] == true) { video_recording_element = document.createElement('i'); video_recording_element.classList.add('fa-video-camera'); video_recording_element.classList.add('fa'); @@ -352,6 +319,11 @@ function openModal(e) { } event_instance_id = target.dataset.eventInstanceId; + event_instance = EVENT_INSTANCES.filter( + function(event_instance) { + return event_instance.id == event_instance_id + } + )[0]; modal = modals[event_instance_id]; @@ -383,6 +355,7 @@ function openModal(e) { modal_title = document.createElement('h4'); modal_title.classList.add('modal-title') + modal_title.innerHTML = event_instance['title']; modal_header.appendChild(modal_close_button); modal_header.appendChild(modal_title); @@ -390,12 +363,65 @@ function openModal(e) { modal_body_content = document.createElement('div'); modal_body_content.classList.add('modal-body'); modal_body_content.classList.add('modal-body-content'); + modal_body_content.innerHTML = event_instance['abstract']; modal_content.appendChild(modal_body_content); modal_body = document.createElement('div'); modal_body.classList.add('modal-body'); + speakers_h4 = document.createElement('h4'); + speakers_h4.innerHTML = 'Speaker(s):'; + speakers_ul = document.createElement('ul'); + speakers_ul.classList.add('speakers'); + + speakers = event_instance['speakers']; + for(speaker_id in speakers) { + var speaker = speakers[speaker_id]; + var speaker_li = document.createElement('li'); + var speaker_a = document.createElement('a'); + speaker_a.setAttribute('href', speaker['url']); + speaker_a.appendChild(document.createTextNode(speaker['name'])); + speaker_li.appendChild(speaker_a); + speakers_ul.appendChild(speaker_li); + } + + video_recording_div = document.createElement('div'); + video_recording_div.classList.add('alert'); + video_recording_div.classList.add('alert-info'); + video_recording_div.classList.add('video-recording'); + + if(event_instance['video_url'] != undefined) { + // We have an URL to the video + video_url_icon = document.createElement('i'); + video_url_icon.classList.add('fa'); + video_url_icon.classList.add('fa-film'); + video_url_link = document.createElement('a'); + video_url_link.setAttribute('href', event_instance['video_url']); + video_url_link.setAttribute('target', '_blank'); + video_url_link.innerHTML = " Watch the video recording here!"; + + video_recording_div.appendChild(video_url_icon); + video_recording_div.appendChild(video_url_link); + + } else if(event_instance['video_recording'] == true) { + // This instance will be recorded + video_notice_icon = document.createElement('i'); + video_notice_icon.classList.add('fa'); + video_notice_icon.classList.add('fa-camera'); + video_notice_span = document.createElement('span'); + video_notice_span.innerHTML = " This event will be recorded!"; + + video_recording_div.appendChild(video_notice_icon); + video_recording_div.appendChild(video_notice_span); + } else { + // This instance will NOT be recorded! + video_recording_element.remove(); + } + + modal_body.appendChild(speakers_h4); + modal_body.appendChild(speakers_ul); + modal_body.appendChild(video_recording_div); + modal_content.appendChild(modal_body); - modal_body.innerHTML = '

Speaker(s):

'; modal_footer = document.createElement('div'); modal_footer.classList.add('modal-footer'); @@ -415,12 +441,20 @@ function openModal(e) { favorite_button.classList.add('btn-success'); favorite_button.classList.add('favorite-button'); favorite_button.innerHTML = ' Favorite'; + if(event_instance['is_favorited'] !== undefined) { + favorite_button.setAttribute('data-state', event_instance['is_favorited']) + toggleFavoriteButton(favorite_button); + } else { + favorite_button.remove(); + } + modal_footer.appendChild(favorite_button); more_button = document.createElement('a'); more_button.classList.add('btn'); more_button.classList.add('btn-info'); more_button.classList.add('more-button'); + more_button.setAttribute('href', event_instance['url']); more_button.innerHTML = ' More'; modal_footer.appendChild(more_button); @@ -431,7 +465,6 @@ function openModal(e) { } $('#event-modal-' + event_instance_id).modal(); - webSocketBridge.send({action: 'get_event_instance', event_instance_id: event_instance_id}) } function init_modals(event_class_name) {