Permalink
Browse files

Added extend playlist video

  • Loading branch information...
kracekumar committed Dec 6, 2012
1 parent 3864868 commit 576b602650e67229735e5bf1d66bb856204bef1e
View
@@ -20,7 +20,7 @@
app.register_blueprint(baseframe)
assets = Environment(app)
-js = Bundle(baseframe_js, toastr_js,
+js = Bundle(baseframe_js, toastr_js, 'js/ajax-chosen.min.js',
filters='jsmin', output='js/packed.js')
css = Bundle(baseframe_css, toastr_css, 'css/app.css',
filters='cssmin', output='css/packed.css')
View
@@ -1,3 +1,4 @@
+
# -*- coding: utf-8 -*-
import re
@@ -40,5 +41,12 @@ class PlaylistAddForm(Form):
playlist = wtf.SelectField('Add to playlist', coerce=int)
+def playlist_validate_url(self, field):
+ youtube_regex = re.compile(r'(http|https)://www.youtube.com/playlist?([0-9A-Za-z]+.)', re.UNICODE)
+ if not youtube_regex.search(field.data):
+ raise wtf.ValidationError("InCorrect Youtube Playlist URL")
+
+
class PlaylistImportForm(Form):
- playlist_url = wtf.html5.URLField(u"Playlist URL", validators=[wtf.Required()])
+ playlist_url = wtf.html5.URLField(u"Playlist URL", validators=[wtf.Required(), playlist_validate_url])
+
View
@@ -210,6 +210,8 @@ def url_for(self, action='view'):
return url_for('playlist_view', channel=self.channel.name, playlist=self.name)
elif action == 'edit':
return url_for('playlist_edit', channel=self.channel.name, playlist=self.name)
+ elif action == 'extend':
+ return url_for('playlist_extend', channel=self.channel.name, playlist=self.name)
elif action == 'delete':
return url_for('playlist_delete', channel=self.channel.name, playlist=self.name)
elif action == 'new-video':
View
@@ -96,6 +96,10 @@ def url_for(self, action='view', channel=None, playlist=None, _external=False):
return url_for('video_add_speaker',
channel=channel.name, playlist=playlist.name,
video=self.url_name, _external=_external)
+ elif action == 'autocomplete-speaker':
+ return url_for('video_autocomplete_speaker',
+ channel=channel.name, playlist=playlist.name,
+ video=self.url_name, _external=_external)
elif action == 'remove-speaker':
return url_for('video_remove_speaker',
channel=channel.name, playlist=playlist.name,
@@ -1,3 +1,4 @@
+
{% extends "layout.html" %}
{% block title %}{{ playlist.title }}{% endblock %}
{% from "macros.html" import render_thumbnail %}
@@ -26,6 +27,7 @@ <h1>{{ playlist.title }}</h1>
{%- endif %}
{% if 'edit' in g.permissions -%}
<a href="{{ playlist.url_for('edit') }}"><span class="icon-pencil">Edit playlist</span></a>
+ <a href="{{ playlist.url_for('extend') }}" class='extend-playlist' data-method='GET'><span class="icon-pencil">Extend playlist</span></a>
{%- endif %}
</div>
</div>
@@ -75,4 +77,66 @@ <h1>{{ playlist.title }}</h1>
</div>
{% endblock %}
{% endblock %}
+{% endblock %}
+{% block footerscripts %}
+<script type="text/javascript">
+ $(function() {
+ var playlist_action = function(url, type) {
+ $.ajax({
+ type: type,
+ url: url,
+ success: function(msg) {
+ console.log(msg);
+ if (msg['message_type'] === 'success') {
+ if (msg['action'] === 'modal-window') {
+ $(msg['html']).modal({show: true, keyboard: true}).bind('click keydown', function(event) {
+ //code for on enter keypress & submit button
+ var modalWindow = $(this);
+ //e.which === 1 says button is clicked
+ if (event.keyCode === 13 || event.which === 1) {
+ if ($(event.target).hasClass('btn-primary') || event.keyCode === 13) {
+ event.preventDefault();
+ $("#modal-form").ajaxSubmit({
+ beforeSubmit: function() {
+ $(".loading").removeClass('hidden');
+ },
+ success: function(msg) {
+ if (msg['message_type'] === 'success') {
+ if (msg['action'] === 'append') {
+ $(msg['html']).insertBefore($('.divider'));
+ toastr.success(msg['message']);
+ } else if (msg['action'] === 'redirect'){
+ window.location = msg['url'];
+ } else if (msg['action'] === 'noop') {
+ toastr.success(msg['message']);
+ }
+ $(modalWindow).modal('hide');
+ $(modalWindow).empty(); //remove modal window once playlist is created.
+ } else if (msg['message_type'] === 'error') {
+ $(modalWindow).html(msg['html']);
+ } else if (msg['message_type'] === 'server-error') {
+ toastr.error(msg['message']);
+ }
+ },
+ error: function(msg) {
+ toastr.error("Please try after sometime");
+ $(modalWindow).modal('hide');
+ }
+ });
+ }
+ }
+ });
+ }
+ }
+ },
+ error: function(msg) {}
+ });
+ }
+ $("a.extend-playlist").on('click', function(event) {
+ event.preventDefault();
+ playlist_action($(this).attr('href'), $(this).attr('data-method'));
+
+ });
+ });
+</script>
{%- endblock %}
@@ -233,7 +233,6 @@ <h1>{{ video.title }}</h1>
{%- endblock %} <!-- /block main -->
{% block footerscripts %}
- <script type="text/javascript" src="http://connect2campus.in/slides/swfobject.js"></script>
{% if g.user %}
<script type="text/javascript">
$(function() {
@@ -48,7 +48,7 @@ <h3>Speakers <small>Tag speakers to this video</small></h3>
<li id="no-speakers" {%- if speakers %} class="hidden"{% endif %}><em>(No speakers have been tagged)</em></li>
</ol>
<div class="form-inline">
- <input type="text" class="span4" id="speaker_name" placeholder="Username or email">
+ <select multiple="multiple" class="span4" id="speaker_name" placeholder="Username or email"></select>
<button class="btn" id="add-speaker" disabled="disabled"><i class="icon-plus"></i> Add</button>
<span class="loading hidden">&nbsp;</span>
</div>
@@ -98,6 +98,7 @@ <h3>Speakers <small>Tag speakers to this video</small></h3>
{% endblock %}
{% block footerscripts %}
{{ richtext_editor(form.description) }}
+ <script src="https://raw.github.com/meltingice/ajax-chosen/master/lib/ajax-chosen.min.js"></script>
<script type="text/javascript">
$(function() {
// Ajax call to server
@@ -136,9 +137,25 @@ <h3>Speakers <small>Tag speakers to this video</small></h3>
},
});
};
+ /* AJAX CHOSEN work in progress
+ $("#speaker_name").ajaxChosen({
+ type: "GET",
+ url: "https://auth.hasgeek.com/api/1/user/autocomplete",
+ minTermLength: 2,
+ jsonTermKey: 'q'
+ //data: {csrf_token: $('input[name="csrf_token"]').val()}
+ }, function (data) {
+ var results = {};
+ $.each(data, function (i, val) {
+ results[i] = val;
+ });
+
+ return results;
+ console.log(data);
+ }); */
//Enable Add button on user input, disable if empty
- $('#speaker_name').on('keydown', function(event){
+ $('#speaker_name').on('keypress', function(event){
if (event.keyCode === 13) {
event.preventDefault();
if ($("#speaker_name").val() !== '') {
View
@@ -5,7 +5,7 @@
import requests
from werkzeug import secure_filename
-from flask import render_template, flash, escape
+from flask import render_template, flash, escape, request, jsonify
from coaster.views import load_model, load_models
from baseframe.forms import render_redirect, render_form, render_delete_sqla
from hgtv import app
@@ -49,7 +49,7 @@ def inner(start_index=1, max_result=50, total=0):
# If the video is private still youtube provides the title but doesn't
# provide thumbnail & urls, check for private video
is_private = item.get('app$control')
- if is_private is not None and is_private['yt$state']['reasonCode']:
+ if is_private is not None and is_private.get('yt$state') and is_private.get('yt$state').get('reasonCode'):
continue
video = Video(playlist=playlist)
video.title = item['title']['$t']
@@ -60,12 +60,14 @@ def inner(start_index=1, max_result=50, total=0):
if video_content['yt$name'] == 'mqdefault':
thumbnail_url_request = requests.get(video_content['url'])
filestorage = return_werkzeug_filestorage(thumbnail_url_request,
- filename=secure_filename(item['title']['$t']))
+ filename=secure_filename(item['title']['$t']) or 'name-missing')
video.thumbnail_path = thumbnails.save(filestorage)
video.video_sourceid = item['media$group']['yt$videoid']['$t']
video.video_source = u"youtube"
video.make_name()
- playlist.videos.append(video)
+ if not Video.query.filter_by(video_url=video.video_url, playlist=playlist).first():
+ # check for dupliaction
+ playlist.videos.append(video)
#When no more data is present to retrieve in playlist 'feed' is absent in json
if 'entry' in r.json['feed']:
total += len(r.json['feed']['entry'])
@@ -79,8 +81,7 @@ def inner(start_index=1, max_result=50, total=0):
raise DataProcessingError("Unable to establish connection")
except gaierror:
raise DataProcessingError("Unable to resolve the hostname")
- except KeyError:
- raise
+ except KeyError, key:
raise DataProcessingError("Supplied youtube URL doesn't contain video information")
else:
raise ValueError("Unsupported video site")
@@ -173,3 +174,37 @@ def playlist_import(channel):
return render_redirect(playlist.url_for(), code=303)
return render_form(form=form, title="Import Playlist", submit=u"Import",
cancel_url=channel.url_for(), ajax=False)
+
+
+@app.route('/<channel>/<playlist>/extend', methods=['GET', 'POST'])
+@lastuser.requires_login
+@load_models(
+ (Channel, {'name': 'channel'}, 'channel'),
+ (Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist'),
+ permission='edit')
+def playlist_extend(channel, playlist):
+ form = PlaylistImportForm()
+ form.channel = channel
+ html = render_template('playlist-extend.html', form=form, channel=channel, playlist=playlist)
+ if request.is_xhr:
+ if form.validate_on_submit():
+ playlist_url = escape(form.playlist_url.data)
+ initial_count = len(playlist.videos)
+ try:
+ process_playlist(playlist_url=playlist_url, playlist=playlist)
+ except:
+ return jsonify({'message_type': "server-error",
+ 'message': 'Oops, something went wrong, please try after some time'})
+ additions = (len(playlist.videos) - initial_count)
+ if additions:
+ db.session.commit()
+ flash(u"Added '%d' videos" % (len(playlist.videos) - initial_count), 'success')
+ return jsonify({'message_type': "success", 'action': 'redirect', 'url': playlist.url_for()})
+ return jsonify({'message_type': "success", 'action': 'noop', 'message': 'Already upto date'})
+ if form.errors:
+ html = render_template('playlist-extend.html', form=form, channel=channel, playlist=playlist)
+ return jsonify({'message_type': "error", 'action': 'append',
+ 'html': html})
+ return jsonify ({'action': 'modal-window', 'message_type': 'success', 'html': html})
+ return html
+

0 comments on commit 576b602

Please sign in to comment.