/social/apps/tornado_app/models.py

https://github.com/priyaank/python-social-auth · Python · 85 lines · 67 code · 13 blank · 5 comment · 0 complexity · 75a7c9208306417ec560f27ae9d06bde MD5 · raw file

  1. """Tornado SQLAlchemy ORM models for Social Auth"""
  2. from sqlalchemy import Column, Integer, String, ForeignKey
  3. from sqlalchemy.orm import relationship, backref
  4. from sqlalchemy.schema import UniqueConstraint
  5. from social.utils import setting_name, module_member
  6. from social.storage.sqlalchemy_orm import SQLAlchemyUserMixin, \
  7. SQLAlchemyAssociationMixin, \
  8. SQLAlchemyNonceMixin, \
  9. SQLAlchemyCodeMixin, \
  10. BaseSQLAlchemyStorage
  11. from social.apps.tornado_app.fields import JSONType
  12. class TornadoStorage(BaseSQLAlchemyStorage):
  13. user = None
  14. nonce = None
  15. association = None
  16. code = None
  17. def init_social(Base, session, settings):
  18. UID_LENGTH = settings.get(setting_name('UID_LENGTH'), 255)
  19. User = module_member(settings[setting_name('USER_MODEL')])
  20. app_session = session
  21. class _AppSession(object):
  22. @classmethod
  23. def _session(cls):
  24. return app_session
  25. class UserSocialAuth(_AppSession, Base, SQLAlchemyUserMixin):
  26. """Social Auth association model"""
  27. __tablename__ = 'social_auth_usersocialauth'
  28. __table_args__ = (UniqueConstraint('provider', 'uid'),)
  29. id = Column(Integer, primary_key=True)
  30. provider = Column(String(32))
  31. uid = Column(String(UID_LENGTH))
  32. extra_data = Column(JSONType)
  33. user_id = Column(Integer, ForeignKey(User.id),
  34. nullable=False, index=True)
  35. user = relationship(User, backref=backref('social_auth',
  36. lazy='dynamic'))
  37. @classmethod
  38. def username_max_length(cls):
  39. return User.__table__.columns.get('username').type.length
  40. @classmethod
  41. def user_model(cls):
  42. return User
  43. class Nonce(_AppSession, Base, SQLAlchemyNonceMixin):
  44. """One use numbers"""
  45. __tablename__ = 'social_auth_nonce'
  46. __table_args__ = (UniqueConstraint('server_url', 'timestamp', 'salt'),)
  47. id = Column(Integer, primary_key=True)
  48. server_url = Column(String(255))
  49. timestamp = Column(Integer)
  50. salt = Column(String(40))
  51. class Association(_AppSession, Base, SQLAlchemyAssociationMixin):
  52. """OpenId account association"""
  53. __tablename__ = 'social_auth_association'
  54. __table_args__ = (UniqueConstraint('server_url', 'handle'),)
  55. id = Column(Integer, primary_key=True)
  56. server_url = Column(String(255))
  57. handle = Column(String(255))
  58. secret = Column(String(255)) # base64 encoded
  59. issued = Column(Integer)
  60. lifetime = Column(Integer)
  61. assoc_type = Column(String(64))
  62. class Code(_AppSession, Base, SQLAlchemyCodeMixin):
  63. __tablename__ = 'social_auth_code'
  64. __table_args__ = (UniqueConstraint('code', 'email'),)
  65. id = Column(Integer, primary_key=True)
  66. email = Column(String(200))
  67. code = Column(String(32), index=True)
  68. # Set the references in the storage class
  69. TornadoStorage.user = UserSocialAuth
  70. TornadoStorage.nonce = Nonce
  71. TornadoStorage.association = Association
  72. TornadoStorage.code = Code