Exemplo n.º 1
0
def new_picture(picid):
    pic = Rawpicture.objects(id=picid).first()
    piclinkb64 = base64encode(pic.piclink)
    token = ''
    if request.method == 'GET':
        if 'token' in session:
            token = session['token']
        if 'picid' in session:
            if ("'" + picid + "'") in session['picid']:
                unsavelink = session['picid'].replace("'" + picid + "'", '')
                del session['picid']
                return render_template('new_picture.html',
                                       piclinkb64=unsavelink,
                                       resetlinkb64=piclinkb64,
                                       token=token)
            else:
                return render_template('new_picture.html',
                                       piclinkb64=piclinkb64,
                                       resetlinkb64=piclinkb64,
                                       token=token)
        else:
            return render_template('new_picture.html',
                                   piclinkb64=piclinkb64,
                                   resetlinkb64=piclinkb64,
                                   token=token)
    elif request.method == 'POST':
        form = request.form
        # nếu người dùng chưa đăng nhập:
        if 'token' not in session:
            unsavelink = form['unsavelink']
            session[
                'picid'] = "'" + picid + "'" + unsavelink  # thêm dấu nháy bọc quanh picid để phòng trường hợp trong chuỗi base64 có 1 đoạn ký tự trùng với picid
            return redirect(url_for('login'))
        # nếu người dùng đã đăng nhập:
        if 'token' in session:
            token = session['token']
            picname = form['picname']
            piclink = form['piclink']
            picstatus = form['picstatus']
            picartist = token
            picartistfullname = User.objects(username=token).first().fullname
            newlink = Savepicture(piclink=piclink,
                                  picname=picname.strip(),
                                  picstatus=picstatus,
                                  picartist=picartist,
                                  picartistfullname=picartistfullname,
                                  picrawid=picid)
            newlink.save()
            newid = Savepicture.objects(piclink=piclink).first().id
            # Update database của user tương ứng:
            working_arts = User.objects(username=token).first().working_arts
            finished_arts = User.objects(username=token).first().finished_arts
            if picstatus == 'working':
                User.objects(username=token).first().update(
                    set__working_arts=working_arts + 1)
            elif picstatus == 'finished':
                User.objects(username=token).first().update(
                    set__finished_arts=finished_arts + 1)
            return redirect(url_for('saved', picid=newid))
Exemplo n.º 2
0
def new_picture(picid):
    pic = Rawpicture.objects(id=picid).first()
    piclinkb64 = base64encode(pic.piclink)
    token = ''
    aftersave = ''
    if 'token' in session:
        token = session['token']
    if request.method == 'GET':
        aftersave = 'no'
        return render_template('new_picture.html',
                               piclinkb64=piclinkb64,
                               token=token,
                               aftersave=aftersave)
    elif request.method == 'POST':
        aftersave = 'yes'
        form = request.form
        picname = form['picname']
        piclink = form['piclink']
        picstatus = form['picstatus']
        picartist = token
        picartistfullname = User.objects(username=token).first().fullname
        newlink = Savepicture(piclink=piclink,
                              picname=picname,
                              picstatus=picstatus,
                              picartist=picartist,
                              picartistfullname=picartistfullname)
        newlink.save()
        # Update database của user tương ứng:
        working_arts = User.objects(username=token).first().working_arts
        finished_arts = User.objects(username=token).first().finished_arts
        if picstatus == 'working':
            User.objects(username=token).first().update(
                set__working_arts=working_arts + 1)
        elif picstatus == 'finished':
            User.objects(username=token).first().update(
                set__finished_arts=finished_arts + 1)
        return render_template('new_picture.html',
                               piclinkb64=piclink,
                               aftersave=aftersave)