def context_processor():
    return {
        'user': g.user,
        'archived_playlist': UserController.get_archived_playlist(),
        'playlist_number': PlaylistController.get_playlist_number(),
        'today': datetime.now().replace(microsecond=0),
    }
def google_authorized(response):
    if not response:
        return u'Failed to login using google OAuth'

    if 'next' in session:
        next_url = session.pop('next')
    else:
        next_url = url_for('main.index')
    session[GOOGLE_TOKEN_NAME] = (response.get('access_token'), '')

    result = UserController.register_google(response)
    if result.get(u'ok', False):
        user = result.get(u'user')

        login_request = UserController.login(user_info=user)
        if login_request:
            if result.get(u'created', False):
                # 새로운 회원으로 가입 되었을때
                playlist = PlaylistController.create_default_playlist(user=user)
                flash(u'%s, 가입을 진심으로 환영합니다!' % user.id_str)
                if not playlist.get(u'ok', True):
                    flash(u'기본 플레이리스트가 생성되지 않았습니다.\n%s' % playlist.get(u'msg', u''))

            else:
                # 기존 회원일때
                flash(u'%s, 재방문을 진심으로 환영합니다!' % user.id_str)
        else:
            flash(u'로그인에 실패하였습니다.')

    else:
        # 뭔가 잘못됨.
        flash(result.get(u'msg'))

    return redirect(next_url)
def bookmarklet():
    opt = {
        'is_login': False,
        'is_active': False,
        'done': False,
    }

    if g.user:
        opt['is_login'] = True

        if g.user.active:
            opt['is_active'] = True

    if request.method == 'GET':
        opt['address'] = request.args.get('url', '')

        media_tuple = MediaController.inspect_url(form_data=opt)
        opt['result'] = MediaController.route_parser(media_tuple=media_tuple)

    elif request.method == 'POST':
        inspected_data = MediaController.inspect_url(request.form)
        created_media, created_flag = MediaController.get_or_create(inspected_data)

        playlist = UserController.get_archived_playlist()
        success = PlaylistController.add_media(playlist=playlist, media=created_media)
        if success:
            opt['done'] = True
        else:
            opt['fail'] = True

    return render_template("media/bookmarklet.html", **opt)
def media_list(id_int):
    playlist = PlaylistController.get_playlist_by_id_int(id_int)
    all_media = playlist.media_list

    res_type = request.args.get('v', None)
    if res_type == 'json':
        tmp_list = []
        for media in all_media:
            tmp_list.append({
                'media_type': media.media_type,
                'title': media.title,
                'duration': media.duration,
                'id_str': media.id_str,
                'url': media.url,
            })

        return jsonify(media_list=tmp_list)

    else:
        opt = {
            'playlist': playlist,
            'media_list': all_media,
        }

        return render_template('playlist/medialist.html', **opt)
def archive():
    if request.method == 'GET':
        opt = {
            'bookmarklet_address': url_for('media.bookmarklet', _external=True),
        }
        return render_template("media/archive.html", **opt)

    else:
        inspected_data = MediaController.inspect_url(request.form)
        created_media, created_flag = MediaController.get_or_create(inspected_data)

        playlist = UserController.get_archived_playlist()
        success = PlaylistController.add_media(playlist=playlist, media=created_media)
        if not success:
            flash(u'failed to archive')

        return redirect(url_for('user.archived'))