def from_db(cls, id_): # lookup event sql = 'SELECT * FROM event ' \ ' WHERE ? = id_' row = query_db(sql, [id_], one=True) if row is None: return None e = cls(**row) # lookup buddies for event sql = 'SELECT * FROM event_to_buddies as e2b ' \ ' WHERE ? = e2b.event_id' rows = query_db(sql, [e.id_]) for row in rows: b = Buddy.from_db(id_=row['buddy_id']) assert b is not None e.buddies.append(b) if row['is_owner']: e.owners.append(b) return e
def setUp(self): app.config['TESTING'] = True self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() self.app = app.test_client() db.init() with app.app_context(): with db.get_db(): sql = 'INSERT INTO buddy (hash_, name, email) VALUES (?, ?, ?)' db.query_db(sql, [self.fakehash, self.fakename, self.fakemail])
def get_restrictions(self): sql = 'SELECT buddy.* from buddy' \ ' JOIN restrictions' \ ' WHERE restrictions.santa_id = ?' \ ' AND restrictions.buddy_id = buddy.id_' result = [] rows = query_db(sql, [self.id_]) for row in rows: result.append(Buddy(**row)) return result
def get_events(self, from_db=False): sql = 'SELECT event.* FROM event' \ ' JOIN event_to_buddies as map' \ ' WHERE map.buddy_id = ?' \ ' AND map.event_id = event.id_' if self.buddies is None or from_db: self.buddies = defaultdict(list) rows = query_db(sql, [self.id_]) for row in rows: unused = self.buddies[Event(**row)] return self.buddies.keys()
def from_db(cls, email=None, hash_=None, id_=None): assert (email is not None) ^ (hash_ is not None) ^ (id_ is not None) # the above xor (with three operands) will result to True when all three operands are # True; so we specifically check against that as well assert (email is None) or (hash_ is None) or (id_ is None) sql = 'SELECT * FROM buddy ' \ ' WHERE ? = email OR ? = hash_ OR ? = id_' row = query_db(sql, [email, hash_, id_], one=True) if row is None: return None return cls(**row)
def get_buddies(self, from_db=False): sql = 'SELECT buddy.* from buddy' \ ' JOIN pair' \ ' WHERE pair.santa_id = ?' \ ' AND pair.event_id = ?' \ ' AND pair.buddy_id = buddy.id_' if self.buddies is None or from_db: events = self.get_events(from_db) for ev in events: rows = query_db(sql, [self.id_, ev.id_]) for row in rows: b = Buddy(**row) self.buddies[ev].append(b) return self.buddies.values()
def index(): sql = 'select name, description, image from event order by name desc' cur = get_db().execute(sql) entries = [dict(name=row[0], email=row[1]) for row in cur.fetchall()] return render_template('show_entries.html', entries=query_db(sql))