Example #1
0
def surf():

    if current_user.is_authenticated:
        current_user.login_update()

    surfMode = request.args.get("surfMode")
    requestURL = ""
    c = ""
    lang = ""
    to = ""

    if surfMode == "byLang":

        # 關鍵字:集數
        c = request.args.get("c").replace(" ", "")
        if c == "":
            return redirect("/"), 302

        # 關鍵字:語言
        lang = request.args.get("lang").replace(" ", "")
        if lang == "":
            return redirect("/"), 302

        # API 串接 搜尋
        requestURL = "https://church-music-api.herokuapp.com/api/songs/search?lang=" + \
        lang + "&c=" + c + "&to=&title=&lyrics=&test=0"

    if surfMode == "byTo":

        # 關鍵字:調性
        to = request.args.get("to")
        if to == "":
            return redirect("/"), 302

        # API 串接 搜尋
        requestURL = "https://church-music-api.herokuapp.com/api/songs/search?lang=&c=&to=" + to + "&title=&lyrics=&test=0"

    r = requests.get(requestURL)

    if not r.status_code == 200:
        result = []
    else:
        result = json.loads(r.text)

    try:
        return render_template("songs/songs.html",
                               songs=result,
                               songs_num=len(result),
                               mode="surf",
                               c=c,
                               to=to,
                               lang=lang,
                               surfMode=surfMode), r.status_code

    except TemplateNotFound:
        abort(404)
Example #2
0
def user_songlist():

    if current_user.is_authenticated:
        current_user.login_update()

    try:
        return render_template("songs/list_of_songlist.html"), 200

    except TemplateNotFound:
        abort(404)
Example #3
0
def workspace():

    # 更新使用者登入時間
    if current_user.is_authenticated:
        current_user.login_update()

    if not current_user.is_manager:
        flash("您並沒有權限。", "danger")
        return redirect("/")

    if request.method == "GET":
        return render_template("admin/workspace.html")
Example #4
0
def users():

    # 更新使用者登入時間
    if current_user.is_authenticated:
        current_user.login_update()

    if not current_user.is_admin:
        flash("您並沒有管理員權限。", "danger")
        return redirect("/")

    if request.method == "GET":
        users = User.query.order_by(User.id).all()
        return render_template("admin/users.html", users=users)
Example #5
0
def logout():
    try:
        if current_user.is_authenticated:
            current_user.login_update()

        return_url = request.values.get('return_url')

        logout_user()
        flash("您已成功登出", "primary")
        return redirect(return_url), 302
    except:
        flash("發生錯誤,請回報管理員", "danger")
        return redirect("/"), 302
Example #6
0
def song_list_by_id(out_id):

    if current_user.is_authenticated:
        current_user.login_update()

    songs = []

    songlist = SongList.query.filter_by(out_id=out_id).first()

    if not songlist:
        flash("錯誤的歌單資訊", "danger")
        return redirect("/")

    listowner = User.query.filter_by(id=songlist.user_id).first()

    if songlist.is_private:
        if not current_user.is_authenticated:
            flash("您目前造訪的是一份私人歌單,<br>請先確認您擁有此歌單的權限。", "danger")
            return redirect("/")

        if current_user.id != listowner.id:
            flash("您目前造訪的是一份私人歌單,<br>請先確認您擁有此歌單的權限。", "danger")
            return redirect("/")

    sids = ""
    for i in range(len(songlist.songs_sid_list)):
        if i == 0:
            sids += songlist.songs_sid_list[i]
        else:
            sids += "+" + songlist.songs_sid_list[i]

    requestURL = "https://church-music-api.herokuapp.com/api/songs/sid/" + sids
    r = requests.get(requestURL)
    if r.status_code == 200:
        songs = json.loads(r.text)
    elif r.status_code == 404:
        songs = []

    old_description = songlist.description

    new_description = old_description.replace("\r\n", "<br>")

    try:
        return render_template("songs/songlist.html",
                               songs=songs,
                               songlist=songlist,
                               listowner=listowner,
                               new_description=new_description), 200

    except TemplateNotFound:
        abort(404)
Example #7
0
def update_songlist(song_sid, songlist_outid):

    if current_user.is_authenticated:
        current_user.login_update()

    if request.method == "PUT":

        songlist = SongList.query.filter_by(out_id=songlist_outid).first()

        if not songlist:
            return jsonify({"success": False, "message": "wrong out_id"})

        tempList = songlist.songs_sid_list
        songlist.songs_sid_list = None
        songlist.update()
        songlist.refresh()

        if song_sid in tempList:
            tempList.remove(song_sid)
            songlist.songs_amount -= 1

            songlist.songs_sid_list = tempList

            songlist.update()

            return jsonify({
                "success": True,
                "act": "remove",
                "song": song_sid,
                "songlist": songlist_outid
            })

        else:
            tempList.append(song_sid)
            songlist.songs_amount += 1

            songlist.songs_sid_list = tempList

            songlist.update()

            return jsonify({
                "success": True,
                "act": "append",
                "song": song_sid,
                "songlist": songlist_outid
            })

    else:

        return jsonify({"success": False, "message": "wrong method"})
Example #8
0
def surfer():

    if current_user.is_authenticated:
        current_user.login_update()

    # Tonality Collection
    toColl = [
        "C", "Cm", "C#", "D", "Dm", "Db", "E", "Em", "Eb", "F", "Fm", "F#m",
        "G", "Gm", "Gb", "A", "Am", "Ab", "B", "Bm", "Bb"
    ]

    try:
        return render_template("pages/surfer.html", toColl=toColl), 200
    except TemplateNotFound:
        abort(404)
Example #9
0
def download_sheet(sid):

    if current_user.is_authenticated:
        current_user.login_update()

    return_url = request.args.get("next")

    try:
        result = dbx.files_get_temporary_link("/CatenMusic_Data/Sheet/" + sid +
                                              ".pdf")

        return redirect(result.link), 302

    except Exception as no_pdf_error:
        print(no_pdf_error)
        flash("很抱歉,這首歌目前沒有歌譜。", "warning")
        return redirect(return_url)
Example #10
0
def seeHome():

    result = []

    random_amount = str(6)

    if current_user.is_authenticated:
        current_user.login_update()

        requestURL = "https://church-music-api.herokuapp.com/api/songs/random/" + random_amount
        r = requests.get(requestURL)

        if r.status_code == 200:
            result = json.loads(r.text)

    env_state = os.environ.get("APP_SETTING")
    return render_template("pages/home.html",
                           songs=result,
                           env_state=env_state), 200
Example #11
0
def surf_one(sid):

    if current_user.is_authenticated:
        current_user.login_update()

    # API

    requestURL = "https://church-music-api.herokuapp.com/api/songs/sid/" + sid

    r = requests.get(requestURL)

    if not r.status_code == 200:
        return redirect("/")
    else:
        result = json.loads(r.text)

    try:
        return render_template("songs/songs.html", songs=result,
                               mode="one"), r.status_code
    except TemplateNotFound:
        abort(404)
Example #12
0
def download_ppt(sid):

    if current_user.is_authenticated:
        current_user.login_update()

    return_url = request.args.get("next")

    try:
        result = dbx.files_get_temporary_link("/CatenMusic_Data/PPT/ppt_" +
                                              sid + ".ppt")
        return redirect(result.link), 302

    except Exception as no_ppt_error:
        pass

    try:
        result = dbx.files_get_temporary_link("/CatenMusic_Data/PPT/ppt_" +
                                              sid + ".pptx")
        return redirect(result.link), 302

    except Exception as no_pptx_error:
        flash("很抱歉,這首歌目前沒有投影片。", "warning")
        return redirect(return_url)
Example #13
0
def add_songlist():

    if current_user.is_authenticated:
        current_user.login_update()

    if request.method == "POST":
        posttype = request.values.get("posttype")
        title = request.values.get("title")
        privacy = request.values.get("privacy")

        is_private = False

        if posttype == "withsong":
            song_sid = request.values.get("song_sid")
            if privacy == "private":
                is_private = True

            new_songlist = SongList(title=title,
                                    user=current_user,
                                    songs_sid_list=[song_sid],
                                    songs_amount=1,
                                    is_private=is_private)

            new_songlist.flush()

            new_songlist.init()

            new_songlist.save()

            return render_template("songs/list_of_songlist.html"), 200

    else:
        try:
            return render_template("songs/list_of_songlist.html"), 200

        except TemplateNotFound:
            abort(404)
Example #14
0
def delete(out_id):

    if current_user.is_authenticated:
        current_user.login_update()

    if request.method == "POST":

        songlist = SongList.query.filter_by(out_id=out_id).first()

        if not songlist:
            flash("錯誤的歌單資訊", "danger")
            return redirect("/")

        if songlist.user_id == current_user.id:
            songlist.kill_self()
            flash("已成功刪除歌單 #" + out_id, "success")
            return redirect(url_for("user_songlist_bp.user_songlist"))

        else:
            flash("想幹嘛?您沒有權限刪除此歌單。<br>請登入後再試一次。", "danger")

    else:

        return redirect("/")
Example #15
0
def add():

    # 更新使用者登入時間
    if current_user.is_authenticated:
        current_user.login_update()

    # 確認使用者擁有管理員權限
    if not current_user.is_manager:
        flash("很抱歉,您並沒有新增歌曲的權限。", "danger")
        return redirect("/")

    if request.method == "GET":

        # Tonality Collection
        toColl = [
            "C", "Cm", "C#", "D", "Dm", "Db", "E", "Em", "Eb", "F", "Fm",
            "F#m", "G", "Gm", "Gb", "A", "Am", "Ab", "B", "Bm", "Bb"
        ]

        return render_template("admin/song_create.html", toColl=toColl)

    elif request.method == "POST":
        try:

            # 取得表單資料
            # 新增歌曲時,可以自訂語言、集數, 首數及 SID 為自動產生
            title = request.values.get("title")
            num_c = request.values.get("num_c")
            language = request.values.get("language")
            originalTitleOriginal = request.values.get("title_original")
            scripture = request.values.get("scripture")
            tonality = request.values.get("tonality")
            year = request.values.get("year")
            lyricist = request.values.get("lyricist")
            composer = request.values.get("composer")
            translator = request.values.get("translator")
            album = request.values.get("album")
            publisher = request.values.get("publisher")
            publisher_original = request.values.get("publisher_original")
            tempo = request.values.get("tempo")
            time_signature = request.values.get("time_signature")

            originLyrics = request.values.get("lyrics")

            title_original_old = originalTitleOriginal.split("/")
            title_original = []
            for title_o in title_original_old:
                title_o = title_o.strip()
                if len(title_o) > 0:
                    title_original.append(title_o)

            lyrics_old = originLyrics.split("\n")
            lyrics = []

            lyrics_len = len(lyrics_old)
            for i in range(lyrics_len):
                p = lyrics_old[i]
                if len(p) > 0:
                    p = p.strip()
                    p = p.replace("\n", "")
                    p = p.replace("\r", "")
                if len(p) > 0:
                    lyrics.append(p)

            mostAdminToken = os.environ.get("SONGS_DB_MOST_ADMIN_TOKEN")

            reqBase = "https://church-music-api.herokuapp.com/"
            # reqBase = "http://localhost:7700/"

            reqURL = reqBase + "api/songs/search?lang=" + language + "&c=" + num_c + "&to=&title=&lyrics=&test=0"
            searchRes = json.loads(requests.get(reqURL).text)

            newNumI = 0
            if type(searchRes) == type([]):
                currentCollecAmount = len(searchRes)
                newNumI = currentCollecAmount + 1
            elif searchRes["Code"] == 1600:
                newNumI = 1

            newNumI = str(newNumI)

            newSong = {
                # "sid": sid, # SID 由後端產生
                "num_c": num_c,
                "num_i": newNumI,
                "title": title,
                "title_original": title_original,
                "scripture": scripture,
                "year": year,
                "lyricist": lyricist,
                "composer": composer,
                "translator": translator,
                "lyrics": lyrics,
                "tonality": tonality,
                "tempo": tempo,
                "time_signature": time_signature,
                "album": album,
                "publisher": publisher,
                "publisher_original": publisher_original,
                "language": language,
                "token": mostAdminToken,
            }

            for key, value in newSong.items():
                if type(value) == type("string"):
                    newSong[key] = newSong[key].strip()

            newSong_json = json.dumps(newSong)

            print(newSong_json)

            postURL = "https://church-music-api.herokuapp.com/api/songs"
            # postURL = "http://0.0.0.0:7700/api/songs"

            r_post = requests.post(postURL, newSong_json)

            response = json.loads(r_post.text)
            # return r_post.text

            newSID = response["NewSID"]

            return_url = request.values.get("next")
            if not helper.is_safe_url(return_url):
                flash("不安全的連結", "danger")
                return abort(400)
            else:
                flash("成功新增歌曲 #" + newSID, "success")
                if not return_url:
                    return redirect("/")
                else:
                    return redirect(return_url)

        except Exception as error:
            print(error)
            flash("發生錯誤,請洽網站管理員。", "danger")
            return redirect("/")
Example #16
0
def edit(id_):

    # 更新使用者登入時間
    if current_user.is_authenticated:
        current_user.login_update()

    if not current_user.is_admin:
        flash("您並沒有總管理員權限。", "danger")
        return redirect("/")

    if request.method == "GET":
        user = User.query.filter_by(id=id_).first()
        return render_template("admin/user_edit.html", user=user)

    elif request.method == "POST":

        try:
            user = User.query.filter_by(id=id_).first()
        except:
            flash("錯誤的使用者資訊", "danger")

        displayname = request.values.get("displayname")
        authority = request.values.get("authority")

        # 確認顯示名稱格式
        if re.fullmatch(r"^[\u4e00-\u9fa5_a-zA-Z0-9]{1,17}$", displayname):
            stringLen = 0

            for c in displayname:
                if re.fullmatch(r"^[\u4e00-\u9fa5]+$", c):
                    stringLen += 2
                else:
                    stringLen += 1

            if stringLen <= 16:
                user.displayname = displayname

            else:
                flash("編輯使用者顯示名稱時發生錯誤", "danger")
                return redirect("/")
        else:
            flash("編輯使用者顯示名稱時發生錯誤", "danger")
            return redirect("/")

        # 確認權限
        if authority == "admin":
            user.is_admin = True
            user.is_manager = True
        elif authority == "manager":
            user.is_admin = False
            user.is_manager = True
        elif authority == "normal":
            user.is_admin = False
            user.is_manager = False
        else:
            flash("編輯使用者權限時發生錯誤", "danger")
            return redirect("/")

        user.update()

        return redirect(url_for("users_bp.users"))
Example #17
0
def edit(sid):

    # 更新使用者登入時間
    if current_user.is_authenticated:
        current_user.login_update()

    # 確認使用者擁有管理員權限
    if not current_user.is_manager:
        flash("很抱歉,您並沒有編輯歌曲的權限。", "danger")
        return redirect("/")

    # Get 歌曲資訊
    requestURL = "https://church-music-api.herokuapp.com/api/songs/sid/" + sid

    r = requests.get(requestURL)

    if not r.status_code == 200:
        flash("錯誤的資訊", "danger")
        return redirect("/")
    else:
        result = json.loads(r.text)
        song = result[0]

    if request.method == "GET":

        return_url = request.values.get("next")

        # Tonality Collection
        toColl = [
            "C", "Cm", "C#", "D", "Dm", "Db", "E", "Em", "Eb", "F", "Fm",
            "F#m", "G", "Gm", "Gb", "A", "Am", "Ab", "B", "Bm", "Bb"
        ]

        # Get original titles
        song_title_original = ""
        len_title_o = len(song["title_original"])
        if len_title_o > 0:
            for i in range(len_title_o):
                if i == (len_title_o - 1):
                    song_title_original += song["title_original"][i]
                else:
                    song_title_original += song["title_original"][i] + " / "

        # Get lyrics
        song_lyrics = ""
        for p in song["lyrics"]:
            song_lyrics += p + "\n"

        return render_template("admin/song_edit.html",
                               song=song,
                               toColl=toColl,
                               song_lyrics=song_lyrics,
                               song_title_original=song_title_original,
                               return_url=return_url)

    elif request.method == "POST":

        try:

            return_url = request.values.get("return_url")

            # 編輯歌曲時,不可更改語言、編號、sid
            # num_c = request.values.get("num_c")
            # num_i = request.values.get("num_i")
            # language = request.values.get("language")

            num_c = song["num_c"]
            num_i = song["num_i"]
            language = song["language"]

            # 取得表單資料
            title = request.values.get("title")
            originalTitleOriginal = request.values.get("title_original")
            scripture = request.values.get("scripture")
            tonality = request.values.get("tonality")
            year = request.values.get("year")
            lyricist = request.values.get("lyricist")
            composer = request.values.get("composer")
            translator = request.values.get("translator")
            album = request.values.get("album")
            publisher = request.values.get("publisher")
            publisher_original = request.values.get("publisher_original")
            tempo = request.values.get("tempo")
            time_signature = request.values.get("time_signature")

            originLyrics = request.values.get("lyrics")

            # 編輯歌曲時,不可更改語言、編號、sid
            # sid = ""
            # if language == "Chinese":
            #     sid += "1"
            # else:
            #     sid += "2"

            # if len(num_c) < 2:
            #     sid += "00" + num_c
            # else:
            #     sid += "0" + num_c

            # if len(num_i) < 2:
            #     sid += "00" + num_i
            # else:
            #     sid += "0" + num_i

            title_original_old = originalTitleOriginal.split("/")
            title_original = []
            for title_o in title_original_old:
                title_o = title_o.strip()
                if len(title_o) > 0:
                    title_original.append(title_o)

            lyrics_old = originLyrics.split("\n")
            lyrics = []

            lyrics_len = len(lyrics_old)
            for i in range(lyrics_len):
                p = lyrics_old[i]
                if len(p) > 0:
                    p = p.strip()
                    p = p.replace("\n", "")
                    p = p.replace("\r", "")
                if len(p) > 0:
                    lyrics.append(p)

            mostAdminToken = os.environ.get("SONGS_DB_MOST_ADMIN_TOKEN")

            newSong = {
                "sid": sid,
                "num_c": num_c,
                "num_i": num_i,
                "title": title,
                "title_original": title_original,
                "scripture": scripture,
                "year": year,
                "lyricist": lyricist,
                "composer": composer,
                "translator": translator,
                "lyrics": lyrics,
                "tonality": tonality,
                "tempo": tempo,
                "time_signature": time_signature,
                "album": album,
                "publisher": publisher,
                "publisher_original": publisher_original,
                "language": language,
                "token": mostAdminToken,
            }

            newSong_json = json.dumps(newSong)

            putURL = "https://church-music-api.herokuapp.com/api/songs/sid/" + sid
            # putURL = "http://0.0.0.0:7700/api/songs/sid/" + sid

            r_put = requests.put(putURL, newSong_json)

            response = json.loads(r_put.text)
            # return r_put.text

            if not helper.is_safe_url(return_url):
                flash("不安全的連結", "danger")
                return abort(400)
            else:
                if not return_url:
                    return redirect("/")
                flash("成功編輯歌曲 #" + sid, "success")
                return redirect(return_url)

        except Exception as error:
            print(error)
            flash("發生錯誤,請洽網站管理員。", "danger")
            return redirect("/")
Example #18
0
def report_song(sid):

    # 更新使用者登入時間
    if current_user.is_authenticated:
        current_user.login_update()

    requestURL = "https://church-music-api.herokuapp.com/api/songs/sid/" + sid

    r = requests.get(requestURL)

    if not r.status_code == 200:
        return redirect("/")
    else:
        result = json.loads(r.text)
        song = result[0]

    # method == "POST"
    if request.method == "POST":

        # 取得登入表單資料
        try:
            report_description = request.values.get("report_description")
            next_url = request.values.get("next_url")

            if len(report_description) < 5:
                next_url = next_url.replace('/', '%2F').replace(
                    '?', '%3F').replace('=', '%3D').replace('&', '%26')
                flash("回報的內容至少需要5個字。", "danger")
                return redirect(
                    url_for("report_bp.report_song", sid=sid) + '?next=' +
                    next_url)

        # 當有人故意送出奇怪的request
        except Exception as error:
            print(error)
            return render_template("error/403.html",
                                   error_message="Don't Play With Me."), 403

        song_report = SongReport(description=report_description,
                                 user_id=current_user.id,
                                 song_sid=song["sid"])
        song_report.save()
        print(song_report)

        if next_url == "None":
            flash("已順利回報歌曲問題 #" + sid, "success")
            return redirect("/"), 302

        if not helper.is_safe_url(next_url):
            return abort(400)
        else:
            flash("已順利回報歌曲問題 #" + sid, "success")
            return redirect(next_url), 302

    # method == "GET"
    else:
        try:
            return render_template('report/report_song.html',
                                   next_url=request.args.get("next"),
                                   song=song), 200

        except TemplateNotFound:
            abort(404)
Example #19
0
def edit(out_id):

    if current_user.is_authenticated:
        current_user.login_update()

    songlist = SongList.query.filter_by(out_id=out_id).first()

    if not songlist:
        flash("錯誤的歌單資訊", "danger")
        return redirect("/")

    if current_user.id != songlist.user_id:
        flash("想幹嘛?你沒有權限編輯此歌單。<br>請登入後再試一次。", "danger")
        return redirect("/")

    if request.method == "GET":

        songs = []

        sids = ""
        for i in range(len(songlist.songs_sid_list)):
            if i == 0:
                sids += songlist.songs_sid_list[i]
            else:
                sids += "+" + songlist.songs_sid_list[i]

        requestURL = "https://church-music-api.herokuapp.com/api/songs/sid/" + sids
        r = requests.get(requestURL)
        if r.status_code == 200:
            songs = json.loads(r.text)
        elif r.status_code == 404:
            songs = []

        return render_template("songs/songlist_edit.html",
                               songlist=songlist,
                               songs=songs)

    elif request.method == "POST":

        title = request.values.get("title")

        description = request.values.get("description")

        is_private = False

        if request.values.get("privacy") == "private":
            is_private = True

        is_archived = False

        if request.values.get("archive") == "archived":
            is_archived = True

        songs_amount = int(request.values.get("songs_amount"))

        songs_sid_list_new = []

        for index in range(songs_amount):
            songs_sid_list_new.append(request.values.get(str(index)))

        songlist.title = title
        songlist.description = description
        songlist.is_private = is_private
        songlist.is_archived = is_archived
        songlist.songs_amount = songs_amount

        songlist.songs_sid_list = None
        songlist.update()
        songlist.refresh()

        songlist.songs_sid_list = songs_sid_list_new

        songlist.update()

        return redirect(
            url_for("song_list_by_id_bp.song_list_by_id", out_id=out_id))