def add_workout(): if request.method == 'POST': user = User.query.filter_by(name=session['username']).first() workout = Workout(date=datetime.utcnow(), user_id=user.id) exercise_count = int(request.form['exercise_count']) for exercise_num in range(1, exercise_count + 1): exercise = Exercise(order=1, exercise_id=request.form['exercise' + str(exercise_num)], workout=workout) weights = request.form.getlist('weight' + str(exercise_num)) reps = request.form.getlist('reps' + str(exercise_num)) set_order = 1 for weight, rep in zip(weights, reps): work_set = Set(order=set_order, exercise=exercise, weight=weight, reps=reps) set_order += 1 db.session.add(workout) db.session.commit() return redirect(url_for('index')) exercises = Exercises.query.all() return render_template('add_workout.html', exercises=exercises)
def test_user_workout(self): """Does workout get added to user? Then does a set get added to workout?""" test_workout = Workout( name = "test", date = "2021-01-01", time = 60 ) self.u.workouts.append(test_workout) db.session.commit() self.assertEqual(len(self.u.workouts), 1) test_set = Set( exercise="test_exercise", reps = 10, weight = 150, workoutid = test_workout.id ) test_workout.sets.append(test_set) db.session.commit() self.assertEqual(len(self.u.workouts.sets), 1)
def edit_photos(): upload = FileForm() set_loop = Set.query.order_by(Set.id.desc()).all() if upload.validate_on_submit(): for loop in set_loop: if request.form["select_set"] == loop.tag: file = request.files['file'] bucket.put_object(Key='Tractor Show/' + loop.name + '/' + file.filename, Body=file) newFile = Images(name=file.filename, set=loop) db.session.add(newFile) db.session.commit() return redirect('/dashboard/photos#' + loop.tag) add_set = AddSet() if add_set.validate_on_submit(): render_tag = tag_generator(request.form["set"]) new_set = Set(name=request.form["set"], tag=render_tag) db.session.add(new_set) db.session.commit() return redirect(url_for('edit_photos')) return render_template('authentication/edit-photos.html', upload=upload, add_set=add_set, set_loop=set_loop, s3projects=s3projects)
def post(self, class_name="", username=""): set_name = self.request.get('set-title') set_description = self.request.get('set-description') user = self.getCookieCacheUser() if set_name and set_description and class_name: if user: class_obj = memcache.get(str(user.key().id()) + class_name) if type(class_obj) is not Class: class_obj = self.cacheClass(user.key().id(), class_name) new_set = Set(name=set_name, description=set_description, total_attempts=0, total_scores=0, user=user, class_name=class_obj) new_set.put() memcache.set(str(class_obj.key().id()) + set_name, new_set) self.redirect("/%s/%s/%s" % (username, class_name, set_name)) else: self.render("createset.html", current_user=user, set_name=set_name, set_description=set_description, class_name=class_name, current_id=str(user.key().id()), class_active="active", page_title=class_name)
def cacheClassSets(self, class_obj): class_sets = memcache.get(str(class_obj.key().id())) if type(class_sets) is list: return class_sets else: class_sets = Set.all().filter('class_name =', class_obj) memcache.set(str(class_obj.key().id()), class_sets) return class_sets
def get_set_parts(set_id): params = {'key': API_KEY, 'format': 'json', 'set': set_id} response = requests.get(PARTS_URL, params) data = json.loads(response.text)[0] parts = [] for p in data['parts']: parts.append(Part(p['part_id'], p['element_id'], p['qty'])) return Set(data['set_id'], data['descr'], parts)
def cacheSet(self, set_name, class_obj): set_obj = memcache.get(str(class_obj.key().id()) + set_name) if type(set_obj) is Set: return set_obj else: set_obj = Set.all().filter("class_name =", class_obj).filter("name =", set_name).get() memcache.set(str(class_obj.key().id()) + set_name, set_obj) return set_obj
def newSet(self, ob): """Return new default linkset for ``ob``""" if self.types: s = Set(type=self.types) if self.inverse is not None: def maintainInverse(event): for other in event.removed: self.inverse.of(other).remove(ob) for other in event.added: self.inverse.of(other).add(ob) s.subscribe(maintainInverse, hold=True) return s raise TypeError("No types defined for " + repr(self))
def create_set(token): if not request.method == 'POST': abort(405) data = request.get_json() id = data.get('id') name = data.get('name') year = data.get('year') pieces = data.get('pieces') try: set = Set(id=id, name=name, year=year, pieces=pieces) set.insert() return jsonify({'success': True, 'created': set.short()}), 200 except SQLAlchemyError: set.rollback() abort(422)
def new_set(workout_id): """ Creates a new set on the workout page """ workout = Workout.query.get_or_404(workout_id) form = SetForm() if form.validate_on_submit(): new_set = Set( exercise = form.exercise.data, reps = form.reps.data, weight = form.weight.data, workoutid = workout_id ) workout.sets.append(new_set) db.session.commit() return redirect(url_for('view_workout',workout_id=workout_id)) return render_template('workout/new-set.html', form=form, workout=workout)
def test_set_relationship_with_user(self): """ Test that the relationship between set and user is \ correctly established """ test_user1 = Set.query.get(self.u1Id) my_set = Set.query.get(self.set1Id) self.assertEqual(test_user1, my_set.user) self.assertEqual(len(test_user1.sets), 2) set3 = Set( name="testset3", description="description3", ) test_user1.sets.append(set3) db.session.commit() # Test that test_user1.sets is a list containing my_set and set3? self.assertEqual(len(test_user1.sets), 3) self.assertIn(my_set, test_user1.sets) self.assertIn(set3, test_user1.sets)
def addset(): if request.method == 'POST': if current_user.is_authenticated(): user_id = current_user.id else: user_id = None list = request.json.get('data') title = request.json.get('title') if title == "": return json.dumps({ 'success': False, 'error': 'You must include a title' }), 400, { 'ContentType': 'application/json' } set = Set(title, user_id) for input in list: if input != None: word = Word.query.filter_by(id=input).first() if word: set.words.append(word) if len(set.words) == 0: return json.dumps({ 'success': False, 'error': 'You cannot leave all fields blank' }), 400, { 'ContentType': 'application/json' } db.session.add(set) db.session.commit() return json.dumps({ 'success': True, 'url': url_for('user') }), 200, { 'ContentType': 'application/json' } return render_template('addset.html', title="Add Set")
def config_set(): if request.method == 'POST': try: set_number = int(request.form.get('set_number')) land_id = int(request.form.get('land_id')) except ValueError as e: print("Wrong set_number or land_id") traceback.print_exc() return redirect(url_for('sets.configuration')) set_obj = Set.query.filter_by(number=set_number).first() if not set_obj: set_obj = Set(number=set_number) db.session.add(set_obj) db.session.commit() land = Land.query.filter_by(id=land_id).first() land.set_id = set_obj.id db.session.add(land) db.session.commit() return redirect(url_for('sets.configuration')) else: payload = request.get_json() set_id = payload['setId'] if 'setId' in payload else None land_id = payload['landId'] if 'landId' in payload else None land = Land.query.filter_by(id=land_id).first() land.set_id = None db.session.add(land) lands = Land.query.filter_by(set_id=set_id).all() if len(lands) < 1: Set.query.filter_by(id=set_id).delete() db.session.commit() return Response(status=200)
from models import Set, Exercise, Base from sqlalchemy import create_engine, text, and_, func from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///:memory:', echo=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() first = Set(exercise_name='Bench press', weight=75, number_of_reps=10, tempo='3010') second = Set(exercise_name='Bench press', weight=80, number_of_reps=8, tempo='3010') third = Set(exercise_name='Squat', weight=60, number_of_reps=6, tempo='3010') fourth = Set(exercise_name='Squat', weight=120, number_of_reps=3, tempo='3010') session.add_all([first, second, third, fourth]) session.commit() q = session.query(Set).filter(text('weight > 78 AND number_of_reps = 8')) q2 = session.query(Set).filter(and_(text('weight > 78')), (text('number_of_reps = 8'))) q3 = session.query(Set).filter( and_((Set.weight > 78), (Set.number_of_reps == 8))) q4 = session.query(Set).filter(Set.weight > 78).filter(Set.number_of_reps == 8)
def setUp(self): """Create test client, add sample data.""" User.query.delete() Set.query.delete() Verse.query.delete() u1 = User(first_name="fn1", last_name="ln1", email="*****@*****.**", username="******", password="******") db.session.add(u1) db.session.commit() verse1 = Verse(reference="ref1", verse='v1') verse2 = Verse(reference="ref2", verse="v2") db.session.add(verse1, verse2) db.session.commit() # Set 1 belonging to a user set1 = Set( name="testset1", description="description1", user_id=u1.id, created_at=None, ) db.session.add(set1) db.session.commit() u1.sets.append(set1) db.session.commit() # Set2 that is favorited by the user set2 = Set( name="testset2", description="description2", user_id=u1.id, ) u1.sets.append(set2) u1.favorites.append(set2) db.session.commit() # Add the verses to the set set1.verses = [verse1, verse2] db.session.commit() # Put user, set and verse ids on self self.u1Id = u1.id self.set1Id = set1.id self.set2Id = set2.id self.v1Id = verse1.id self.v2Id = verse2.id self.client = app.test_client()
'LoadEvent', 'Enumeration', 'Activator', 'RelationshipClass', 'EnumerationClass', 'ActiveDescriptor', 'EntityClass', ] from models import Set from events import Event from types import ClassType from query import AbstractFilter ClassTypes = type, ClassType NullSet = Set(type=()) def __setUUID(self, uuid): """Validate a UUID and save it in self._uuid unless it's already set Used to create a property for roles and entity/relationship classes to track their UUIDs. """ uustr = str(uuid).lower() if map(len, uustr.split('-')) == [8, 4, 4, 4, 12]: for c in uustr: if c not in '0123456789-abcdef': raise ValueError("%r is not valid in UUID format" % c) else: old = getattr(self, 'uuid', None)
def get_set_by_user(self, user, set_name): set_name = Set.all().filter('user ='******'name =', set_name) if set_name: return set_name.get()