Exemplo n.º 1
0
class ItemModel(db.Model):
    __tablename__ = "items"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False, unique=True)
    price = db.Column(db.Float(precision=2), nullable=False)

    store_id = db.Column(db.Integer, db.ForeignKey(
        "stores.id"), nullable=False)
    store = db.relationship("StoreModel")

    @classmethod
    def find_by_name(cls, name: str) -> "ItemModel":
        return cls.query.filter_by(name=name).first()

    @classmethod
    def find_all(cls) -> List["ItemModel"]:
        return cls.query.all()

    def save_to_db(self) -> None:
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self) -> None:
        db.session.delete(self)
        db.session.commit()
Exemplo n.º 2
0
class UserModel(db.Model):
    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), nullable=False, unique=True)
    email = db.Column(db.String(255), nullable=False, unique=True)
    password = db.Column(db.String(128), nullable=False)

    activation = db.relationship("ActivationModel",
                                 lazy="dynamic",
                                 cascade="all, delete-orphan")

    @property
    def most_recent_activation(self) -> "ActivationModel":
        return self.activation.order_by(db.desc(
            ActivationModel.expire_at)).first()

    @classmethod
    def find_by_username(cls, username: str) -> "UserModel":
        return cls.query.filter_by(username=username).first()

    @classmethod
    def find_by_email(cls, email: str) -> "UserModel":
        return cls.query.filter_by(email=email).first()

    @classmethod
    def find_by_id(cls, _id: int) -> "UserModel":
        return cls.query.filter_by(id=_id).first()

    def send_confirmation_email(self) -> Response:
        link = request.url_root[:-1] + url_for(
            "activation", activation_id=self.most_recent_activation.id)
        subject = "Registration activation"
        text = f"Please click the link to activate your registration: {link}"
        html = f'<html>Please click the link to activate your registration: <a href="{link}">Activation Link</a></html>'

        return Mailgun.send_email(self.email, subject, text, html)

    def save_to_db(self) -> None:
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self) -> None:
        db.session.delete(self)
        db.session.commit()

    def verify_password(self, password: str) -> bool:
        return bc.check_password_hash(self.password, password)
Exemplo n.º 3
0
class StoreModel(db.Model):
    __tablename__ = "stores"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False, unique=True)

    items = db.relationship("ItemModel", lazy="dynamic", viewonly=True)

    @classmethod
    def find_by_name(cls, name: str) -> "StoreModel":
        return cls.query.filter_by(name=name).first()

    @classmethod
    def find_all(cls) -> List["StoreModel"]:
        return cls.query.all()

    def save_to_db(self) -> None:
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self) -> None:
        db.session.delete(self)
        db.session.commit()
class ActivationModel(db.Model):
    __tablename__ = "activations"

    id = db.Column(db.String(50), primary_key=True)
    expire_at = db.Column(db.Integer, nullable=False)
    activated = db.Column(db.Boolean, nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    user = db.relationship("UserModel", viewonly=True)

    def __init__(self, user_id: int, **kwargs):
        super().__init__(**kwargs)
        self.user_id = user_id
        self.id = uuid4().hex
        self.expire_at = int(time()) + ACTIVATION_EXPIRATION_DELTA
        self.activated = False

    @classmethod
    def find_by_id(cls, _id: str) -> "ActivationModel":
        return cls.query.filter_by(id=_id).first()

    @property
    def expired(self) -> bool:
        return time() > self.expire_at

    def force_to_expire(self) -> None:
        if not self.expired:
            self.expire_at = int(time())
            self.save_to_db()

    def save_to_db(self) -> None:
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self) -> None:
        db.session.delete(self)
        db.session.commit()
Exemplo n.º 5
0
class UserModel(db.Model):
    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(10), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(256), nullable=False, unique=True)

    country = db.Column(db.String(50), nullable=False)
    phone_number = db.Column(db.String(30), nullable=False)
    state = db.Column(db.String(50), nullable=False)
    city = db.Column(db.String(50), nullable=False)

    confirmed = db.relationship("UserConfirmationModel",
                                lazy="dynamic",
                                cascade="all, delete-orphan")

    async def init(self, username, password, email, country: str, phone_number,
                   state, city, *args):
        self.username = username
        self.password = psw.generate_password_hash(password).decode('utf8')
        self.email = email
        self.country = await Country.get_country_name(Country, country)
        self.region = await Country.get_country_region(Country)
        self.phone_number = await Country.get_user_phonenumber(
            Country, phone_number)
        self.state = await Country.get_states(Country, state)
        self.city = await Country.get_city(Country, city)

    @property
    def recent_confirmation(self) -> "UserConfirmationModel":
        return self.confirmed.order_by(
            db.desc(UserConfirmationModel.token_expires_at)).first()

    @classmethod
    async def find_user_by_id(cls, id) -> "UserModel":
        return cls.query.filter(cls.id == id).first()

    @classmethod
    async def find_user_by_email(cls, email):
        return cls.query.filter(cls.email == email).first()

    @classmethod
    async def find_user_by_name(cls, name):
        return cls.query.filter(cls.username == name).first()

    async def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    async def send_email(self):
        subject = "Registration Confirmation"
        link = request.url_root[:-1] + url_for(
            "user_confirm.user_confirm") + "/" + str(
                self.recent_confirmation.confirmation_id)
        text = f"Please click the link to confirm your registration:{link}"
        html = f"<html>Click the link to confirm your registration:<a href={link}>Confirmation Token</a></html>"
        return await Mailgun.send_email([self.email], subject, text,
                                        html), "Done Here"

    async def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()

    def __repr__(self):
        return f"{self.id, self.username, self.email, self.password}"