def submit(country, wishes): if len(wishes) != 3: raise InvalidInput with connection() as conn, conn.cursor() as cur: if not _country_exists(country, cur): raise InvalidInput if not _wishes_exist(wishes, cur): raise InvalidInput for wish in wishes: cur.execute( 'insert into country_wish(country_id, wish_id, count) ' 'values (%s, %s, 1) ' 'on duplicate key update ' 'count = count + 1', ( country, wish, )) wishes_in_same_country = _wishes_in_same_country(country, wishes, cur) wishes_world_wide = _wishes_world_wide(wishes, cur) conn.commit() return wishes_in_same_country, wishes_world_wide
def countries(): with connection() as conn, conn.cursor() as cur: cur.execute('select id, name from country') result = [] for id, name in cur.fetchall(): result.append(Country(id, name)) return result
def wishes(): with connection() as conn, conn.cursor() as cur: cur.execute('select id, name from wish') result = [] for id, name in cur.fetchall(): result.append(Wish(id, name)) return result
def email(emailaddress): try : email_validator.validate_email(emailaddress) except email_validator.EmailNotValidError: raise InvalidInput with connection() as conn, conn.cursor() as cur : cur.execute("SELECT email_id from email where email_id = %s ",(emailaddress,)) if cur.rowcount == 1 : raise InvalidInput cur.execute("INSERT INTO email(email_id) values (%s)",(emailaddress,)) conn.commit()
def create_wish(name): name.strip() if re.search(r'^[\w\d\s]+$', name) is None: raise InvalidInput with connection() as conn, conn.cursor() as cur: cur.execute('Select id from wish where name = %s ', (name, )) if cur.rowcount == 1: raise InvalidInput cur.execute("INSERT INTO wish(name) values(%s)", (name, )) conn.commit() return cur.lastrowid
def emailNotification(): with connection() as conn, conn.cursor() as cur : cur.execute("SELECT * FROM email") for ids in cur : sendNotification(ids[0])