Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @login_manager.user_loader
- def user_loader(user_id):
- """Given *user_id*, return the associated User object.
- :param unicode user_id: user_id (email) user to retrieve
- """
- print('within user loader')
- print('user_id:')
- print(user_id)
- # try:
- # return User.query.get(user_id)
- # except:
- user = User(email=user_id)
- print('the user:')
- print(user)
- return user
- @login_manager.request_loader
- def request_loader(request):
- print('within request_loader')
- email = request.form.get('email')
- try:
- user = User.query.get(email)
- except:
- return
- # DO NOT ever store passwords in plaintext and always compare password
- # hashes using constant-time comparison!
- try:
- user.is_authenticated = request.form['password'] == user.password
- except:
- pass
- return user
- class CRUDMixin(object):
- """Mixin that adds convenience methods for CRUD (create, read, update, delete)
- operations.
- """
- @classmethod
- def create(cls, **kwargs):
- """Create a new record and save it the database."""
- instance = cls(**kwargs)
- return instance.save()
- def update(self, commit=True, **kwargs):
- """Update specific fields of a record."""
- for attr, value in kwargs.iteritems():
- setattr(self, attr, value)
- return commit and self.save() or self
- def save(self, commit=True):
- """Save the record."""
- # with application.app_context():
- db.session.add(self)
- if commit:
- db.session.commit()
- return self
- def delete(self, commit=True):
- """Remove the record from the database."""
- # with application.app_context():
- db.session.delete(self)
- return commit and db.session.commit()
- class Model(CRUDMixin, db.Model):
- """Base model class that includes CRUD convenience methods."""
- __abstract__ = True
- def __init__(self, **kwargs):
- db.Model.__init__(self, **kwargs)
- # From Mike Bayer's "Building the app" talk
- # https://speakerdeck.com/zzzeek/building-the-app
- class SurrogatePK(object):
- """A mixin that adds a surrogate integer 'primary key' column named
- ``id`` to any declarative-mapped class.
- """
- __table_args__ = {'extend_existing': True}
- id = db.Column(db.Integer, primary_key=True)
- @classmethod
- def get_by_id(cls, id):
- _id = to_int(id)
- if _id is not None:
- return cls.query.get(_id)
- return None
- class Website(Model, SurrogatePK):
- __tablename__ = 'websites'
- name = db.Column(db.String(100), unique=True)
- url = db.Column(db.String(250), unique=True)
- user_id = db.Column(db.Integer, db.ForeignKey('users.email'))
- # def __init__(self, name, url):
- # self.name = name
- # self.url = url
- def __repr__(self):
- return "{}: {}".format(self.name, self.url)
- class User(Model):
- """
- :param str email: email address of user
- :param str password: encrypted password for the user
- """
- __tablename__ = 'users'
- email = db.Column(db.String, primary_key=True)
- password = db.Column(db.String)
- authenticated = db.Column(db.Boolean, default=False)
- approved = db.Column(db.Boolean, default=False)
- websites = db.relationship('Website', backref='websites', lazy='dynamic')
- def is_active(self):
- """True, as all users are active."""
- return True
- def is_authenticated(self):
- """Return True if the user is authenticated."""
- return self.authenticated
- def is_anonymous(self):
- """False, as anonymous users aren't supported."""
- return False
- def get_id(self):
- """Return the email address to satisfy Flask-Login's requirements."""
- return self.email
- @application.route('/login', methods=['GET', 'POST'])
- def login():
- form = LoginForm(request.form)
- if request.method == 'POST':
- if form.validate_on_submit():
- email = form.email.data
- password = form.password.data
- user = User.query.filter_by(email=email, password=password).first()
- if user:
- login_user(user)
- current_user.authenticated = True
- print(current_user.authenticated)
- flash('Logged in successfully.')
- return redirect(url_for('main'))
- handle_login_form(form)
- return render_template('login.html', form=form)
- within user loader
- user_id:
- cchilder@mail.usf.edu
- the user:
- <app.User object at 0x7f4de90f1990>
- @application.route('/')
- @login_required
- def main():
- print('nnIn main')
- print(current_user.is_authenticated())
- print(current_user.approved)
- print(current_user)
- if not current_user:
- return "still having login issue"
- if current_user.is_authenticated and not current_user.approved:
- return redirect(url_for('awaiting_approval'))
- statuses = gather_statuses()
- length = len(statuses['system_statuses']) + len(statuses['website_statuses'])
- is_empty = length == 0
- return render_template('status.html', is_empty=is_empty, **statuses)
- In main
- None
- None
- <app.User object at 0x7f4de90f1990>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement