Пример #1
0
def delete_userboard(username, board):
    if username != session["username"]: return abort(404)
    removedid = models.Board().delete(
        session["username"],
        board)  # BoardとImg docsが削除され、BoardのIDが返る。...ファイル削除しない方向で...
    models.User().pop_boardid(session["username"], removedid)
    models.Img().delete_multi(session["username"], board)
    return redirect(url_for("userboards", username=session["username"]))
Пример #2
0
def regif(pagename):
    imgdoc = models.Img().fetch_imgdoc(pagename)
    if imgdoc["username"] == session["username"]:
        return abort(404)  # GifのRegifページに管理者ユーザーが接続してきた場合404へ

    form = Regifform(request.form)
    boards_li = models.User().fetch_boards(session["username"])
    form.board.choices = [
        (index, board)
        for (index, board) in zip(range(1,
                                        len(boards_li) + 1), boards_li)
    ]  # 動的にoption要素の値を出力するための下処理

    if request.method == "GET":
        return render_template("regif.html",
                               form=form,
                               item=imgdoc,
                               sign_in_username=session["username"])

    if request.method == "POST" and form.validate() and form.validate_tags(
            request.form.getlist("item[tags][]")):
        index = request.form["board"]
        tags = request.form.getlist("item[tags][]")
        description = request.form["description"]
        regifed_board = boards_li[int(index) - 1]
        fromimgid = str(imgdoc["fromimgid"]) if "fromimgid" in imgdoc else str(
            imgdoc["_id"])

        imgid = models.Img().add(username=session["username"],
                                 filename=imgdoc["filename"],
                                 description=description,
                                 tags=tags,
                                 fromimgid=fromimgid,
                                 board=regifed_board)

        return redirect(
            url_for("userboard",
                    username=session["username"],
                    board=regifed_board))
    else:
        return render_template("regif.html",
                               form=form,
                               item=imgdoc,
                               sign_in_username=session["username"])
Пример #3
0
def gifpage(pagename):
    imgdoc = models.Img().fetch_imgdoc(pagename)
    if imgdoc == None: return abort(404)  # 存在しないGifの個別ページに接続してきた場合404へ

    fromimgdoc = models.Img().fetch_from_imgid(
        imgdoc["fromimgid"]
    ) if "fromimgid" in imgdoc else None  # Noneがassignされる時はNot Regifなページ

    regifedimgdocs = models.Img.fetch_from_imgid(imgdoc["fromimgid"], regif="regif") if "fromimgid" in imgdoc else \
                     models.Img.fetch_from_imgid(str(imgdoc["_id"]), regif="regif")  # オリジナルのGifであることが確定すると、Regifされ生成されたdocsを返す(else以下)

    if regifedimgdocs:
        (regifedimgdocs, cp) = itertools.tee(
            regifedimgdocs
        )  # (オリジナルを)Regifしてできたimgdocsのイテレータobjectのコピーを生成するため分割させる
        regifs = len((list(cp)))
    else:
        regifs = None

    commentdocs = models.Comment.fetch_comments(pagename)

    if "username" not in session:
        return render_template("anonymous/gif.html",
                               item=imgdoc,
                               fromitem=fromimgdoc,
                               regifeditems=regifedimgdocs,
                               regifs=regifs,
                               form1=Sign_upform(request.form),
                               form2=Sign_inform(request.form),
                               icon_filename="usericon.png",
                               commentitems=commentdocs)

    icon_filename = models.Img.fetch_latest_imgdoc(session["username"])
    return render_template("gif.html",
                           item=imgdoc,
                           fromitem=fromimgdoc,
                           regifeditems=regifedimgdocs,
                           regifs=regifs,
                           icon_filename=icon_filename,
                           commentitems=commentdocs,
                           sign_in_username=session["username"])
Пример #4
0
def create_gif():
    form = CreateGifform(request.form)
    boards_li = models.User().fetch_boards(session["username"])
    form.board.choices = [
        (index, board)
        for (index, board) in zip(range(1,
                                        len(boards_li) + 1), boards_li)
    ]  # 動的にoption要素の値を出力するための下処理
    source_url_encoded = urllib.quote_plus(
        request.args.get("source_url"))  # querystrigをURIエンコード

    if request.method == 'GET':
        return render_template("creategif.html",
                               form=form,
                               sign_in_username=session['username'],
                               source_url_encoded=source_url_encoded,
                               source_gifurl=request.args.get("source_gifurl"))

    if request.method == 'POST' and form.validate() and form.validate_tags(
            request.form.getlist("item[tags][]")):
        filename = "".join([
            random.choice(string.ascii_letters + string.digits + '_')
            for i in range(21)
        ]) + ".gif"
        dir = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        urllib.urlretrieve(request.args.get("source_gifurl"), dir)
        agifjudge = Agifjudge(dir)

        if agifjudge.validate() == 'gif/create':  # アニメイトしていないor5M以上のGifの場合の分岐
            return render_template(
                "creategif.html",
                form=form,
                sign_in_username=session['username'],
                source_url_encoded=source_url_encoded,
                source_gifurl=request.args.get("source_gifurl"),
                gif_error=True)

        board = boards_li[int(request.form["board"]) - 1]
        models.Img().add(username=session["username"],
                         filename=filename,
                         description=form.description.data,
                         tags=request.form.getlist("item[tags][]"),
                         board=board,
                         from_externalurl=request.args.get("source_gifurl"))
        return redirect(url_for("index"))

    else:
        return render_template("creategif.html",
                               form=form,
                               sign_in_username=session['username'],
                               source_url_encoded=source_url_encoded,
                               source_gifurl=request.args.get("source_gifurl"))
Пример #5
0
def edit_gifpage(pagename):
    imgdoc = models.Img().fetch_imgdoc(pagename)
    if imgdoc["username"] != session["username"]:
        return abort(404)  # Gifの編集ページに非管理者ユーザーが接続してきた場合404へ

    form = EditGifform(request.form)
    boards_li = models.User().fetch_boards(session["username"])
    form.board.choices = [
        (index, board)
        for (index, board) in zip(range(1,
                                        len(boards_li) + 1), boards_li)
    ]  # 動的にoption要素の値を出力するための下処理

    if request.method == "GET":
        form.description.data = imgdoc["description"]
        return render_template("edit_gif.html",
                               form=form,
                               item=imgdoc,
                               sign_in_username=session["username"])

    if request.method == "POST" and form.validate() and form.validate_tags(
            request.form.getlist("item[tags][]")):
        tags = request.form.getlist("item[tags][]")

        index = request.form["board"]
        newboard = boards_li[int(index) - 1]
        description = request.form["description"]
        models.Img().change_board(pagename=pagename,
                                  newboard=newboard,
                                  tags=tags,
                                  description=description)  # 単一Gifのボード名変更
        return redirect(url_for("gifpage", pagename=pagename))
    else:
        return render_template("edit_gif.html",
                               form=form,
                               item=imgdoc,
                               sign_in_username=session["username"])
Пример #6
0
def edit_userboard(username, board):
    boarddoc = models.Board().fetch_boarddoc(username, board)
    form = EditBoardform(request.form)
    if boarddoc['username'] != session['username']:
        return abort(404)  # ボードの管理ページに非管理者ユーザーが接続してきた場合404へ

    if request.method == "GET":
        return render_template("edit_userboard.html",
                               form=form,
                               boarddoc=boarddoc,
                               sign_in_username=session["username"],
                               placeho_board=boarddoc['board'],
                               placeho_description=boarddoc['description'])

    if request.method == "POST" and form.validate():
        (existing_boards,
         existing_ids) = models.User().fetch_boards(username, edit="edit")

        for (existing_board,
             existing_id) in zip(existing_boards,
                                 existing_ids):  # 既に存在しているボード名の場合の条件分岐
            if existing_board == form.board.data and existing_id != str(
                    boarddoc["_id"]):
                return render_template(
                    "edit_userboard.html",
                    form=form,
                    boarddoc=boarddoc,
                    placeho_board=boarddoc['board'],
                    sign_in_username=session["username"],
                    placeho_description=boarddoc['description'],
                    edit_board_error=gettext(u"This boardname is created"))

        models.Board().change_board(
            username=username,
            oldboard=board,
            newboard=form.board.data,
            description=form.description.data)  # Board Collectionの該当docをUpdate
        models.Img().change_board(
            username=username, oldboard=board,
            newboard=form.board.data)  # Img Collectionの該当docsをUpdate
        return redirect(
            url_for("userboard", username=username, board=form.board.data))
    else:
        return render_template("edit_userboard.html",
                               form=form,
                               boarddoc=boarddoc,
                               sign_in_username=session["username"],
                               placeho_board=boarddoc['board'],
                               placeho_description=boarddoc['description'])
Пример #7
0
def tag(tag, page):
    imgdocs = models.Img().fetch_limitedimgdocs(**{
        "per_page": PER_PAGE,
        "page": int(page),
        "tag": tag
    })
    if "username" not in session:
        return render_template("anonymous/tag.html",
                               items=imgdocs,
                               next=tag,
                               form1=Sign_upform(request.form),
                               form2=Sign_inform(request.form))

    return render_template("tag.html",
                           items=imgdocs,
                           next=tag,
                           sign_in_username=session['username'])
Пример #8
0
def index(page):
    form2 = Sign_inform(request.form)
    imgdocs = models.Img().fetch_limitedimgdocs(**{
        "per_page": PER_PAGE,
        "page": int(page)
    })
    if "username" not in session:
        if int(page) >= 6: return abort(404)
        return render_template("anonymous/index.html",
                               items=imgdocs,
                               form1=Sign_upform(request.form),
                               form2=Sign_inform(
                                   request.form))  # 非登録ユーザーには別テンプレートでレンダリング

    return render_template("index.html",
                           items=imgdocs,
                           sign_in_username=session["username"])
Пример #9
0
def userboard(username, board, page):
    imgdocs = models.Img().fetch_imgdocs(username, board, PER_PAGE, int(page))
    if int(page) >= 2 and len(imgdocs) == 0: return abort(404)

    description = models.Board().fetch_description(username, board)

    if "username" not in session:
        return render_template("anonymous/userboard.html",
                               items=imgdocs,
                               username=username,
                               board=board,
                               description=description,
                               form1=Sign_upform(request.form),
                               form2=Sign_inform(request.form))
    return render_template("userboard.html",
                           items=imgdocs,
                           sign_in_username=session['username'],
                           username=username,
                           board=board,
                           description=description)
Пример #10
0
def upload_gif():
    boards_li = models.User().fetch_boards(session["username"])
    form = UploadGifform(request.form)
    form.board.choices = [
        (index, board)
        for (index, board) in zip(range(1,
                                        len(boards_li) + 1), boards_li)
    ]  # 動的にoption要素の値を出力するための下処理

    if request.method == 'GET':
        return render_template("uploadgif.html",
                               form=form,
                               sign_in_username=session['username'])

    if request.method == 'POST' and form.validate():
        texts = request.form["hidden"]
        index = request.form["board"]
        board = boards_li[int(index) - 1]

        dict = json.loads(texts)
        Postedidlist = []
        (li, imgli) = (dict.keys(), [])
        for (k, i) in zip(li, range(len(li))):
            if k.startswith("description") and len(k) == 37:
                descriptionid = k.lstrip("description[").rstrip("]")
                for (comparedelemen, comparedi) in zip(li, range(len(li))):
                    if comparedelemen.startswith("tags") and len(
                            comparedelemen) == 30:
                        tagsid = comparedelemen.lstrip("tags[").rstrip("]")
                        if descriptionid == tagsid:
                            Postedidlist.append(descriptionid)

        readyidlist = models.Readyimg().fetch_idlist(session["username"])

        list = []
        for element in Postedidlist:
            index = readyidlist.index(element)
            tuple = (index, element)
            list.append(tuple)
            list.sort()

        try:  # このtry, except文は実質的なvalidationにあたる
            for sortedid in list:
                if not 0 <= len(
                        dict["description[" + sortedid[1] + "]"]) <= 200:
                    return render_template(
                        "uploadgif.html",
                        form=form,
                        sign_in_username=session['username'])
                if not 1 <= len(
                        dict["tags[" + sortedid[1] + "]"].split()) <= 5:
                    return render_template(
                        "uploadgif.html",
                        form=form,
                        sign_in_username=session['username'])
                for tag in dict["tags[" + sortedid[1] + "]"].split():
                    if not 1 <= len(tag) <= 20: raise END
                    if not re.compile(
                            u"^([a-zA-Z0-9_-]|[ぁ-ん0-9_ー]|[ァ-ヴ]|[一-龠])+$"
                    ).search(tag):
                        raise END
        except END:  # 不正なタグが入力された時error分岐
            return render_template("uploadgif.html",
                                   form=form,
                                   sign_in_username=session['username'])

        for sortedid in list:
            readyimgdoc = models.Readyimg().fetch_readyimgdoc(sortedid[1])
            models.Img().add(username=readyimgdoc["username"],
                             filename=readyimgdoc["filename"],
                             description=dict["description[" + sortedid[1] +
                                              "]"],
                             tags=dict["tags[" + sortedid[1] + "]"],
                             board=board)

        return redirect(
            url_for("userboard", username=session["username"], board=board))
    else:
        return render_template("uploadgif.html",
                               form=form,
                               sign_in_username=session['username'])
Пример #11
0
def delete_gifpage(pagename):
    username = models.Img().fetch_imgdoc(pagename)["username"]
    if username != session["username"]: return abort(404)
    models.Img().delete(pagename)  # Img Collectionの該当のGifdocを削除します
    # Gifのファイル自体は削除しない方向です
    return redirect(url_for("userboards", username=session["username"]))