Skip to content

Instantly share code, notes, and snippets.

@takwas
Last active April 29, 2016 15:18
Show Gist options
  • Save takwas/7394213d99ab6a9dd3510d73b7cf34e7 to your computer and use it in GitHub Desktop.
Save takwas/7394213d99ab6a9dd3510d73b7cf34e7 to your computer and use it in GitHub Desktop.
Flask-SQLAlchemy User Model
@user.route('/profile/<user_uid>')
@user.route('/profile/')
@user.route('/')
@login_required
def profile(user_uid=None):
if user_uid is None:
user = current_user
else:
user = db_ops.ret_val(db_ops.User, dict(uid=user_uid))
if user is None:
abort(404)
user.user_site_data.profile_views += 1
return render_template('user/profile.html', user=user)
import shortuuid
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(128), unique=True, nullable=False)
phone = db.Column(db.String(64), unique=True, nullable=False)
password_hash = db.Column(db.String, unique=False, nullable=False)
uid = db.Column(db.String, nullable=False, unique=True)
active = db.Column(db.Boolean, default=True, nullable=False, unique=False)
def __init__(self, email, password, phone=None):
print "self.id %r" %self.id
self.email = email
self.phone = phone
# will use `password` method to generate and save password hash
self.password = password
# Generate short UUID
uid = shortuuid.uuid() # current implementation uses `shortuid` but not sure if this is the right way to do it
self.uid = uid
self.active = True
def __repr__(self):
return '<User %r: %r>' % (self.email, self.phone)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment