def unique_email(form, field): session = Session() email = session.query(User).filter(User.email == field.data).first() session.close() if email: raise ValidationError('%s[%s] is already registered' % (field.name, field.data))
def stock_exist_valid(code): if(code in ('sz','sh')): return True sql = "select code,name from [stock].[comp_basic] where code='{code}'".format(code=code) s = Session() r = s.execute(sql).fetchall() return r.__len__() > 0
def stock_exist_valid(code): if (code in ('sz', 'sh')): return True sql = "select code,name from [stock].[comp_basic] where code='{code}'".format( code=code) s = Session() r = s.execute(sql).fetchall() return r.__len__() > 0
def load_user(user_id): from app.models import User from app.db_info import Session session = Session() user = session.query(User).options(subqueryload( User.address)).filter(User.id == user_id).first() session.close() return user
def mock_appt_bill(): with app.app_context(): session = Session() appts = [ [2, 1, dt.date(2018, 7, 17), 2,[1,2]], [2, 1, dt.date(2018, 7, 18), 3,[2,3]], [2, 1, dt.date(2018, 7, 19), 1,[1,4]], ] for a in appts: appt = Appt(a[0],a[1],a[2],a[3]) session.add(appt) total_fee = 0 for s in a[4]: appt_service = Appt2Ser(appt, s) total_fee += SERVICES[s][2] session.add(appt_service) bill = Bill(appt,2,total_fee) session.add(bill) session.commit()
def mock_service(): with app.app_context(): session = Session() for s in SERVICES: service = Service(s[0], s[1], s[2]) session.add(service) session.commit()
def mock_appt_slot(): with app.app_context(): session = Session() slots = ["9:30 - 10:00", "10:00 - 11:30", "12:30 - 14:00", "14:00 - 15:30", "15:30 - 17:00", "17:00 - 18:30"] for s in slots: time_slot = ApptTimeSlot(s) session.add(time_slot) session.commit()
class ApptForm(Form): id = IntegerField("Appointment ID") appt_date = DateField("Appointment Date") appt_timeslot = NoValidationSelectField("Time Slot", coerce=int, choices=[(1, "Select date first")]) appt_service = SelectMultipleField("Services", coerce=int, choices=Session().query( Service.id, Service.type, Service.desc).all()) pet = NoValidationSelectField("Pet to Serve", coerce=int, choices=[], validators=[DataRequired()]) def pre_validate(self, form): pass
def mock_user_pet(): with app.app_context(): session = Session() address = Address("Melbourne", "Queen", 3000) md5 = hashlib.md5() md5.update("pwd".encode('utf-8')) password = md5.hexdigest() tom = User(email="*****@*****.**", password=password, first_name="Tom", last_name="Unimelb", dob=dt.date(1989, 9, 8), gender="M", phone="000000", home_number="000000", work_number="000000", address=address, active=True, admin=True, register_date=dt.datetime.now()) user1 = User(email="*****@*****.**", password=password, first_name="User1", last_name="Unimelb", dob=dt.date(1999, 12, 8), gender="M", phone="000000", home_number="000000", work_number="000000", address=address, active=True, admin=False, register_date=dt.datetime.now()) user2 = User(email="*****@*****.**", password=password, first_name="User2", last_name="Unimelb", dob=dt.date(1988, 7, 17), gender="F", phone="000000", home_number="000000", work_number="000000", address=address, active=True, admin=False, register_date=dt.datetime.now()) pet1 = Pet(user1, "Puppy1", "breed1", "M", dt.date(2017, 7, 11)) pet2 = Pet(user1, "Puppy2", "breed2", "M", dt.date(2017, 7, 12)) pet3 = Pet(user1, "Puppy3", "breed3", "F", dt.date(2017, 7, 13)) card1 = Card(user1, "1234567890123456", "ANZ") card2 = Card(user1, "2234567890123456", "ANZ") session.add(address) session.add(tom) session.add(user1) session.add(user2) session.add(pet1) session.add(pet2) session.add(pet3) session.add(card1) session.add(card2) session.commit()
def run_sql_via_sqlalchemy(sql): session = Session() r = session.execute(sql) result = r.fetchall() session.close() return result