def init_database(): # Create the database and the database table db.create_all() # Insert user data user1 = User(username="******", email="*****@*****.**") user2 = User(username="******", email="*****@*****.**") user1.set_password("password") user2.set_password("password2") db.session.add(user1) db.session.add(user2) db.session.flush() habit1 = Habit(habit="Test Habit 1", user_id=user1.id, weekly_goal=1) habit2 = Habit(habit="Test Habit 2", user_id=user2.id, weekly_goal=2) db.session.add(habit1) db.session.add(habit2) db.session.flush() habit1_history = HabitHistory(habit_id=habit1.id, timestamp=datetime(2019, 1, 1)) habit2_history = HabitHistory(habit_id=habit2.id, timestamp=datetime(2019, 1, 3)) db.session.add(habit1_history) db.session.add(habit2_history) db.session.flush() # Commit the changes for the users db.session.commit() yield db # this is where the testing happens! db.drop_all()
def new_habit(): db.session.modified = True form = New_habit() category_list = Category.list_of_category() form.submit() if form.validate_on_submit(): new_record = Habit(name=form.name.data, category_id=form.category_type.data, date_start=form.start_date.data, priority=form.priority.data, date_end=form.end_date.data) Habit.save_to_db(new_record) new_habit_id = Habit.query.filter_by(name=form.name.data).first().id for single_date in daterange(form.start_date.data, form.end_date.data): dates = Date(date=single_date, habit_id=new_habit_id, category_id=form.category_type.data) Date.save_to_db(dates) flash('Your post has been created!', 'success') return redirect(url_for('home')) return render_template('habit.html', title='New Category', form=form, legend='New Category', category_list=category_list)
def complete(): id = request.form["id"] timestamp = request.form["timestamp"] timestamp = datetime.strptime(timestamp, "%Y-%m-%d") weekly_count = int(request.form["weekly_count"]) habit = Habit.query.filter_by(id=id).first_or_404() habit_history = HabitHistory.query.filter_by(habit_id=id) Habit.complete_habit(habit, current_user, timestamp) weekly_count = habit.increase_streak(weekly_count) db.session.commit() return jsonify( current_streak=habit.current_streak, longest_streak=habit.longest_streak, weekly_count=weekly_count, )
def create_habit(pid): form = HabitForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate(): newHabit = Habit( habit=form.data['habit'], description=form.data['description'], frequency=str(form.data['frequency']), color_id=form.data['color'], stamp_id=form.data['stamp'], creator_id=request.json['userId'], program_id=pid, ) db.session.add(newHabit) print(newHabit) db.session.commit() newHabit = Habit.query.options(joinedload(Habit.stamp), joinedload(Habit.color)).get( newHabit.id) habit = habit_schema.dump(newHabit) habit["color"] = color_schema.dump(newHabit.color) habit["stamp"] = stamp_schema.dump(newHabit.stamp) print("\n\nNEW HABIT DICTIONARY:", habit) return jsonify(habit) return "Habit failure"
def create(): form = CreateForm() if form.validate_on_submit(): habit = Habit(habit=form.habit.data, start_date=form.start_date.data, end_date=form.end_date.data, user_id=current_user.id) db.session.add(habit) db.session.commit() flash('New habit created') return redirect(url_for('main.index')) return render_template('create.html', title='Create Habit', form=form)
def add_habit(): form = HabitForm() if form.validate_on_submit(): habit = Habit(description=form.description.data, complete=form.complete.data, parent_id=current_user.id) db.session.add(habit) db.session.commit() return redirect('/habits') return render_template('main/add_habit.html', form=form)
def create_habit(username): form = HabitCreation() form.systemID.choices = search_for_systems() # search for available systems if form.validate_on_submit(): habit = Habit(title=form.title.data, goal=form.goal.data, system_id=form.systemID.data) db.session.add(habit) db.session.commit() flash('Congratulations, you have started a new habit!') return redirect(url_for('dashboard', username=current_user.username)) return render_template('create_habit.html', title='New Habit', form=form)
def new_habit(): user = current_user form = NewHabitForm() if form.validate_on_submit(): habit = Habit( habit=form.habit.data, creator=current_user, weekly_goal=form.weekly_goal.data, ) db.session.add(habit) db.session.commit() flash("Your habit is saved.") return redirect(url_for("main.index")) return render_template("new_habit.html", title="New Habit", user=user, form=form)
def myhabit(name): allcategory = Category.find_by_category(name) myhabits = Habit.find_by_category_id(allcategory.id) return render_template('myhabit.html', myhabits=myhabits, category=name)