class LearningObjective(db.Model): __tablename__ = 'learning_objectives' id = db.Column(db.Integer, primary_key=True) display_text = db.Column(db.String(40), nullable=False) network_id = db.Column(db.Integer, db.ForeignKey("social_networks.id")) network = db.relationship("SocialNetwork", foreign_keys=network_id) article_links = db.relationship("ArticleLink", backref=__tablename__) def __init__(self, display_text, network): self.display_text = display_text self.network = network def __repr__(self): return "<LearningObjective(display_text='%s', network='%s')>" % ( self.display_text, self.network.display_text) def dict_serialize(self): return { "id": self.id, "display_text": self.display_text, "network": self.network.display_text } def update_for_optional_params(self, json): if 'display_text' in json: self.display_text = json['display_text'] if 'network' in json: network = SocialNetwork.query.filter_by(display_text=json['network']).first() self.network = network
class SocialNetwork(db.Model): __tablename__ = 'social_networks' id = db.Column(db.Integer, primary_key=True) display_text = db.Column(db.String(40)) objectives = db.relationship("LearningObjective", backref=__tablename__) def __init__(self, display_text): self.display_text = display_text def __repr__(self): return "<SocialNetwork(display_text='%s')>" % (self.display_text) def dict_serialize(self): return {"id": self.id, "display_text": self.display_text}
class ScheduledMessage(db.Model, BaseModel): __tablename__ = 'scheduled_messages' id = db.Column(db.Integer, primary_key=True) facebook_id = db.Column(db.String(40)) topic = db.Column(db.String(80), nullable=False) next_day_to_send = db.Column(db.Integer) def __init__(self, facebook_id, topic, next_day_to_send=1): self.facebook_id = facebook_id self.topic = topic self.next_day_to_send = next_day_to_send def __repr__(self): return "<ScheduleMessage(user_id='%s', topic='%s', next_day_to_send='%s')>" % ( self.day, self.topic, self.next_day_to_send)
class ZendeskTicket(db.Model): __tablename__ = 'zendesk_tickets' # Zendesk Ticket ID id = db.Column(db.String(32), primary_key=True) status = db.Column(db.String(80), nullable=False) user_id = db.Column(db.String(20), db.ForeignKey("users.id")) user = db.relationship("User", foreign_keys=user_id) def __init__(self, ticket_id, status, user_id): self.id = ticket_id self.status = status self.user_id = user_id def __repr__(self): return "<ZendeskTicket(id='%d', status='%s', user_id='%s')>" % ( self.id, self.status, self.user_id)
class AdminUser(db.Model, BaseModel): __tablename__ = 'admin_users' email = db.Column(db.String(240), primary_key=True) password = db.Column(db.String(240), nullable=False) def __init__(self, email, password): self.email = email self.password = password def __repr__(self): return "<AdminUser(email='%s')>" % self.email @staticmethod def hash_password(password): return pbkdf2_sha256.hash(password) def verify_password(self, password): return pbkdf2_sha256.verify(password, self.password)
class AdminToken(db.Model, BaseModel): __tablename__ = 'admin_tokens' token = db.Column(db.String(64), primary_key=True) created_date = db.Column(db.DateTime) def __init__(self, token, created_date=datetime.datetime.utcnow()): self.token = token self.created_date = created_date def __repr__(self): return "<AdminToken(token='%s', created_date='%s')>" % ( self.token, self.created_date) @staticmethod def generate_and_save_token(): token = AdminToken(binascii.hexlify(os.urandom(32))) db.session().add(token) db.session().commit() return token
class UserObjective(db.Model): __tablename__ = "user_objectives" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.String(20), db.ForeignKey('users.id')) objective_id = db.Column(db.Integer, db.ForeignKey('learning_objectives.id')) timestamp = db.Column(db.DateTime) user = db.relationship(User, backref="user_objectives") objective = db.relationship(LearningObjective, backref="user_objectives") def __init__(self, user_id, objective_id, timestamp=datetime.datetime.utcnow()): self.user_id = user_id self.objective_id = objective_id self.timestamp = timestamp @classmethod def clear_selected_objectives(cls, user_id): cls.query.filter_by(user_id=user_id).delete() db.session().commit()
class ScheduledMessageContent(db.Model): __tablename__ = 'scheduled_message_contents' id = db.Column(db.Integer, primary_key=True) day = db.Column(db.Integer) topic = db.Column(db.String(80), nullable=False) title = db.Column(db.String(80)) description = db.Column(db.String(400)) link = db.Column(db.String(240)) image_url = db.Column(db.String(240)) def __init__(self, day, topic, title, description, link): if day < 1: raise ValueError("Day must be greater than zero") self.day = day self.topic = topic self.title = title self.description = description self.link = link def __repr__(self): return "<ScheduleMessageContent(day='%s', topic='%s', title='%s'," \ "description='%s', link='%s')>" % ( self.day, self.topic, self.title, self.description, self.link)
class User(db.Model, BaseModel): __tablename__ = 'users' id = db.Column(db.String(20), primary_key=True) # Facebook Page ID first_name = db.Column(db.String(80)) last_name = db.Column(db.String(80)) created_date = db.Column(db.DateTime) email = db.Column(db.String(256)) zendesk_id = db.Column(db.String(32)) tickets = db.relationship("ZendeskTicket", backref=__tablename__) def __init__(self, fb_id, first_name="", last_name="", created_date=datetime.datetime.utcnow(), email="", zendesk_id=""): self.id = fb_id self.first_name = first_name self.last_name = last_name self.created_date = created_date self.email = email self.zendesk_id = zendesk_id def __repr__(self): return "<User(id='%s', email='%s')>" % (self.id, self.email) def get_open_tickets(self): return [ x for x in self.tickets if x.status == ZendeskStatus.OPEN.value ] def get_solved_tickets(self): return [ x for x in self.tickets if x.status == ZendeskStatus.SOLVED.value ]
class ArticleLink(db.Model): __tablename__ = 'article_links' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(240), nullable=False) title = db.Column(db.String(80), nullable=False) description = db.Column(db.String(80)) image_url = db.Column(db.String(240)) link_type = db.Column(db.String(80)) objective_id = db.Column(db.Integer, db.ForeignKey("learning_objectives.id")) objective = db.relationship('LearningObjective', foreign_keys=objective_id) def __init__(self, url, title, objective, link_type, description=None, image_url=None): self.url = url self.title = title self.objective = objective self.link_type = link_type if not description and not image_url: raise ValueError( "Must be provided either a description or image url - both cannot be empty." ) if description: self.description = description if image_url: self.image_url = image_url def __repr__(self): return "<ArticleLink(title='%s', url='%s', objective='%s')>" % ( self.title, self.url, self.objective.display_text) def dict_serialize(self): return { "id": self.id, "url": self.url, "title": self.title, "description": self.description, "image_url": self.image_url, "link_type": self.link_type, "objective": self.objective.display_text, "network": self.objective.network.display_text } def update_for_optional_params(self, json): if 'url' in json: self.url = json['url'] if 'title' in json: self.title = json['title'] if 'description' in json: self.description = json['description'] if 'image_url' in json: self.image_url = json['image_url'] if 'link_type' in json: self.link_type = str(json['link_type']).upper() if 'objective' in json: network = SocialNetwork.query.filter_by( display_text=json['network']).first() objective = LearningObjective.query.filter_by( display_text=json['objective'], network=network).first() self.objective = objective