Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding DB sessions based on SQLAlchemy 1, changing how to declare Base Model classes, and other code modernization #5

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
38 changes: 38 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: check-json
- id: debug-statements
- id: mixed-line-ending
args: [--fix=lf]
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
args: [--profile=black]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.1
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/psf/black
rev: 24.2.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.12.0
hooks:
- id: pretty-format-yaml
args: [--autofix, --indent, '2']
32 changes: 0 additions & 32 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- Modified model and access code to work with SQLAlchemy version 2 (Issue #2)
- Updated packaging information files per PEP 517, PEP 518 (Issue #3)
- Restricted Python minimum working version to 3.7 or higher to align with SQLAlchemy 2 (Issue #2)
- Fixed csrfmiddlewaretoken argument error when calling disconnect method in view.py when disconnecting social connections (Issue #4)

## [1.0.0](https://github.com/python-social-auth/social-app-cherrypy/releases/tag/1.0.0) - 2017-01-22

### Added
Expand Down
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
build:
@ python setup.py sdist
@ python setup.py bdist_wheel --python-tag py2
@ BUILD_VERSION=3 python setup.py bdist_wheel --python-tag py3
@ python -m build

publish:
@ python setup.py sdist upload
@ python setup.py bdist_wheel --python-tag py2 upload
@ BUILD_VERSION=3 python setup.py bdist_wheel --python-tag py3 upload
@ twine upload dist/*

clean:
@ find . -name '*.py[co]' -delete
Expand Down
58 changes: 58 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = 'social-auth-app-cherrypy'
dynamic = ["version"]
dependencies = [
"six",
"social-auth-core>=1.0.0",
"social-auth-storage-sqlalchemy>=1.0.0",
]
authors = [
{name = "Matias Aguirre", email = "matiasaguirre@gmail.com"},
{name = "Lee Ji-ho", email = "search5@gmail.com"},
]
description = 'Python Social Authentication, CherryPy integration.'
license = {text = 'BSD'}
keywords = ["cherrypy", "sqlalchemy", "social auth"]
readme = "README.md"
classifiers=[
'Development Status :: 4 - Beta',
'Topic :: Internet',
'License :: OSI Approved :: BSD License',
'Intended Audience :: Developers',
'Environment :: Web Environment',
'Programming Language :: Python',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12'
]
requires-python = ">= 3.7"

[project.urls]
Repository = 'https://github.com/python-social-auth/social-app-cherrypy'
Documentation = 'http://python-social-auth.readthedocs.org'
Issues = 'https://github.com/python-social-auth/social-app-cherrypy/issues'
Changelog = 'https://github.com/python-social-auth/social-app-cherrypy/blob/master/CHANGELOG.md'

[options]
zip_safe = false

[tool.setuptools]
include-package-data = true

[tool.setuptools.packages]
find = {}

[tool.setuptools.dynamic]
version = {attr = "social_cherrypy.__version__"}

[tool.flake8]
max-line-length = 79
# Ignore some well known paths
exclude = ['.venv','.tox','dist','doc','build','*.egg','db/env.py','db/versions/*.py','site','Pipfile','Pipfile.lock']
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

36 changes: 0 additions & 36 deletions setup.py

This file was deleted.

2 changes: 1 addition & 1 deletion social_cherrypy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = "1.0.0"
51 changes: 29 additions & 22 deletions social_cherrypy/models.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
"""Flask SQLAlchemy ORM models for Social Auth"""
import cherrypy

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

from social_core.utils import setting_name, module_member
from social_sqlalchemy.storage import SQLAlchemyUserMixin, \
SQLAlchemyAssociationMixin, \
SQLAlchemyNonceMixin, \
SQLAlchemyPartialMixin, \
BaseSQLAlchemyStorage

import cherrypy
from social_core.utils import module_member, setting_name
from social_sqlalchemy.storage import (
BaseSQLAlchemyStorage,
SQLAlchemyAssociationMixin,
SQLAlchemyNonceMixin,
SQLAlchemyPartialMixin,
SQLAlchemyUserMixin,
)
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship


class SocialBase(DeclarativeBase):
pass

SocialBase = declarative_base()

DB_SESSION_ATTR = cherrypy.config.get(setting_name('DB_SESSION_ATTR'), 'db')
UID_LENGTH = cherrypy.config.get(setting_name('UID_LENGTH'), 255)
User = module_member(cherrypy.config[setting_name('USER_MODEL')])
DB_SESSION_ATTR = cherrypy.config.get(setting_name("DB_SESSION_ATTR"), "db")
UID_LENGTH = cherrypy.config.get(setting_name("UID_LENGTH"), 255)
User = module_member(cherrypy.config[setting_name("USER_MODEL")])


class CherryPySocialBase(object):
class CherryPySocialBase:
@classmethod
def _session(cls):
return getattr(cherrypy.request, DB_SESSION_ATTR)


class UserSocialAuth(CherryPySocialBase, SQLAlchemyUserMixin, SocialBase):
"""Social Auth association model"""
uid = Column(String(UID_LENGTH))
user_id = Column(Integer, ForeignKey(User.id),
nullable=False, index=True)
user = relationship(User, backref='social_auth')

uid: Mapped[str] = mapped_column(String(UID_LENGTH))
user_id: Mapped[int] = mapped_column(
ForeignKey(User.id), nullable=False, index=True
)
user: Mapped["User"] = relationship(User, backref="social_auth")

@classmethod
def username_max_length(cls):
return User.__table__.columns.get('username').type.length
return User.__table__.columns.get("username").type.length

@classmethod
def user_model(cls):
Expand All @@ -44,16 +48,19 @@ def user_model(cls):

class Nonce(CherryPySocialBase, SQLAlchemyNonceMixin, SocialBase):
"""One use numbers"""

pass


class Association(CherryPySocialBase, SQLAlchemyAssociationMixin, SocialBase):
"""OpenId account association"""

pass


class Partial(CherryPySocialBase, SQLAlchemyPartialMixin, SocialBase):
"""Partial pipeline storage"""

pass


Expand Down
17 changes: 8 additions & 9 deletions social_cherrypy/strategy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import six
import cherrypy

from social_core.strategy import BaseStrategy, BaseTemplateStrategy


Expand All @@ -25,7 +23,7 @@ def get_setting(self, name):
def request_data(self, merge=True):
if merge:
data = cherrypy.request.params
elif cherrypy.request.method == 'POST':
elif cherrypy.request.method == "POST":
data = cherrypy.body.params
else:
data = cherrypy.request.params
Expand All @@ -41,9 +39,9 @@ def html(self, content):
return content

def authenticate(self, backend, *args, **kwargs):
kwargs['strategy'] = self
kwargs['storage'] = self.storage
kwargs['backend'] = backend
kwargs["strategy"] = self
kwargs["storage"] = self.storage
kwargs["backend"] = backend
return backend.authenticate(*args, **kwargs)

def session_get(self, name, default=None):
Expand All @@ -59,8 +57,9 @@ def session_setdefault(self, name, value):
return cherrypy.session.setdefault(name, value)

def build_absolute_uri(self, path=None):
return cherrypy.url(path or '')
return cherrypy.url(path or "")

def is_response(self, value):
return isinstance(value, six.string_types) or \
isinstance(value, cherrypy.CherryPyException)
return isinstance(value, str) or isinstance( # fmt: skip
value, cherrypy.CherryPyException
)
Loading