예제 #1
0
def dashboard():
    # initialize the current user
    user = User.find_by_username(current_user.username)

    # initialize forms
    todo_form = ToDoForm()
    keepdo_form = KeepDoForm()

    # query url '?todopage=' and '?keepdopage='
    todo_page = request.args.get('todopage', 1, type=int)
    keepdo_page = request.args.get('keepdopage', 1, type=int)

    # initialize pagination objects and build necessary arguments
    todolist = user.todolist.order_by(ToDoList.status.desc(),
                                      ToDoList.timestamp.desc()).paginate(
                                          todo_page, 8, False)
    keepdolist = user.keepdolist.order_by(
        KeepDoList.created_timestamp.desc()).paginate(keepdo_page, 8, False)

    todo_args = pagination_builder(todolist,
                                   'todopage',
                                   form=todo_form,
                                   page=todo_page,
                                   todolist=todolist.items)
    keepdo_args = pagination_builder(keepdolist,
                                     'keepdopage',
                                     form=keepdo_form,
                                     page=keepdo_page,
                                     keepdolist=keepdolist.items)

    # IMPORTANT!
    # not based on user's timezone, improve in the future
    # modify keepdolist database everytime page is loaded, may slow the performance
    for keepdo in keepdolist.items:
        days_interval = (datetime.utcnow() - keepdo.last_check_point).days
        # if not checked for over 1 day
        # change daily check status to False and commit to database
        if days_interval > 0:
            keepdo.daily_check_status = False
            db.session.commit()

    # initialize session keys for the first time
    # will not run those lines afterwards
    # those session keys will be used in generating templates
    if not 'todolist_collapse' in session:
        session['todolist_collapse'] = 'in'  # default expand todolist panel
        # default expand keepdolist panel
        session['keepdolist_collapse'] = 'in'

    return render_template('dashboard.html',
                           todo_args=todo_args,
                           keepdo_args=keepdo_args)
예제 #2
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        username = form.username.data
        if User.find_by_username(username):
            flash("The username has been taken.",
                  "alert alert-danger")
            return redirect(url_for('bp_user.register'))
        else:
            user = User(username=username)
            user.set_password(form.password.data)
            db.session.add(user)
            db.session.commit()
            flash("Welcome {}, you're now ready to go.".format(username),
                  "alert alert-success")
            # add auto login after register
            return redirect(url_for('bp_user.login'))
    return render_template('register.html', form=form)
예제 #3
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('bp_dashboard.dashboard'))

    form = LoginForm()
    if form.validate_on_submit():
        user = User.find_by_username(username=form.username.data)

        if user is None:
            flash('User does not exist.', 'alert alert-danger')
            return redirect(url_for('bp_user.login'))

        # may raise problem if database does not handle binary well
        if user.verify_password(form.password.data):
            login_user(user)
            flash('Welcome, {}!'.format(user.username), 'alert alert-success')
            # safe url check?
            return redirect(url_for('bp_dashboard.dashboard'))
        else:
            flash('Wrong password, please try agin.',
                  'alert alert-danger')
            return redirect(url_for('bp_user.login'))

    return render_template('login.html', form=form)