Exemplo n.º 1
0
 def is_accessible(self):
     """
     admin access handle
     :return:
     """
     _access = False
     if current_user.is_authenticated:
         # TODO 检测是否还存在获取不到name(调用__repr__只是为了解决__dict__获取不到的问题), 后续需解决
         current_user.__repr__()
         user = get_username(current_user.__dict__["name"])
         if user is not None:
             if user.is_admin:
                 _access = True
     return _access
Exemplo n.º 2
0
def result():
    if current_user.is_authenticated is None:
        return redirect('/login')
    strategy = ''
    result = ''
    rank = ''
    current = ''
    if request.method == 'POST':
        form = request.form
        strategy = [int(form.get("area" + str(i))) for i in range(1, 11)]
        if sum(strategy) != 100:
            strategy = ''
        else:
            current = str(datetime.datetime.now())
            result, rank = compete(strategy)

            # One entry represent one record of practice.
            entry = current + seprator + ','.join(
                [str(i) for i in strategy]) + seprator + ','.join(
                    [str(i) for i in result[0]]) + seprator + str(rank)

            # write the entry into the practice file.
            with open(
                    './app/data/practiceData/' + current_user.__repr__() +
                    '.txt', 'a') as f:
                f.write(entry + '\n')
    return render_template('result.html',
                           strategy=strategy,
                           result=result,
                           rank=rank,
                           time=current)
Exemplo n.º 3
0
def practiceHistory():
    if current_user.is_authenticated is None:
        return redirect('/login')
    try:
        with open(
                './app/data/practiceData/' + current_user.__repr__() + '.txt',
                'r') as f:
            history = f.readlines()
            history = [entry[:-1].split(seprator) for entry in history]
    except FileNotFoundError:
        history = ''
    return render_template('practiceHistory.html', history=history)
Exemplo n.º 4
0
def signup():
    form = account_forms.SignupForm()

    if request.method == "POST":
        # proper try/except here.
        models.User.create_new_user(form.email.data, form.password.data)

        return redirect(url_for("reader_bp.reader_home"))

    return render_template("account/signup.html",
                           form=form,
                           current_user=current_user.__repr__())
Exemplo n.º 5
0
def competeHistory():
    if current_user.is_authenticated is None:
        return redirect('/login')
    try:
        with open(
                './app/data/competitionData/' + current_user.__repr__() +
                '.txt', 'r') as f:
            lines = f.readlines()
            lines = [line for line in lines if not line.endswith(seprator)]
            lines = [line for line in lines if not line.endswith('ignore\n')]
            history = [entry[:-1].split(seprator) for entry in lines]
            print(history)
    except FileNotFoundError:
        history = ''
    except IndexError:
        print('index error in competition data!')
        history = ''
    return render_template('competeHistory.html', history=history)
Exemplo n.º 6
0
def real():
    if current_user.is_authenticated is None:
        return redirect('/login')
    if request.method == 'GET':
        hour = datetime.datetime.now().hour
        minite = datetime.datetime.now().minute
        second = datetime.datetime.now().second
        if hour < START or hour > END:  # Should be changed to 19 and 20
            return redirect('/war')
        else:
            return render_template('compete.html',
                                   round=minite // 10 + 1,
                                   timeLeft=60 * (10 - minite % 10) - second,
                                   whoami=current_user)
    else:  # POST
        hour = datetime.datetime.now().hour
        minute = datetime.datetime.now().minute
        # Block invalid POSTS.
        if hour < START or hour > END:
            return redirect('/war')
        form = request.form
        strategy = [int(form.get("area" + str(i))) for i in range(1, 11)]
        if sum(strategy) != 100:
            strategy = ''
        else:
            current = str(datetime.datetime.now())
            round = minute // 10 + 1
        filename = './app/data/competitionData/' + current_user.__repr__(
        ) + '.txt'
        with open(filename, 'a') as f:
            # If read from f, the contents will be ''
            # because f points to the end of file.
            temp = open(filename, 'r')
            if temp.read().endswith(seprator):
                f.write('ignore\n')
            else:
                f.write(
                    str(round) + seprator +
                    ','.join([str(i) for i in strategy]) + seprator + current +
                    seprator)
            temp.close()
        return redirect('/war')
Exemplo n.º 7
0
def login():
    # immediately bypass this logic if the user is authenticated
    if current_user.is_authenticated:
        return redirect(url_for("creator_bp.home"))

    form = account_forms.LoginForm()

    if form.validate_on_submit():
        print('validated form')
        user = models.User.get_user_by_email(form.email.data)

        if user and user.verify_password(user.id, form.password.data) == True:
            login_user(user, remember=False)

            next_url = request.args.get('next')
            if next_url and is_safe_url(next_url):
                return redirect(next_url)
            else:
                return redirect(url_for('creator_bp.home'))

            # if "next" in session:
            #     next_url = session["next"]
            #     session.pop("next")
            #     session.modified = True
            #     print("next in session")
            #     print(session)
            #     if is_safe_url(next_url):
            #         print("url is safe")
            #         print(next_url)
            #         return redirect(next_url)
            #     else:
            #         print("url is not safe")
            #         return redirect(url_for("creator_bp.home"))
            # else:
            #     print("next is not in session")
            #     return redirect(url_for("creator_bp.home"))
        else:
            flash("Please enter a valid user name and password.")

    return render_template("account/login.html",
                           form=form,
                           current_user=current_user.__repr__())
Exemplo n.º 8
0
def manage_account():
    user_data = {
        "about": current_user.about,
        "website": current_user.personal_website,
        "name": current_user.name
    }
    form = account_forms.UserManagementForm(data=user_data)

    if request.method == "POST":
        updated_user = {
            "id": current_user.id,
            "about": form.about.data,
            "name": form.name.data,
            "personal_website": form.website.data
        }

        models.User.update_user(updated_user)

        return redirect(url_for("creator_bp.home"))

    return render_template("account/manage.html",
                           form=form,
                           current_user=current_user.__repr__())
Exemplo n.º 9
0
def index():
    user = current_user
    if len(current_user.__repr__()) > 30:
        user = '******'
    return render_template("index.html", whoami=user, start=START, end=END)