Search code examples
pythonpython-datetimeflask-security

Flask-Security. Converting datetime(2014, 5, 31, 23, 46, 38, 559513) to python format datetime(2014-5-31, 23:46:38.559513)


I am using Flask-Security and everything works fine, except the email confirmation link. Clearly, when a user registers he recieves an email with the link to confirm the email, so when he/she clicks on the link my application gives an SQL datetime fromat error. The exact error is:

sqlalchemy.exc.StatementError
StatementError: SQLite DateTime type only accepts Python datetime and date objects as input. (original cause: 
TypeError: SQLite DateTime type only accepts Python datetime and date objects as input.) 
'UPDATE user SET confirmed_at=?, last_login_at=?, current_login_at=?, last_login_ip=?, current_login_ip=?,
login_count=? WHERE user.id = ?' [{u'user_id': 1, 'confirmed_at': datetime.datetime(2014, 5, 31, 23, 46, 38, 559513),
'login_count': 1, 'last_login_ip': '127.0.0.1', 'last_login_at': datetime.datetime(2014, 5, 31, 23, 46, 38, 557310),
'current_login_at': datetime.datetime(2014, 5, 31, 23, 46, 38, 557310), 'current_login_ip': '127.0.0.1'}]

Consequently, wanted to ask what should I do to overcome this error.

here is my models.py:

from app import db
from flask.ext.security import UserMixin, RoleMixin

#association table that stores users and their roles
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

#orm table that stores information on roles 
class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))


class User(db.Model, UserMixin):
    """a class that used to create ORM Database and 
    objects related to the class"""

    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(255), unique = True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    # Extra fields for Flask-Security
    name = db.Column(db.String(255))
    surname = db.Column(db.String(255))
    birth_date  = db.Column(db.Date)

    # SECURITY_CONFIRMABLE 
    confirmed_at = db.Column(db.DateTime())

    # SECURITY_TRACKABLE
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
    last_login_ip = db.Column(db.String(40))
    current_login_ip = db.Column(db.DateTime())
    login_count = db.Column(db.Integer(6))

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def set_password():
        pass

    def check_password():
        pass

    def __repr__(self):
        return '<User %r>' % (self.username)

Solution

  • Your current_login_ip column has the wrong type:

    current_login_ip = db.Column(db.DateTime())
    

    It expects a datetime and you are passing in a string containing an ip address ('127.0.0.1') instead. It should probably be a db.String() column, like last_login_ip:

    current_login_ip = db.Column(db.String(40))
    

    The rest of your input values are just fine; there is no such thing as a datetime(2014-5-31, 23:46:38.559513) format.