This question already has answers here:
How Can I Automatically Populate SQLAlchemy Database Fields? (Flask-SQLAlchemy)
(2 answers)
Closed 4 years ago.
I am new to SQLAlchemy and have been unable to set the DateTime for created. I have tried using the "default" option found in many examples. I have also tried setting it manually (I have it commented out). Neither have worked so far. Any help would be appreciated.
models.py
import datetime
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(40))
lastname = db.Column(db.String(40))
email = db.Column(db.String(120), unique=True)
created = db.Column(db.DateTime, default=datetime.datetime.utcnow())
confirmed = db.Column(db.DateTime, nullable=True)
pwdhash = db.Column(db.String(100))
def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)
#self.created = datetime.datetime.utcnow()
self.confirmed = None
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
routes.py
from tasks import app
from datetime import datetime
from flask import render_template, request, flash, session, url_for, redirect
from forms import ContactForm, SignupForm, SigninForm
from flask.ext.mail import Message, Mail
from models import db, User, Tags, Tasks
#app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if 'email' in session:
return redirect(url_for('profile'))
if request.method == 'POST':
if form.validate() == False:
return render_template('signup.html', form=form)
else:
newuser = User(form.firstname.data, form.lastname.data, form.email.data, form.password.data)
db.session.add(newuser)
db.session.commit()
session['email'] = newuser.email
return redirect(url_for('profile'))
elif request.method == 'GET':
return render_template('signup.html', form=form)
The problem with your default is that you're calling datetime.utcnow immediately there, and the value returned (at the time of class definition) is always used as default. You need to pass the callable itself like following:
# Note the lack of parenthesis after datetime.utcnow
created = db.Column(db.DateTime, default=datetime.datetime.utcnow)
This way SQLAlchemy will call datetime.utcnow itself upon row insert.
Related
How can I create a default guest user with username guest and password equal to password when I start the web server; i.e. flask run?
The purpose of this default guest user is to be a demo user so that the actual user doesn't have to register and be able to test and tour the web app.
models.py
from flask_login import UserMixin
from app import bcrypt, db, login_manager
#login_manager.user_loader
def load_user(user_id: int):
return User.query.get(user_id)
class User(db.Model, UserMixin):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True)
username = db.Column(db.String(60), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
def __init__(self, username="guest", password="password"):
self.username = username
self.password = bcrypt.generate_password_hash(password).decode("UTF-8")
def __repr__(self):
return f"<User {self.username!r}>"
#classmethod
def authenticate(cls, username, password):
user = cls.query.filter_by(username=username).first()
if user and bcrypt.check_password_hash(user.password, password):
return user
return False
views.py
from flask import Blueprint, flash, redirect, render_template, url_for
from flask_login import current_user, login_user
from app.auth.forms import SigninForm
from app.models import User
auth = Blueprint("auth", __name__, url_prefix="/auth")
#auth.route("/signin", methods=["GET", "POST"])
def signin():
if current_user.is_authenticated:
return redirect(url_for("main.home"))
form = SigninForm()
if form.validate_on_submit():
user = User.authenticate(
username=form.username.data, password=form.password.data
)
if user:
login_user(user, remember=form.remember.data)
flash(f"Hello, {form.username.data}", category="info")
return redirect(url_for("main.home"))
else:
flash("Login Unsuccessful. Please check username and password", "danger")
return render_template(
"auth/signin.html", title="Sign in", icon="log-in", form=form
)
EDIT:
My idea is to add the guest user manually to the DB even before the flask run. But what if the code is redistributed? I want to make the guest user creation to be automated using Python.
You could try the before_first_request decorator.
Docs: https://flask.palletsprojects.com/en/2.2.x/api/#flask.Flask.before_first_request
So something like this
from flask import Flask
app = Flask(__name__)
...
#app.before_first_request
def create_guest_user():
# include code to create new user here
pass
I'm trying to make a login page I have been trying for weeks but this error keeps popping up:
sqlalchemy.orm.exc.FlushError: Instance <Users at 0x10bd8c580> has a NULL identity key.
The error lies in the register.py file. Apparently flask doesn't like me using .commit() or .add(). I've also tried to use .flush
but it gave me the same error still
register.py:
from flask import Blueprint, url_for, render_template, redirect, request
from flask_login import LoginManager
from werkzeug.security import generate_password_hash
import sqlalchemy
from models import db, Users
register = Blueprint('register', __name__, template_folder='../frontend')
login_manager = LoginManager()
login_manager.init_app(register)
#register.route('/register', methods=['GET', 'POST'])
def show():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
confirm_password = request.form['confirm-password']
if username and email and password and confirm_password:
if password == confirm_password:
hashed_password = generate_password_hash(
password, method='sha256')
try:
new_user = Users(
username=username,
email=email,
password=hashed_password,
)
db.session.add(new_user)
db.session.commit()
except sqlalchemy.exc.IntegrityError:
return redirect(url_for('register.show') + '?error=user-or-email-exists')
return redirect(url_for('login.show') + '?success=account-created')
else:
return redirect(url_for('register.show') + '?error=missing-fields')
else:
return render_template('register.html')
Models.py:
from flask_login import UserMixin
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Users(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String)
item = db.Column(db.String(25))
amount = db.Column(db.Integer)
I'm still a beginner with flask so excuse me if it's real obvious, any assistance would be very welcome!
You didn't specify what sql database you are using with sqlAlchemy. Anyways, the id is not being generated so the new_user has no identity. that's why your getting this error. To solve the problem modify your model as follows:
class Users(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String)
item = db.Column(db.String(25))
amount = db.Column(db.Integer)
or you can use Sequence instead of autoincrement:
id = db.Column(db.Integer, primary_key=True, Sequence('user_seq'))
This is for the identity issue, but note that you need to specify user_loader for flask_login to work.
I would like to change a users password in db using db.session.commit()
I am getting the appropriate flash for form validation. But on next login, the db change does not go through / I cannot login with the newly created password. The old password is the one that needs to be used on the next login.
from Portfolio import db, login_manager
from Portfolio import bcrypt
from flask_login import UserMixin
#login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer(), primary_key=True)
username = db.Column(db.String(length=30), nullable=False, unique=True)
password_hash = db.Column(db.String(length=60), nullable=False)
#property
def password(self):
return self.password
#password.setter
def password(self, plain_text_password):
self.password_hash = bcrypt.generate_password_hash(plain_text_password)
def check_password_correction(self, attempted_password):
return bcrypt.check_password_hash(self.password_hash, attempted_password)
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'c133ce687016b5000d7b56cc81e0d974c9f1b0730836b4997765c34c7f417c56'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = "login_page"
login_manager.login_message_category = "info"
from Portfolio import routes
class ResetForm(FlaskForm):
def validate_reset(self, reset_to_check):
password = User.query.filter_by(password_hash=reset_to_check.data).first()
if password:
raise ValidationError('Please input a proper password')
resetpass = PasswordField(label='Reset Password',
validators=[Regexp('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{12,}$'),
Length(min=12), DataRequired()])
confreset = PasswordField(label='Confirm Changed Password:', validators=[EqualTo('resetpass'), DataRequired()])
newsubmit = SubmitField(label='Submit New Password')
#app.route('/reset', methods=['GET', 'POST'])
#login_required
def reset():
form = ResetForm()
if form.validate_on_submit():
user = User.username
reset_password = User(password=form.resetpass.data)
user.password = reset_password
db.session.commit()
logout_user()
flash('Password has been changed. Please login.')
return redirect(url_for('login_page'))
return render_template('reset.html', form=form, date=format_date, time=format_time)
Are you sure that validation step passed?
Here you compared the incoming data (probably not hashed) with hashed password in db.
password = User.query.filter_by(password_hash=reset_to_check.data).first()
if password:
raise ValidationError('Please input a proper password')
I assume that it will always be None, so no raise.
Second thing, you assign new User instance (why creating a new user?) to reset_password variable and afterwards assign this User instance under reset_password to user.reset_password atrribute. It is awkward and wrong for me. You should reset password for current_user :
from flask_login import current_user, logout_user
#app.route('/reset', methods=['GET', 'POST'])
#login_required
def reset():
form = ResetForm()
if form.validate_on_submit():
user = current_user
user.password = resetpass.data
db.session.commit()
logout_user()
flash('Password has been changed. Please login.')
return redirect(url_for('login_page'))
return render_template('reset.html', form=form, date=format_date, time=format_time)
I wanted to start programming and thus looked at some tutorials on YouTube.
I have followed this tutorial 1:1 but it still doesn't work and I am stuck, can someone maybe help me fix this?
from flask import Blueprint, render_template, request, flash
from flask_login import login_required, current_user
from .models import Vocab
from . import db
views = Blueprint('views', __name__)
#views.route('/', methods=['GET', 'POST'])
#login_required
def home():
if request.method == 'POST':
vocab = request.form['vocab']
if len(vocab) < 1:
flash('Vocabulary is too short!', category='error')
else:
# somehow can't find the problem, the vocab can't be saved in the database, data is apparently an invalid keyword
new_vocab = Vocab(data=vocab, user_id=current_user.id)
db.session.add(new_vocab)
db.session.commit()
flash('Vocabulary added successfully', category='success')
return render_template("home.html", user=current_user)
models.py :
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class Vocab(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
username = db.Column(db.String(150))
password = db.Column(db.String(100))
notes = db.relationship('Vocab')
I want to load database value? using sql alchmey.
If I ask using get mehtod '127.0.0.1/api/v1/user/25/'
There is a value in database(id=5, username=jaeyeon, email=jae#naver.com)
I want to show this column.
but it can't. Where can I fix the code?
I don't know how parameter deliver.. Please help me :(
from flask import Flask, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:12345#localhost/catchat'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
password = db.Column(db.String(120), unique=True)
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
def __repr__(self):
return '<User %r>' % self.username
class user(Resource):
def get(self, id):
args = parser.parse_args()
user_id = db.session.query(User.id == args['id']).filter(id)
print user_id
entry = [dict(id=user.id, username=user.username, email=user.email, password=user.password)]
print entry
return entry, 200
api.add_resource(user,'/api/v1/user/<int:id>')
if __name__ == '__main__':
app.run(debug=True)
You've set up your session a bit differently than I'm used to doing, so I'm not sure whether that makes any difference, but I can see some issues with the query you're making. Try this instead:
user_id = db.session.query(User).filter_by(id = args['id']).one()
Assuming that args['id'] is going to produce the proper value, then this will return the single matching row from User, and you can use dot notation to access the columns. I see you seem to be a bit off when using the dot notation, remember that the values are now stored in user_id so you'd have to access them like this:
email = user_id.email