def profile_create(user=None): user_id = get_jwt_identity() profile_fields = profile_schema.load(request.json) profile = Profile.query.get(user_id) if not profile: new_profile = Profile() new_profile.username = profile_fields["username"] new_profile.fname = profile_fields["fname"] new_profile.lname = profile_fields["lname"] new_profile.account_active = profile_fields["account_active"] new_profile.admin = profile_fields["admin"] user.profile.append(new_profile) db.session.add(new_profile) db.session.commit() return jsonify(profile_schema.dump(new_profile)) else: return abort(401, description='User Profile already exists')
def profile_create(user): user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Do I know you?") profile_fields = profile_schema.load(request.json) is_profile = Profile.query.get(user.id) if not is_profile: new_profile = Profile() new_profile.username = profile_fields["username"] new_profile.fname = profile_fields["fname"] new_profile.lname = profile_fields["lname"] user.profile_id.append( new_profile ) # user = defined above; client_id is linked as relationship to user: client_id = db.relationship("Client", backref=backref("users", uselist=False)) in users table db.session.add(new_profile) db.session.commit() #return jsonify(client_schema.dump(new_profile)) return render_template("Profile.html", new_profile=new_profile) else: return abort(401, description='Profile already exists')
def seed_db(): from models.User import User from models.Profile import Profile from faker import Faker from main import bcrypt from models.Equipment import Equipment from models.EquipmentOrder import EquipmentOrder import random from random import seed from random import randint faker = Faker() profiles = [] equipments = [] categories = ["dumbells", "cardio", "machine", "yoga", "mobility"] for i in range(10): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) db.session.commit() for i in range(10): profile = Profile() profile.username = faker.name() profile.fname = faker.first_name() profile.lname = faker.last_name() profile.account_active = faker.boolean() profile.user_id = i + 1 profiles.append(profile) db.session.add(profile) db.session.commit() for i in range(30): equipment = Equipment() equipment.equipment_name = faker.name() equipment.equipment_description = faker.catch_phrase() equipment.rented = random.choice([True, False]) equipment.rentpw = randint(8, 120) equipment.owner_id = random.choice(profiles).profileid equipment.category = random.choice(categories) equipments.append(equipment) db.session.add(equipment) for i in range(30): equipment_order = EquipmentOrder() equipment_order.order_begin_date = faker.date_between(start_date='-1y', end_date='today') equipment_order.order_return_date_estimate = faker.date_between( start_date='today', end_date='+1y') equipment_order.order_actual_return_date = faker.date_between( start_date='today', end_date='+1y') equipment_order.order_active = random.choice([True, False]) equipment_order.equipment_id = randint(1, 29) # equipment_order.equipment_id = random.choice(equipments).id equipment_order.hirer_id = random.choice(profiles).profileid db.session.add(equipment_order) db.session.commit() print("Tables seeded")