Example #1
0
class User(UserMixin, SurrogatePK, Model):

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)

    def __init__(self, username, email, password=None, **kwargs):
        db.Model.__init__(self, username=username, email=email, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        return bcrypt.check_password_hash(self.password, value)

    @property
    def full_name(self):
        return "{0} {1}".format(self.first_name, self.last_name)

    def __repr__(self):
        return '<User({username!r})>'.format(username=self.username)
Example #2
0
class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    address = Column(db.String(50), nullable=True)
    active = Column(db.Boolean(), default=True)
    admin = db.Column(db.Boolean, nullable=False, default=False)
    confirmed = db.Column(db.Boolean, nullable=False, default=False)
    confirmed_on = db.Column(db.DateTime, nullable=True)
    is_banned = Column(db.Boolean(), default=False)
    spot_id = Column(db.String(50), nullable=True)
    dest_id = Column(db.String(50), nullable=True)
    place_id = Column(db.String(50), nullable=True)
    spot_avail = Column(db.Boolean(), default=False)
    phone = Column(db.Unicode(50), unique=False, nullable=True)

    def __init__(self,
                 username,
                 email,
                 password=None,
                 admin=False,
                 confirmed_on=None,
                 **kwargs):
        """Create instance."""
        db.Model.__init__(self,
                          username=username,
                          email=email,
                          admin=admin,
                          confirmed_on=confirmed_on,
                          **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    @property
    def full_name(self):
        """Full user name."""
        return '{0} {1}'.format(self.first_name, self.last_name)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<User({username!r})>'.format(username=self.username)
Example #3
0
class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = "users"
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.LargeBinary(128), nullable=True)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)

    def __init__(self, username, email, password=None, **kwargs):
        """Create instance."""
        db.Model.__init__(self, username=username, email=email, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    @property
    def full_name(self):
        """Full user name."""
        return f"{self.first_name} {self.last_name}"

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<User({self.username!r})>"
Example #4
0
class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    address = Column(db.String(50), nullable=True)
    active = Column(db.Boolean(), default=True)
    admin = Column(db.Boolean, nullable=False, default=False)
    confirmed = Column(db.Boolean, nullable=False, default=False)
    confirmed_on = Column(db.DateTime, nullable=True)
    confirm_code = Column(db.Unicode(50), nullable=True)
    is_banned = Column(db.Boolean(), default=False)
    phone = Column(db.Unicode(50), unique=False, nullable=True)
    #avatar_hash = Column(db.String(32))
    #last_seen_at = db.Column(db.Integer, default=timestamp)
    #online = db.Column(db.Boolean, default=False)

    def __init__(self, username, email, password=None, admin=False, confirmed_on=None, **kwargs):
        """Create instance."""
        db.Model.__init__(self, username=username, email=email, admin=admin, confirmed_on=confirmed_on, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def gravatar_hash(self, **kwargs):
        if self.email is not None and self.avatar_hash is None:
            self.avatar_hash = hashlib.md5(
                self.email.encode('utf-8')).hexdigest()

    def gravatar(self, size=100, default='identicon', rating='g'):
        """call gravatar with size arg 1-128 user.gravatar(size=128)"""
        if request.is_secure:
            url = 'https://secure.gravatar.com/avatar'
        else:
            url = 'http://www.gravatar.com/avatar'
        hash = self.avatar_hash or \
               hashlib.md5(self.email.encode('utf-8')).hexdigest()
        return '{url}/{hash}?s={size}&d={default}&r={rating}'.format(
            url=url, hash=hash, size=size, default=default, rating=rating)

    # https://github.com/miguelgrinberg/flack/blob/master/flack/events.py
    def ping(self):
        """Marks the user as recently seen and online.
        mobile app clients must send a ping event periodically
         to keep the user online"""
        self.last_seen_at = timestamp()
        last_online = self.online
        self.online = True
        return last_online != self.online

    @staticmethod
    def find_offline_users():
        """Find users that haven't been active and mark them as offline."""
        users = User.query.filter(User.last_seen_at < timestamp() - 60,
                                  User.online == True).all()  # noqa
        for user in users:
            user.online = False
            db.session.add(user)
        db.session.commit()
        return users

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    @property
    def full_name(self):
        """Full user name."""
        return '{0} {1}'.format(self.first_name, self.last_name)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<User({username!r})>'.format(username=self.username)
Example #5
0
class AddressEntry(SurrogatePK, Model):
    __tablename__ = 'address'
    user_id = Column(db.ForeignKey('users.id'), unique=False,
                     nullable=False)  # Owner
    latitude = Column(db.String(80), unique=False, nullable=False)
    longitude = Column(db.String(80), unique=False, nullable=False)
    mapbox_place_name = Column(db.String(1024), unique=False, nullable=True)
    street_name = Column(db.Unicode(1024), unique=False, nullable=True)
    street_number = Column(db.Unicode(1024), unique=False, nullable=True)
    building_number = Column(db.Unicode(1024), unique=False, nullable=True)
    cross_street = Column(db.Unicode(1024), unique=False, nullable=True)
    suite_number = Column(db.Unicode(1024), unique=False, nullable=True)
    neighborhood = Column(db.Unicode(1024), unique=False,
                          nullable=True)  # mapbox.neighborhood
    district = Column(db.Unicode(1024), unique=False,
                      nullable=True)  # mapbox.place
    county = Column(db.Unicode(1024), unique=False, nullable=True)
    country = Column(db.Unicode(1024), unique=False,
                     nullable=True)  # mapbox.country
    provence = Column(db.Unicode(1024), unique=False, nullable=True)
    phone = Column(db.Unicode(10), unique=False, nullable=True)
    state = Column(db.Unicode(1024), unique=False,
                   nullable=True)  # mapbox.region

    #Sometimes postal_code comes back NULL from mapbox geocoder if it's a weird address
    #set postalcode to be nullable true to account for the weirdness
    postal_code = Column(db.Unicode(1024), unique=False,
                         nullable=False)  # mapbox.postcode
    name = Column(db.Unicode(1024), unique=False, nullable=True)
    details = Column(db.Text, unique=False, nullable=True)
    price = Column(db.Float, nullable=True)
    is_dest = Column(db.Boolean(), default=True)
    is_avail = Column(db.Boolean(), default=True)

    photo_url = Column(db.Unicode(300), unique=False, nullable=True)
    spot_type = Column(db.Unicode(50), unique=False, nullable=True)
    avail_type = Column(db.Unicode(50), unique=False, nullable=True)

    def format_phone(self, phone_raw):
        return phone_raw

    def as_geojson(self, is_dest=False):
        return {
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                # Mapbox geocoding api sends back lat long in the format of long/lat. This may have
                #  to be rendered differently for the inclusion in the mapbox map.
                'coordinates':
                [self.longitude,
                 self.latitude],  # match mapbox specification, t
            },
            'properties': {
                'p0': self.id,
                'p1': self.name,
                'p2': self.mapbox_place_name,
                'p4': self.phone,
                'p5':
                ' '.join([self.street_number or '', self.street_name or '']),
                'p6': self.state,
                'p8': self.cross_street,
                'p9': self.price,
                'marker-color': '#ff8888' if is_dest else '#66a3ff'
            }
        }
Example #6
0
class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.Binary(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)
    email_confirmed = Column(db.Boolean(), nullable=True, default=False)
    email_confirmed_at = Column(db.DateTime, nullable=True)

    def __init__(self, username, email, password=None, **kwargs):
        """Create instance."""
        db.Model.__init__(self, username=username, email=email, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    @property
    def full_name(self):
        """Full user name."""
        return '{0} {1}'.format(self.first_name, self.last_name)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<User({username!r})>'.format(username=self.username)

    def get_reset_password_token(self, expires_in=600):
        return jwt.encode(
            {'reset_password': self.id, 'exp': time() + expires_in},
            current_app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        try:
            user_id = jwt.decode(token, current_app.config['SECRET_KEY'],
                                 algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(user_id)

    def confirm_email(self):
        self.email_confirmed = True
        self.email_confirmed_at = dt.datetime.utcnow()

    def get_confirmation_token(self):
        return jwt.encode(
            {'confirm_email': self.id},
            current_app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_confirmation_token(token):
        try:
            user_id = jwt.decode(token, current_app.config['SECRET_KEY'],
                                 algorithms=['HS256'])['confirm_email']
        except:
            return
        return User.query.get(user_id)