Skip to content

Commit

Permalink
fix db_session
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerSpace committed Sep 19, 2020
1 parent fdeb625 commit 29f7da4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from seatable_thumbnail import DBSession
from seatable_thumbnail.serializers import ThumbnailSerializer
from seatable_thumbnail.permissions import ThumbnailPermission
from seatable_thumbnail.thumbnail import Thumbnail
Expand Down Expand Up @@ -34,12 +35,14 @@ async def __call__(self, scope, receive, send):

# ===== thumbnail =====
elif 'thumbnail/' == request.url[:10]:
db_session = DBSession()
# serializer
try:
serializer = ThumbnailSerializer(request)
serializer = ThumbnailSerializer(db_session, request)
thumbnail_info = serializer.thumbnail_info
except Exception as e:
logger.exception(e)
db_session.close()
response_start, response_body = gen_error_response(
400, 'Bad request.')
await send(response_start)
Expand All @@ -48,21 +51,25 @@ async def __call__(self, scope, receive, send):

# permission
try:
permission = ThumbnailPermission(**thumbnail_info)
permission = ThumbnailPermission(db_session, **thumbnail_info)
if not permission.check():
db_session.close()
response_start, response_body = gen_error_response(
403, 'Forbidden.')
await send(response_start)
await send(response_body)
return
except Exception as e:
logger.exception(e)
db_session.close()
response_start, response_body = gen_error_response(
500, 'Internal server error.')
await send(response_start)
await send(response_body)
return

db_session.close()

# cache
try:
etag = thumbnail_info.get('etag')
Expand Down
1 change: 0 additions & 1 deletion seatable_thumbnail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@
engine = create_engine(db_url, **db_kwargs)
Base = declarative_base()
DBSession = sessionmaker(bind=engine)
db_session = DBSession()
14 changes: 7 additions & 7 deletions seatable_thumbnail/permissions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from seaserv import ccnet_api
from seatable_thumbnail import db_session
from seatable_thumbnail.models import DTables, DTableShare, \
DTableGroupShare, DTableViewUserShare, DTableViewGroupShare, \
DTableExternalLinks
from seatable_thumbnail.constants import PERMISSION_READ, PERMISSION_READ_WRITE


class ThumbnailPermission(object):
def __init__(self, **info):
def __init__(self, db_session, **info):
self.db_session = db_session
self.__dict__.update(info)
self.dtable = db_session.query(
self.dtable = self.db_session.query(
DTables).filter_by(uuid=self.dtable_uuid).first()

def check(self):
Expand Down Expand Up @@ -54,7 +54,7 @@ def check_dtable_permission(self):
return PERMISSION_READ_WRITE

if dtable: # check user's all permissions from `share`, `group-share` and checkout higher one
dtable_share = db_session.query(
dtable_share = self.db_session.query(
DTableShare).filter_by(dtable_id=dtable.id, to_user=username).first()
if dtable_share and dtable_share.permission == PERMISSION_READ_WRITE:
return dtable_share.permission
Expand All @@ -65,7 +65,7 @@ def check_dtable_permission(self):
else:
groups = ccnet_api.get_groups(username, return_ancestors=True)
group_ids = [group.id for group in groups]
group_permissions = db_session.query(
group_permissions = self.db_session.query(
DTableGroupShare.permission).filter(DTableGroupShare.dtable_id == dtable.id, DTableGroupShare.group_id.in_(group_ids)).all()

for group_permission in group_permissions:
Expand All @@ -91,7 +91,7 @@ def get_user_view_share_permission(self):
username = self.username
dtable = self.dtable

view_share = db_session.query(
view_share = self.db_session.query(
DTableViewUserShare).filter_by(dtable_id=dtable.id, to_user=username).order_by(DTableViewUserShare.permission.desc()).first()
if not view_share:
return ''
Expand All @@ -103,7 +103,7 @@ def get_group_view_share_permission(self):
username = self.username
dtable = self.dtable

view_shares = db_session.query(
view_shares = self.db_session.query(
DTableViewGroupShare).filter_by(dtable_id=dtable.id).order_by(DTableViewGroupShare.permission.desc()).all()

target_view_share = None
Expand Down
9 changes: 4 additions & 5 deletions seatable_thumbnail/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
from email.utils import formatdate

from seaserv import seafile_api
from seatable_thumbnail import db_session
import seatable_thumbnail.settings as settings
from seatable_thumbnail.constants import FILE_EXT_TYPE_MAP, \
IMAGE, PSD, VIDEO, XMIND
from seatable_thumbnail.models import Workspaces, DjangoSession


class ThumbnailSerializer(object):
def __init__(self, request):
def __init__(self, db_session, request):
self.db_session = db_session
self.request = request
self.check()
self.gen_thumbnail_info()

def check(self):
self.params_check()
db_session.commit() # clear db session cache
self.session_check()
self.resource_check()
self.gen_thumbnail_info()
Expand All @@ -40,7 +39,7 @@ def parse_django_session(self, session_data):

def session_check(self):
session_key = self.request.cookies[settings.SESSION_KEY]
django_session = db_session.query(
django_session = self.db_session.query(
DjangoSession).filter_by(session_key=session_key).first()
self.session_data = self.parse_django_session(django_session.session_data)

Expand Down Expand Up @@ -102,7 +101,7 @@ def resource_check(self):
file_path = self.params['file_path']
size = self.params['size']

workspace = db_session.query(
workspace = self.db_session.query(
Workspaces).filter_by(id=workspace_id).first()
repo_id = workspace.repo_id
workspace_owner = workspace.owner
Expand Down

0 comments on commit 29f7da4

Please sign in to comment.