Skip to content

Commit dd1f247

Browse files
committed
fixed up session and query handling in basemodels.py
1 parent aa9330a commit dd1f247

File tree

9 files changed

+43
-84
lines changed

9 files changed

+43
-84
lines changed

flask_xxl/apps/auth/models.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
from basemodels import BaseMixin
22
from flask import url_for
3-
from ext import db
43
from LoginUtils import encrypt_password, check_password
4+
from sqlalchemy import Column,String,Integer,Boolean,Date,DateTime,ForeignKey,UnicodeText,Table
5+
from sqlalchemy.orm import relationship,backref
56

6-
#import sqlalchemy to global namespace
7-
for attr in dir(db):
8-
if not attr.startswith('_'):
9-
globals()[attr] = getattr(db,attr)
107

8+
#import sqlalchemy to global namespace
119

1210
class UnknownUser(object):
1311
is_unknown = True
1412

15-
class Role(BaseMixin,Model):
13+
class Role(BaseMixin):
1614
__tablename__ = 'roles'
1715

1816
name = Column(String(255))
@@ -21,7 +19,7 @@ class Role(BaseMixin,Model):
2119
can_edit = Column(Boolean,default=False,nullable=False)
2220
can_delete = Column(Boolean,default=False,nullable=False)
2321

24-
class User(BaseMixin,Model):
22+
class User(BaseMixin):
2523
__tablename__ = 'users'
2624

2725
first_name = Column(String(255),default="")
@@ -32,8 +30,6 @@ class User(BaseMixin,Model):
3230
'users',lazy='dynamic'))
3331
add_date = Column(DateTime,default=func.now())
3432
_pw_hash = Column(UnicodeText,nullable=False)
35-
articles = relationship('Article',backref=backref(
36-
'author'),lazy='dynamic',passive_deletes='all')
3733
age = Column(Integer)
3834

3935

@@ -90,7 +86,3 @@ def _get_absolute_url(self):
9086
def _get_edit_url(self):
9187
return '#'
9288

93-
@property
94-
def article_count(self):
95-
return self.articles.query.count()
96-

flask_xxl/basemodels.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,42 @@
88
from flask import current_app
99
from inflection import underscore, pluralize
1010
from sqlalchemy.ext.declarative import as_declarative, declarative_base, declared_attr
11+
from sqlalchemy.ext.declarative.api import DeclarativeMeta
1112
from sqlalchemy.orm import scoped_session, sessionmaker
12-
from flask.ext.sqlalchemy import SQLAlchemy, _BoundDeclarativeMeta, _QueryProperty
13-
import sqlalchemy as db
13+
from flask.ext.sqlalchemy import SQLAlchemy, _BoundDeclarativeMeta
14+
from sqlalchemy import UniqueConstraint,Column,Integer,Text,String,Date,DateTime,ForeignKey,func,create_engine
15+
16+
get_engine = lambda: create_engine(current_app.config['SQLALCHEMY_DATABASE_URI'])
17+
Session = lambda: scoped_session(sessionmaker(bind=get_engine()))
1418

1519
Model = declarative_base()
1620

17-
def ensure_class(f):
18-
@wraps(f)
19-
def wrapper(*args,**kwargs):
20-
if type(args[0]) != DeclarativeMeta:
21-
args[0] = args[0].__class__
22-
return f(*args,**kwargs)
23-
return wrapper
2421

2522
class SQLAlchemyMissingException(Exception):
2623
pass
2724

2825
class ModelDeclarativeMeta(_BoundDeclarativeMeta):
2926
pass
3027

31-
@as_declarative(name='Model',metaclass=ModelDeclarativeMeta)
28+
@as_declarative(name='BaseMixin',metaclass=ModelDeclarativeMeta)
3229
class BaseMixin(Model):
33-
_engine = None
3430
__abstract__ = True
31+
_session = None
32+
33+
34+
@property
35+
def _engine(self):
36+
return get_engine()
37+
38+
@declared_attr
39+
def id(self):
40+
return Column(Integer,primary_key=True)
41+
42+
@classmethod
43+
def get_session(cls):
44+
if cls._session is None:
45+
cls._session = Session()
46+
return cls._session
3547

3648
@staticmethod
3749
def make_table_name(name):
@@ -40,16 +52,7 @@ def make_table_name(name):
4052
@declared_attr
4153
def __tablename__(self):
4254
return BaseMixin.make_table_name(self.__name__)
43-
44-
@classmethod
45-
@ensure_class
46-
def query(cls,*args,**kwargs):
47-
return
48-
49-
@declared_attr
50-
def id(self):
51-
return db.Column(db.Integer,db.Sequence('user_id_seq'),primary_key=True)
52-
55+
5356
@classmethod
5457
def get_by_id(cls, id):
5558
if any(
@@ -60,23 +63,8 @@ def get_by_id(cls, id):
6063
return None
6164

6265
@classmethod
63-
@ensure_class
6466
def get_all(cls):
65-
return cls.query().all()
66-
67-
@classmethod
68-
@ensure_class
69-
def query(cls):
70-
return cls.session.query(cls)
71-
72-
@property
73-
def session(self):
74-
factory = sessionmaker(bind=self.engine)
75-
return scoped_session(factory)
76-
77-
@property
78-
def engine(self):
79-
return self._engine or db.engine
67+
return cls.query.all()
8068

8169
@classmethod
8270
def create(cls, **kwargs):
@@ -101,6 +89,10 @@ def delete(self, commit=True):
10189
self.session.delete(self)
10290
return commit and self.session.commit()
10391

92+
@classmethod
93+
def query(cls):
94+
return cls.get_session().query(cls)
95+
10496
@property
10597
def absolute_url(self):
10698
return self._get_absolute_url()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from flask import Blueprint
2+
3+
{{{ blueprint.name }}} = Blueprint('{{{ blueprint.name }}}',__name__,
4+
template_folder='templates/{{{ blueprint.name }}}',
5+
url_prefix='{{{ blueprint.name }}}')
6+
7+
from .views import *
8+
from .models import *

flask_xxl/templates/project/+project.name+/+project.name+/__init__.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

flask_xxl/templates/project/+project.name+/+project.name+/models.py

Whitespace-only changes.

flask_xxl/templates/project/+project.name+/+project.name+/views.py

Whitespace-only changes.

flask_xxl/templates/project/+project.name+/context_processors.py.bob

Lines changed: 0 additions & 17 deletions
This file was deleted.

flask_xxl/templates/project/+project.name+/urls.py.bob

Lines changed: 0 additions & 7 deletions
This file was deleted.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = '0,6,9'
1+
VERSION = '0,7,0'
22
import os
33
from setuptools import setup, find_packages,findall
44
from glob import glob

0 commit comments

Comments
 (0)