class Feedback(db.Model): """.""" __tablename__ = "feedback" feedback_id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False) email = db.Column(db.String(255), nullable=False) addressed = db.Column(db.Boolean, default=False, nullable=False) category_id = db.Column(db.Integer, db.ForeignKey('feedback_categories.category_id'), nullable=False) new_vendor_name = db.Column(db.String(32)) new_program_name = db.Column(db.String(32)) outgoing_name = db.Column(db.String(32)) receiving_name = db.Column(db.String(32)) new_numerator = db.Column(db.Integer) new_denominator = db.Column(db.Integer) feedback = db.Column(db.Text, nullable=False) user = db.relationship('User', backref=db.backref('feedback')) user = db.relationship('FeedbackCategory', backref=db.backref('feedback')) def __repr__(self): """Provide helpful representation when printed.""" return "<from {}: {}>".format( self.email, self.feedback, )
class TransactionHistory(db.Model): """Transactions that impact program balance.""" __tablename__ = "transaction_history" transaction_id = db.Column(db.Integer, autoincrement=True, primary_key=True) action_id = db.Column(db.Integer, db.ForeignKey('actions.action_id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False) program_id = db.Column(db.Integer, db.ForeignKey('programs.program_id'), nullable=False) beginning_balance = db.Column(db.Integer, nullable=False) ending_balance = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) user = db.relationship('User', backref=db.backref('transaction_history')) program = db.relationship('Program', backref=db.backref('transaction_history')) action = db.relationship('Action', backref=db.backref('transaction_history')) def __repr__(self): """Provide helpful representation when printed.""" return "<{} |user: {} |program: {} | beg: {} end: {} | {}>".format(self.transaction_id, self.user_id, self.program_id, self.beginning_balance, self.ending_balance, self.created_at, )
def seed_flights(): for line in open("dropflightstable.csv"): line.strip("\n") print line.split('"') # 123,,JFK,SEA,B6,63,"2015-09-02 09:01","2015-09-02 12:11",SEA,JFK,B6,464,"2015-09-16 13:19","2015-09-16 21:43",346,54,401,, flight_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=True) outbound_city_origin = db.Column(db.String(75), nullable=False) outbound_city_final_destination = db.Column(db.String(75), nullable=False) outbound_airline_code = db.Column(db.String(75), nullable=False) outbound_airline_name = db.Column(db.String(75), nullable=False) outbound_flight_number = db.Column(db.Integer, nullable=False) outbound_datetime_departure = db.Column(db.String(75), nullable=False) outbound_datetime_arrival = db.Column(db.String(75), nullable=False) inbound_city_origin = db.Column(db.String(75), nullable=False) inbound_city_final_destination = db.Column(db.String(75), nullable=False) inbound_airline_code = db.Column(db.String(75), nullable=False) inbound_airline_name = db.Column(db.String(75), nullable=False) inbound_flight_number = db.Column(db.Integer, nullable=False) inbound_datetime_departure = db.Column(db.String(75), nullable=False) inbound_datetime_arrival = db.Column(db.String(75), nullable=False) outbound_destination_city_airport = db.Column(db.String(75), nullable=True) inbound_destination_city_airport = db.Column(db.String(75), nullable=True) outbound_city_airport = db.Column(db.String(75), nullable=True) inbound_city_airport = db.Column(db.String(75), nullable=True) base_fare = db.Column(db.Integer, nullable=False) taxes = db.Column(db.Integer, nullable=False) total_fare = db.Column(db.Integer, nullable=False) user = db.relationship('User', backref=db.backref('flights', order_by=user_id))
class Book(db.Model): book_id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String, nullable=False) publish_date = db.Column(db.Date, nullable=False) subject = db.Column(db.String, nullable=False) genre = db.Column(db.String, nullable=False) notes = db.relationship('Note', backref=db.backref('book')) authors = db.relationship('Author', secondary=authorship_table, backref=db.backref('books', lazy='dynamic')) copies = db.relationship('BookCopy', cascade="all,delete", backref=db.backref('book')) def __repr__(self): return"<Book(book_id='%s',title='%s',publish_date='%s',subject='%s',genre='%s'," \ "book_note='%s',authors='%s',copies='%s'>" \ % (self.book_id, self.title, self.publish_date, self.subject, self.genre, self.notes, self.authors, self.copies) def to_dict(self): """ Method to transform Book to a dictionary object. :return: """ print('Book to_dict') book_dict = { 'book_id': self.book_id, 'title': self.title, 'publish_date': self.publish_date, 'subject': self.subject, 'genre': self.genre, 'notes': self.notes, 'authors': self.authors, 'copies': self.copies } return book_dict def update(self, **kwargs): """ Method to update a Book's attributes :param kwargs: Given a dictionary of valid key value pairs :return: None """ print('Book.update()') for key, value in kwargs.items(): setattr(self, key, value)
class FoundQR(db.Model): """ a qrcode found by a team """ team_id = db.Column(db.Integer, db.ForeignKey('team.id'), primary_key=True) qr_id = db.Column(db.String, db.ForeignKey('qr_code.id'), primary_key=True) qr = db.relationship('QRCode', backref=db.backref('teams', cascade='all, delete')) def __repr__(self): return f'<FoundQR {self.team.name} found {self.qr}>'
class User(db.Model): """ a user """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(), nullable=False, unique=True) team_id = db.Column(db.Integer, db.ForeignKey('team.id')) team = db.relationship('Team', backref=db.backref('users', uselist=True), uselist=False, foreign_keys=team_id, post_update=True) def __repr__(self): return f'<User {self.name} in {self.team.name if self.team else None}>'
class Balance(db.Model): """Balance a user has at program.""" __tablename__ = "balances" balance_id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False) action_id = db.Column(db.Integer, db.ForeignKey('actions.action_id'), nullable=False) program_id = db.Column(db.Integer, db.ForeignKey('programs.program_id'), nullable=False) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp()) current_balance = db.Column(db.Integer, nullable=False) user = db.relationship('User', backref=db.backref('balances', order_by=program_id)) program = db.relationship('Program', backref=db.backref('balances', order_by=balance_id)) action = db.relationship('Action', backref=db.backref('balances', order_by=balance_id)) def __repr__(self): """Provide helpful representation when printed.""" return "<{} |user: {} |program: {} | balance: {} | {}>".format(self.balance_id, self.user_id, self.program.program_name, self.current_balance, self.updated_at, ) def transferred_to(self, amount, ratio): """Update balance to receiving program balance after transfer.""" self.current_balance = self.current_balance + amount * ratio self.action_id = Action.query.filter(Action.action_type == 'Transfer').one().action_id def transferred_from(self, amount): """Update balance to outgoing program balance after transfer.""" self.current_balance = self.current_balance - amount self.action_id = Action.query.filter(Action.action_type == 'Transfer').one().action_id
class Picture(db.Model): team_id = db.Column(db.Integer, db.ForeignKey('team.id'), primary_key=True) objective_id = db.Column(db.Integer, db.ForeignKey('objective.id'), primary_key=True) filename = db.Column(db.String, nullable=False) status = db.Column(db.Integer, nullable=False) objective = db.relationship('Objective', backref=db.backref('teams', cascade='all, delete')) def __repr__(self): return f'<Picture for {self.objective} by {self.team.name}, status={self.status}>'
class Program(db.Model): """All loyalty program.""" __tablename__ = "programs" program_id = db.Column(db.Integer, autoincrement=True, primary_key=True) vendor_id = db.Column(db.Integer, db.ForeignKey('vendors.vendor_id'), nullable=False) type_id = db.Column(db.Integer, db.ForeignKey('program_types.type_id'), nullable=False) program_name = db.Column(db.String(32), nullable=False, unique=True) vendor = db.relationship('Vendor', backref=db.backref('programs', order_by=program_id)) def __repr__(self): """Provide helpful representation when printed.""" return "<program {}: {} | vendor: {}>".format(self.program_id, self.program_name, self.vendor.vendor_id, )
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) body = db.Column(db.Text) pub_date = db.Column(db.DateTime) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', backref=db.backref('posts', lazy='dynamic')) def __init__(self, title, body, category, pub_date=None): self.title = title self.body = body if pub_date is None: pub_date = datetime.utcnow() self.pub_date = pub_date self.category = category def __repr__(self): return '<Post %r>' % self.title
class BookCollection(db.Model): collection_id = db.Column(db.Integer, primary_key=True) book_ids = db.relationship(Book, secondary=collection_table, backref=db.backref('books', lazy='dynamic')) title = db.Column(db.String, nullable=False, unique=True) def __repr__(self): return "<Collection(collection_id='%s',books='%s',title='%s'>" \ % (self.collection_id, self.book.ids, self.title) def to_dict(self): """ Method to transform BookCollection to a dictionary object. :return: dictionary object. """ print('Book collections to_dict') list_of_books = [] for book in self.book_ids: list_of_books.append({ 'book_id': book.book_id, 'title': book.title }) print(list_of_books) collection_dict = { 'collection_id': self.collection_id, 'book_ids': list_of_books, 'title': self.title } print(collection_dict) return collection_dict def update(self, **kwargs): """ Method to update a BookCollection's attributes :param kwargs: Given a dictionary of valid key value pairs :return: None """ for key, value in kwargs: self[key] = value
class User(db.Model): """Creating class for users.""" __tablename__ = "users" user_id = db.Column(db.Integer, autoincrement=True, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64), nullable=False) username = db.Column(db.String(64), nullable=False) email = db.Column(db.String(64), nullable=False) password = db.Column(db.String(64), nullable=False) location = db.Column(db.String(64), nullable=True) profile_image = db.Column(db.Text, nullable=True) date_joined = db.Column(db.DateTime, nullable=False) following = db.relationship( 'User', secondary='connections', primaryjoin='Connection.follower_user_id == User.user_id', secondaryjoin='Connection.following_user_id == User.user_id', backref=db.backref('followers'), ) def __repr__(self): """For a more manageable way to print object's attributes.""" return "{}'s Info: {} {}, {}".format(self.username, self.first_name, self.last_name, self.email) @classmethod def get_by_username(cls, username): """Gets the user object given a username.""" try: return User.query.filter(User.username == username).one() except NoResultFound: print "No user found." return None @classmethod def add_new_user(cls, first_name, last_name, username, email, password, location=None, profile_image=None): """Add new user to the db.""" user = User(first_name=first_name, last_name=last_name, username=username, email=email, password=password, date_joined=datetime.today().date, location=location, profile_image=profile_image) db.session.add(user) db.session.commit() return user # WHERE DOES THIS ACTUALLY BELONG? def handle_signup_form_errors(cls, first_name, last_name, username, email, password, password_confirmed, location, profile_image): """Generates flash msgs depending on errors""" errors = [] if first_name or last_name or username or email or password: pass else: errors.append("Your profile is incomplete.") if password != password_confirmed: errors.append("The passwords you've entered don't match.") elif username in users: errors.append( "I'm sorry, it looks like that username is taken already. Please select a new one." ) return errors