class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(80), unique=True) name = db.Column(db.String(30)) videos = db.relationship("Video") def __init__(self, name, email): self.name = name self.email = email def save(self): try: db.session.add(self) db.session.commit() except: db.session.flush() db.session.rollback() print(self.id) @classmethod def load(cls, new_db): """ change db for production """ global db db = new_db
class User(db.Model): id = db.Column(db.Integer, primary_key=True) password = db.Column(db.String(60)) email = db.Column(db.String(254), nullable=False, default="", server_default="") active = db.Column(db.Boolean, nullable=False, default=False, server_default=expression.true())
class Video(db.Model): __tablename__ = "videos" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(30)) path = db.Column(db.String(120), default="") desc = db.Column(db.String(400), default="") author_id = db.Column(db.Integer, db.ForeignKey("users.id")) has_voted = db.Column(db.Boolean, default=False) is_owner = db.Column(db.Boolean, default=False) votes = db.relationship("Vote") created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow()) updated_date = db.Column(db.DateTime, default=datetime.datetime.utcnow()) tags = db.relationship('Tag', secondary=videotags) def __init__(self, title="", path="", desc="", tags=tags): self.title = title self.path = path self.desc = desc self.created_date = datetime.datetime.utcnow() def add_tags(self, tag_names): for tag_name in list(set(tag_names)): t = Tag.get_or_create(tag_name) self.tags.append(t) db.session.add(t) db.session.commit() def vote_up(self, user_id): vote = Vote() vote.user_id = user_id self.votes.append(vote) db.session.add(vote) db.session.commit() def save(self): try: db.session.add(self) db.session.commit() except: db.session.flush() db.session.rollback() def __repr__(self): return "<Video id:{0} title:{1} voted:{2}>".format( self.id, self.title, self.has_voted) @classmethod def load(cls, new_db): """ change db for production """ global db db = new_db
class Tag(db.Model): __tablename__ = "tags" id = db.Column(db.Integer, primary_key=True) # videos = db.relationship('Video', secondary=videotags) title = db.Column(db.String(30)) videos = db.relationship("Video", secondary=videotags) def __init__(self, title): self.title = title def __repr__(self): return "<Tag:{0},title:{1}>".format(self.id, self.title) @classmethod def get_or_create(cls, tag_name): t = db.session.query(Tag).filter_by(title=tag_name).first() if t is None: t = Tag(tag_name) try: db.session.add(t) db.session.commit() except: print("err") db.session.flush() db.session.rollback() return t
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(254), nullable=False, unique=True, index=True) password = db.Column(db.String(60)) name = db.Column(db.String(254), nullable=False, default="", server_default="") email = db.Column(db.String(254), nullable=False, default="", server_default="") active = db.Column(db.Boolean, nullable=False, default=False, server_default=expression.true()) groups = db.relationship(Group, lambda: user_group, backref=db.backref("users"))
class Realtor(IDModel): # TODO: getting some repeat licenese numbers, check it out event_target = "ctl00$DefaultContent$RadGridLists$ctl00$ctl04$ctl00" target_file = "Individuals.txt" last_name = db.Column(db.String(100), nullable=False, index=True) first_name = db.Column(db.String(100), nullable=False, index=True) employer_lic_number = db.Column(db.String(11), nullable=False) employer_phone = db.Column(db.String(20), nullable=False) # increase lengths employer_fax = db.Column( db.String(20), nullable=True ) # increase lengths , make nullable mailing_address1 = db.Column(db.String(75), nullable=False) mailing_address2 = db.Column(db.String(75), nullable=True) mailing_city = db.Column(db.String(50), nullable=False) mailing_zip = db.Column(db.Integer, nullable=False) mailing_state = db.Column(db.String(2), nullable=False) broker_id = db.Column(db.Integer, db.ForeignKey("broker.id")) @classmethod def clean_mailing_state(cls, value): return cls.clean_state(value) @classmethod def clean_mailing_zip(cls, value): return cls.clean_zip(value) @classmethod def match_to_brokers(cls): broker_dict = {broker.lic_number: broker.id for broker in Broker.query.all()} for realtor in cls.query.all(): broker_id = broker_dict.get(realtor.employer_lic_number, None) realtor.broker_id = broker_id db.session.add(realtor) db.session.commit()
class Broker(IDModel): event_target = "ctl00$DefaultContent$RadGridLists$ctl00$ctl06$ctl00" target_file = "Entities.txt" legal_name = db.Column(db.String(100), nullable=True) phone = db.Column(db.String(20), nullable=False) fax = db.Column(db.String(20), nullable=True) address1 = db.Column(db.String(75), nullable=False) address2 = db.Column(db.String(75), nullable=True) city = db.Column(db.String(50), nullable=False) zip = db.Column(db.Integer, nullable=False) state = db.Column(db.String(2), nullable=False) realtors = db.relationship("Realtor")
class Group(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True, index=True)
class IDModel(db.Model): __abstract__ = True id = db.Column(db.Integer, primary_key=True) lic_number = db.Column(db.String(11), unique=True, nullable=False) lic_status = db.Column( db.Boolean, nullable=False, default=False, server_default=expression.true() ) expire_date = db.Column(db.DateTime) @property def json(self): return to_json(self, self.__class__) @classmethod def cache(cls): cls.query.delete() for row in get_objects(cls.event_target, cls.target_file): if row["LicCategory"] != "Real Estate": continue row_dict = cls.clean_data(row) obj = cls(**row_dict) db.session.add(obj) try: db.session.commit() except (sqlalchemy.exc.IntegrityError, sqlalchemy.exc.DataError) as e: db.session.rollback() @classmethod def clean_methods(cls): methods = { func[6:]: func for func in dir(cls) if callable(getattr(cls, func)) and func.startswith("clean") } methods.pop("data") methods.pop("methods") return methods @classmethod def clean_data(cls, row): data = {} clean_methods = cls.clean_methods() columns = set(x.name for x in cls.__table__.columns) for key, value in row.items(): new_key = camelToSnake(key) if new_key not in columns: continue if new_key in clean_methods: func = getattr(cls, clean_methods[new_key]) data[new_key] = func(value) else: data[new_key] = value.title() return data @classmethod def clean_lic_status(cls, value): if value == "Active": return True return False @classmethod def clean_expire_date(cls, value): if value: return datetime.datetime.strptime(value, "%m/%d/%Y") @classmethod def clean_state(cls, value): if len(value) == 2: return value.upper() @classmethod def clean_zip(cls, value): match = re.search("\d+|$", value).group() if match: return int(match)
class Video(db.Model): __tablename__ = "videos" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(30)) author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30)) videos = db.relationship("Video") description = db.Column(db.String(140))