def task_break_view(task_id, task_date): form = TaskBreakForm(request.form) try: task = Task.query.filter( Task.id == task_id, Task.owner_id == current_user.id, ).one() except NoResultFound: # This is not an existing task this user owns flash( 'Could not find task {id}.'.format(id=task_id), 'danger', ) return redirect(url_for('home')) date = parse.unquote_plus(task_date) if request.method == 'POST' and form.validate(): return try_to_take_task_break( task_id=task_id, form=form, date=date, ) else: return render_turkey( "task_break.html", form=form, task=task, task_date=date, )
def site_admin_view(): if not current_user.is_admin: abort(403) registration_enabled = registrations_allowed() form = SiteAdminForm(request.form) if request.method == 'POST' and form.validate(): SiteAdmin.user_registration( allowed=form.registration_enabled.data, ) if form.registration_enabled.data: message = "User registrations allowed!" else: message = "User registrations prohibited!" flash( message, 'success', ) return redirect(url_for('home')) else: form.registration_enabled.default = registration_enabled return render_turkey("site_admin.html", form=form)
def create_task_view(): goals = get_goals(owner=current_user.id, include_top_level=False) if len(goals["basic"]) == 0: flash("No goals exist. Create a goal before creating tasks!", "danger") return redirect(url_for("create_goal")) form = CreateTaskForm(request.form) form.associated_goal.choices = goals["display"] if request.method == "POST" and form.validate(): associated_goal_name = None for goal in goals["basic"]: if form.associated_goal.data == goal[0]: associated_goal_name = goal[1] break if associated_goal_name is None: # Should only happen if a page was left open long enough for # the location to be deleted in a separate session flash("Associated goal not found, cannot create task.", "danger") return redirect(url_for("create_task")) new_task = Task.create( name=form.task_name.data, associated_goal_id=form.associated_goal.data, owner_id=current_user.id ) if new_task is None: flash("Task %s already exists under %s!" % (form.task_name.data, associated_goal_name), "danger") return redirect(url_for("create_task")) else: flash("Task %s created under %s!" % (form.task_name.data, associated_goal_name), "success") return redirect(url_for("home")) else: return render_turkey("task.html", form=form)
def login_view(): form = LoginForm(request.form) if request.method == 'POST' and form.validate(): try: user = User.query.filter( or_(User.name == form.username.data, User.email == form.username.data)).one() except NoResultFound: # The user does not exist user = None if user and user.validate_password(form.password.data): # The user exists and the password is valid login_user(user) flash('Welcome back, %s!' % user.name, 'success') # TODO: We need to make sure that 'next' points to something on our # site to avoid malicious redirects return redirect( secure_redirect( request.args.get('next'), request.args.get('hmac'), url_for('home'), )) else: flash('Login failed!', 'danger') return redirect(url_for('login')) else: return render_turkey("login.html", form=form)
def task_history_view(task_id): try: task = Task.query.filter(Task.id == task_id, Task.owner_id == current_user.id).one() except NoResultFound: # This is not an existing task this user owns flash("Could not find task {id}.".format(id=task_id), "danger") return redirect(url_for("home")) current_day = datetime.date.today() midnight = datetime.datetime.min.time() current_day = datetime.datetime.combine(current_day, midnight) creation_day = datetime.datetime.combine(task.creation_time, midnight) creation_history_delta = (current_day - creation_day).days task_history = get_tasks_history(task_id=task_id, days=creation_history_delta, end=task.finish_time) task_name = task.name finish_time = task.finish_time for task in task_history: task["date_quoted"] = parse.quote_plus(task["date"]) days_ago = [] while current_day >= creation_day: days_ago.append(current_day) current_day = current_day - datetime.timedelta(days=1) return render_turkey("task_history.html", history=task_history, task_name=task_name, end=finish_time)
def archived_tasks_view(): if current_user.is_anonymous: goals = [] tasks = [] completed = [] else: current_time = datetime.datetime.now() goals = Goal.query.filter(Goal.owner_id == current_user.id, ).all() tasks = Task.query.filter(Task.owner_id == current_user.id, ).all() tasks = [ task for task in tasks if task.finish_time is not None and task.finish_time < current_time ] completed = CompletedTask.get_completed( date=datetime.datetime.today(), ) tree = {'goals': {}} top_level_goals = [(goal.id, goal.name) for goal in goals if goal.parent_goal_id is None] for goal in top_level_goals: tree['goals'][goal] = make_goal_branch( goal, goals, tasks, completed, ) return render_turkey("archived_tasks.html", tree=tree, current_page='archived_tasks')
def complete_old_task_view(task_id, task_date): form = CompleteOldTaskForm(request.form) try: task = Task.query.filter(Task.id == task_id, Task.owner_id == current_user.id).one() except NoResultFound: # This is not an existing task this user owns flash("Could not find task {id}.".format(id=task_id), "danger") return redirect(url_for("home")) date = parse.unquote_plus(task_date) creation_day = datetime.datetime.combine(task.creation_time, datetime.datetime.min.time()) completion_day = datetime.datetime.strptime(date, "%Y %b %d") if completion_day < creation_day: flash( "Could not complete an old copy of {task_name} " "from before its creation date {creation}".format(task_name=task.name, creation=creation_day), "danger", ) return redirect(url_for("task_history", task_id=task_id)) if request.method == "POST" and form.validate(): return try_to_complete_task(task_id=task_id, form=form, date=date) else: return render_turkey("complete_old_task.html", form=form, task=task, task_date=date)
def my_account_view(): try: User.query.filter(User.name == current_user.name).one() return render_turkey("me.html", user=current_user) except NoResultFound: # The user does not exist, this should not happen flash("Error accessing your account page. Please try logging in again.", "danger") return redirect(url_for("home"))
def my_account_view(): try: User.query.filter(User.name == current_user.name).one() return render_turkey("me.html", user=current_user) except NoResultFound: # The user does not exist, this should not happen flash( 'Error accessing your account page. Please try logging in again.', 'danger') return redirect(url_for('home'))
def create_task_view(): goals = get_goals(owner=current_user.id, include_top_level=False) if len(goals['basic']) == 0: flash( 'No goals exist. Create a goal before creating tasks!', 'danger', ) return redirect(url_for('create_goal')) form = CreateTaskForm(request.form) form.associated_goal.choices = goals['display'] if request.method == 'POST' and form.validate(): associated_goal_name = None for goal in goals['basic']: if form.associated_goal.data == goal[0]: associated_goal_name = goal[1] break if associated_goal_name is None: # Should only happen if a page was left open long enough for # the location to be deleted in a separate session flash( 'Associated goal not found, cannot create task.', 'danger', ) return redirect(url_for('create_task')) new_task = Task.create( name=form.task_name.data, associated_goal_id=form.associated_goal.data, owner_id=current_user.id, ) if new_task is None: flash( 'Task %s already exists under %s!' % ( form.task_name.data, associated_goal_name, ), 'danger', ) return redirect(url_for('create_task')) else: flash( 'Task %s created under %s!' % ( form.task_name.data, associated_goal_name, ), 'success', ) return redirect(url_for('home')) else: return render_turkey("task.html", form=form)
def complete_task_view(task_id): form = CompleteTaskForm(request.form) try: task = Task.query.filter(Task.id == task_id, Task.owner_id == current_user.id).one() except NoResultFound: # This is not an existing task this user owns flash("Could not find task {id}.".format(id=task_id), "danger") return redirect(url_for("home")) if request.method == "POST" and form.validate(): return try_to_complete_task(task_id=task_id, form=form) else: return render_turkey("complete_task.html", form=form, task=task)
def create_goal_view(): goals = get_goals(owner=current_user.id) form = GoalForm(request.form) form.parent_goal.choices = goals['display'] if request.method == 'POST' and form.validate(): parent_goal_name = None for goal in goals['basic']: if form.parent_goal.data == goal[0]: parent_goal_name = goal[1] break if parent_goal_name is None: # Should only happen if a page was left open long enough for # the location to be deleted in a separate session flash( 'Parent goal not found, cannot create goal.', 'danger', ) return redirect(url_for('create_goal')) new_goal = Goal.create( name=form.goal_name.data, parent_goal_id=form.parent_goal.data, owner_id=current_user.id, ) if new_goal is None: flash( 'Goal %s already exists under %s!' % ( form.goal_name.data, parent_goal_name, ), 'danger', ) return redirect(url_for('create_goal')) else: flash( 'Goal %s created under %s!' % ( form.goal_name.data, parent_goal_name, ), 'success', ) return redirect(url_for('home')) else: return render_turkey("goal.html", form=form)
def complete_old_task_view(task_id, task_date): form = CompleteOldTaskForm(request.form) try: task = Task.query.filter( Task.id == task_id, Task.owner_id == current_user.id, ).one() except NoResultFound: # This is not an existing task this user owns flash( 'Could not find task {id}.'.format(id=task_id), 'danger', ) return redirect(url_for('home')) date = parse.unquote_plus(task_date) creation_day = datetime.datetime.combine(task.creation_time, datetime.datetime.min.time()) completion_day = datetime.datetime.strptime( date, '%Y %b %d', ) if completion_day < creation_day: flash( 'Could not complete an old copy of {task_name} ' 'from before its creation date {creation}'.format( task_name=task.name, creation=creation_day, ), 'danger', ) return redirect(url_for('task_history', task_id=task_id)) if request.method == 'POST' and form.validate(): return try_to_complete_task( task_id=task_id, form=form, date=date, ) else: return render_turkey( "complete_old_task.html", form=form, task=task, task_date=date, )
def task_history_view(task_id): try: task = Task.query.filter( Task.id == task_id, Task.owner_id == current_user.id, ).one() except NoResultFound: # This is not an existing task this user owns flash( 'Could not find task {id}.'.format(id=task_id), 'danger', ) return redirect(url_for('home')) current_day = datetime.date.today() midnight = datetime.datetime.min.time() current_day = datetime.datetime.combine(current_day, midnight) creation_day = datetime.datetime.combine(task.creation_time, midnight) creation_history_delta = (current_day - creation_day).days task_history = get_tasks_history( task_id=task_id, days=creation_history_delta, end=task.finish_time, ) task_name = task.name finish_time = task.finish_time for task in task_history: task['date_quoted'] = parse.quote_plus(task['date']) days_ago = [] while current_day >= creation_day: days_ago.append(current_day) current_day = current_day - datetime.timedelta(days=1) return render_turkey( 'task_history.html', history=task_history, task_name=task_name, end=finish_time, )
def home_view(): if current_user.is_anonymous: goals = [] tasks = [] completed = [] breaks = [] else: current_time = datetime.datetime.now() goals = Goal.query.filter( Goal.owner_id == current_user.id, ).all() tasks = Task.query.filter( Task.owner_id == current_user.id, ).all() tasks = [ task for task in tasks if task.finish_time is None or task.finish_time >= current_time ] completed = CompletedTask.get_completed( date=datetime.datetime.today(), ) breaks = TaskBreak.get_breaks( date=datetime.datetime.today(), ) tree = {'goals': {}} top_level_goals = [ (goal.id, goal.name) for goal in goals if goal.parent_goal_id is None ] for goal in top_level_goals: tree['goals'][goal] = make_goal_branch( goal, goals, tasks, completed, breaks, ) return render_turkey("home.html", tree=tree, current_page='active_tasks')
def register_view(): if current_user and not current_user.is_anonymous: flash( 'You already have an account!', 'warning', ) return redirect(url_for('home')) if not registrations_allowed(): abort(403) form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): new_user = User.create( name=form.username.data, email=form.email.data, password=form.password.data, ) if new_user is None: # TODO: Put the error on the form validation instead # TODO: Make this still give a message afterwards, but link it to # the forgotten email password reset thing? flash('That username or email is in use.', 'danger') return redirect(url_for('register')) else: login_user(new_user) flash('Welcome to the Turkey, %s!' % form.username.data, 'success') validation_message = ' '.join([ 'Your email account %s will receive a validation mail.', 'Please click the link in that mail to validate your mail.', ]) % form.email.data flash(validation_message, 'info') # If this is the first ever user, they're an admin all_users = User.query.all() if len(all_users) == 1: new_user.promote_admin() # TODO: Redirect to /me (current user's account page)? return redirect(url_for('home')) else: return render_turkey("register.html", form=form)
def login_view(): form = LoginForm(request.form) if request.method == "POST" and form.validate(): try: user = User.query.filter(or_(User.name == form.username.data, User.email == form.username.data)).one() except NoResultFound: # The user does not exist user = None if user and user.validate_password(form.password.data): # The user exists and the password is valid login_user(user) flash("Welcome back, %s!" % user.name, "success") # TODO: We need to make sure that 'next' points to something on our # site to avoid malicious redirects return redirect(secure_redirect(request.args.get("next"), request.args.get("hmac"), url_for("home"))) else: flash("Login failed!", "danger") return redirect(url_for("login")) else: return render_turkey("login.html", form=form)
def register_view(): if current_user and not current_user.is_anonymous: flash("You already have an account!", "warning") return redirect(url_for("home")) if not registrations_allowed(): abort(403) form = RegisterForm(request.form) if request.method == "POST" and form.validate(): new_user = User.create(name=form.username.data, email=form.email.data, password=form.password.data) if new_user is None: # TODO: Put the error on the form validation instead # TODO: Make this still give a message afterwards, but link it to # the forgotten email password reset thing? flash("That username or email is in use.", "danger") return redirect(url_for("register")) else: login_user(new_user) flash("Welcome to the Turkey, %s!" % form.username.data, "success") validation_message = ( " ".join( [ "Your email account %s will receive a validation mail.", "Please click the link in that mail to validate your mail.", ] ) % form.email.data ) flash(validation_message, "info") # If this is the first ever user, they're an admin all_users = User.query.all() if len(all_users) == 1: new_user.promote_admin() # TODO: Redirect to /me (current user's account page)? return redirect(url_for("home")) else: return render_turkey("register.html", form=form)
def confirm_archive_task_view(task_id): form = ConfirmArchiveTaskForm(request.form) try: task = Task.query.filter( Task.id == task_id, Task.owner_id == current_user.id, ).one() except NoResultFound: # This is not an existing task this user owns flash( 'Could not find task {id}.'.format(id=task_id), 'danger', ) abort(404) if request.method == 'POST' and form.validate(): task.finish() return redirect(url_for('home')) else: return render_turkey("confirm_archive_task.html", form=form, task=task)
def confirm_archive_task_view(task_id): form = ConfirmArchiveTaskForm(request.form) try: task = Task.query.filter( Task.id == task_id, Task.owner_id == current_user.id, ).one() except NoResultFound: # This is not an existing task this user owns flash( 'Could not find task {id}.'.format(id=task_id), 'danger', ) return redirect(url_for('home')) if request.method == 'POST' and form.validate(): task.finish() return redirect(url_for('home')) else: return render_turkey("confirm_archive_task.html", form=form, task=task)
def complete_task_view(task_id): form = CompleteTaskForm(request.form) try: task = Task.query.filter( Task.id == task_id, Task.owner_id == current_user.id, ).one() except NoResultFound: # This is not an existing task this user owns flash( 'Could not find task {id}.'.format(id=task_id), 'danger', ) abort(404) if request.method == 'POST' and form.validate(): return try_to_complete_task( task_id=task_id, form=form, ) else: return render_turkey("complete_task.html", form=form, task=task)
def version_history_view(): return render_turkey("version_history.html", version_history=version_history)
def not_found_view(error): return render_turkey('errors/404.html'), 404
def not_allowed_view(error): return render_turkey('errors/403.html'), 403