<commit_before>from wpc import db, app, socketio
from wpc.flask_utils import url_for_other_page, url_change_args, nl2br, nl2br_py, get_or_create, is_safe_url
from wpc.models import MozillaStreamHack  # NOQA
from wpc.models import YoutubeStream, WPCStream, Stream, Streamer, Subscriber, Idea, ChatMessage
from wpc.forms import SubscribeForm, GLMSubscribeForm, EditStreamerInfoForm, EditStreamTitleForm, SearchForm, IdeaForm, RtmpRedirectForm

from flask import render_template, request, redirect, url_for, flash, jsonify, g, Response, session, abort
from flask.ext.login import login_user, logout_user, login_required, current_user
from flask.ext.socketio import emit, join_room
from jinja2 import Markup

from uuid import uuid4
import praw
import random
from feedgen.feed import FeedGenerator
from datetime import datetime
import pytz
import uuid

app.jinja_env.globals['url_for_other_page'] = url_for_other_page
app.jinja_env.globals['url_change_args'] = url_change_args
app.add_template_filter(nl2br)


@app.before_request
def add_ga_tracking_code():
    g.ga_tracking_code = app.config['GA_TRACKING_CODE']


@app.before_request
def create_search_form():
    g.search_form = SearchForm()


@app.before_request
def add_rtmp_secret():
    if current_user.is_authenticated() and not current_user.rtmp_secret:
        current_user.rtmp_secret = str(uuid.uuid4())
        wpcs = get_or_create(WPCStream, channel_name=current_user.reddit_username)
        current_user.streams.append(wpcs)
        db.session.add(wpcs)
        wpcs.status = 'completed'
        db.session.commit()


@app.after_request
def set_subscribing_cookies(response):
    subscribe_send_only_id = current_user.is_authenticated() and current_user.as_subscriber
    response.set_cookie("subscribe_send_only_id", value="true" if subscribe_send_only_id else "false")
    return response


def process_idea_form(idea_form):
    if idea_form.submit_button.data and idea_form.validate_on_submit():
        idea = Idea()
        idea_form.populate_obj(idea)
        db.session.add(idea)
        db.session.commit()
        flash("Your idea was added successfully", "success")
        return redirect(url_for("idea_list"))


@app.route('/streaming_guide')
def streaming_guide():
    return render_template("streaming_guide.html")


@app.route('/', methods=['GET', 'POST'])
def index():
    live_streams = Stream.query.filter_by(status='live').order_by(Stream.actual_start_time.desc().nullslast(), Stream.id.desc()).all()
    # Uncomment this when mozilla guys start livestreaming
    # live_streams.insert(0, MozillaStreamHack())
    idea_form = IdeaForm(prefix='idea')
    redir = process_idea_form(idea_form)
    if redir:
        return redir

    subscribe_form = SubscribeForm(prefix='subscribe')
    if subscribe_form.submit_button.data and subscribe_form.validate_on_submit():
        subscriber = Subscriber()
        subscribe_form.populate_obj(subscriber)
        db.session.add(subscriber)
        db.session.commit()
        flash("you've subscribed successfully", "success")
        return redirect(url_for('.index'))

    random_stream = YoutubeStream.query.filter(YoutubeStream.status != 'upcoming').order_by(db.func.random()).first()
    upcoming_streams = Stream.query.filter_by(status='upcoming').order_by(Stream.scheduled_start_time.asc()).all()
    regular_streamer = Streamer.query.filter_by(reddit_username='glm_talkshow').one()
    return render_template('index.html', subscribe_form=subscribe_form, idea_form=idea_form, live_streams=live_streams,
                           random_stream=random_stream,
                           upcoming_streams=upcoming_streams,
                           regular_streamer=regular_streamer)


@app.route('/idea_list', methods=['GET', 'POST'])
def idea_list():
    ideas = Idea.query.order_by(Idea.id.desc()).all()

    idea_form = IdeaForm(prefix='idea')
    redir = process_idea_form(idea_form)
    if redir:
        return redir

    return render_template("idea_list.html", ideas=ideas, idea_form=idea_form)


# TODO it is copypasted from index(), but whatever, this is one time change
@app.route('/onlineconf', methods=['GET', 'POST'])
def onlineconf():
    streams = YoutubeStream.query.filter_by(confstream=True).filter(
        Stream.status == 'completed').order_by(
        Stream.actual_start_time.desc().nullsfirst(),
        Stream.id.desc()).all()

    form = SubscribeForm()
    if request.method == "POST" and form.validate_on_submit():
        subscriber = Subscriber()
        form.populate_obj(subscriber)
        db.session.add(subscriber)
        db.session.commit()
        flash("you've subscribed successfully", "success")
        return redirect(url_for('.index'))

    return render_template('onlineconf.html', form=form, streams=streams)


@app.route('/search', methods=['GET', 'POST'])
def search():
    if g.search_form.validate_on_submit():
        return redirect(url_for("past_streams", query=g.search_form.query.data.strip()))
    else:
        # Should never happen, unless user requested /search manually
        return redirect(url_for("past_streams"))


@app.route('/past_streams', defaults={'page': 1, 'query': None})
@app.route('/past_streams/query/<query>', defaults={'page': 1})
@app.route('/past_streams/page/<int:page>', defaults={'query': None})
@app.route('/past_streams/query/<query>/page/<int:page>')
def past_streams(query, page):
    streams = YoutubeStream.query.filter_by(status='completed')

    if query:
        terms = [t.strip() for t in query.split(" ")]<commit_msg>split search query with whitspaces<commit_after>from wpc import db, app, socketio
from wpc.flask_utils import url_for_other_page, url_change_args, nl2br, nl2br_py, get_or_create, is_safe_url
from wpc.models import MozillaStreamHack  # NOQA
from wpc.models import YoutubeStream, WPCStream, Stream, Streamer, Subscriber, Idea, ChatMessage
from wpc.forms import SubscribeForm, GLMSubscribeForm, EditStreamerInfoForm, EditStreamTitleForm, SearchForm, IdeaForm, RtmpRedirectForm

from flask import render_template, request, redirect, url_for, flash, jsonify, g, Response, session, abort
from flask.ext.login import login_user, logout_user, login_required, current_user
from flask.ext.socketio import emit, join_room
from jinja2 import Markup

from uuid import uuid4
import praw
import random
from feedgen.feed import FeedGenerator
from datetime import datetime
import pytz
import uuid

app.jinja_env.globals['url_for_other_page'] = url_for_other_page
app.jinja_env.globals['url_change_args'] = url_change_args
app.add_template_filter(nl2br)


@app.before_request
def add_ga_tracking_code():
    g.ga_tracking_code = app.config['GA_TRACKING_CODE']


@app.before_request
def create_search_form():
    g.search_form = SearchForm()


@app.before_request
def add_rtmp_secret():
    if current_user.is_authenticated() and not current_user.rtmp_secret:
        current_user.rtmp_secret = str(uuid.uuid4())
        wpcs = get_or_create(WPCStream, channel_name=current_user.reddit_username)
        current_user.streams.append(wpcs)
        db.session.add(wpcs)
        wpcs.status = 'completed'
        db.session.commit()


@app.after_request
def set_subscribing_cookies(response):
    subscribe_send_only_id = current_user.is_authenticated() and current_user.as_subscriber
    response.set_cookie("subscribe_send_only_id", value="true" if subscribe_send_only_id else "false")
    return response


def process_idea_form(idea_form):
    if idea_form.submit_button.data and idea_form.validate_on_submit():
        idea = Idea()
        idea_form.populate_obj(idea)
        db.session.add(idea)
        db.session.commit()
        flash("Your idea was added successfully", "success")
        return redirect(url_for("idea_list"))


@app.route('/streaming_guide')
def streaming_guide():
    return render_template("streaming_guide.html")


@app.route('/', methods=['GET', 'POST'])
def index():
    live_streams = Stream.query.filter_by(status='live').order_by(Stream.actual_start_time.desc().nullslast(), Stream.id.desc()).all()
    # Uncomment this when mozilla guys start livestreaming
    # live_streams.insert(0, MozillaStreamHack())
    idea_form = IdeaForm(prefix='idea')
    redir = process_idea_form(idea_form)
    if redir:
        return redir

    subscribe_form = SubscribeForm(prefix='subscribe')
    if subscribe_form.submit_button.data and subscribe_form.validate_on_submit():
        subscriber = Subscriber()
        subscribe_form.populate_obj(subscriber)
        db.session.add(subscriber)
        db.session.commit()
        flash("you've subscribed successfully", "success")
        return redirect(url_for('.index'))

    random_stream = YoutubeStream.query.filter(YoutubeStream.status != 'upcoming').order_by(db.func.random()).first()
    upcoming_streams = Stream.query.filter_by(status='upcoming').order_by(Stream.scheduled_start_time.asc()).all()
    regular_streamer = Streamer.query.filter_by(reddit_username='glm_talkshow').one()
    return render_template('index.html', subscribe_form=subscribe_form, idea_form=idea_form, live_streams=live_streams,
                           random_stream=random_stream,
                           upcoming_streams=upcoming_streams,
                           regular_streamer=regular_streamer)


@app.route('/idea_list', methods=['GET', 'POST'])
def idea_list():
    ideas = Idea.query.order_by(Idea.id.desc()).all()

    idea_form = IdeaForm(prefix='idea')
    redir = process_idea_form(idea_form)
    if redir:
        return redir

    return render_template("idea_list.html", ideas=ideas, idea_form=idea_form)


# TODO it is copypasted from index(), but whatever, this is one time change
@app.route('/onlineconf', methods=['GET', 'POST'])
def onlineconf():
    streams = YoutubeStream.query.filter_by(confstream=True).filter(
        Stream.status == 'completed').order_by(
        Stream.actual_start_time.desc().nullsfirst(),
        Stream.id.desc()).all()

    form = SubscribeForm()
    if request.method == "POST" and form.validate_on_submit():
        subscriber = Subscriber()
        form.populate_obj(subscriber)
        db.session.add(subscriber)
        db.session.commit()
        flash("you've subscribed successfully", "success")
        return redirect(url_for('.index'))

    return render_template('onlineconf.html', form=form, streams=streams)


@app.route('/search', methods=['GET', 'POST'])
def search():
    if g.search_form.validate_on_submit():
        return redirect(url_for("past_streams", query=g.search_form.query.data.strip()))
    else:
        # Should never happen, unless user requested /search manually
        return redirect(url_for("past_streams"))


@app.route('/past_streams', defaults={'page': 1, 'query': None})
@app.route('/past_streams/query/<query>', defaults={'page': 1})
@app.route('/past_streams/page/<int:page>', defaults={'query': None})
@app.route('/past_streams/query/<query>/page/<int:page>')
def past_streams(query, page):
    streams = YoutubeStream.query.filter_by(status='completed')

    if query:
        terms = [t for t in query.split()]
{'fraction_non_alphanumeric': 0.1190733729944114, 'fraction_numerical': 0.0016224986479177934, 'mean_word_length': 4.452088452088452, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6761423', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3464, 'n_words': 662}