refactor: セッションの更新処理を共通化

This commit is contained in:
shimoda.m@nds-tyo.co.jp 2023-08-29 11:41:37 +09:00
parent 095f34b090
commit 2a71e7e454
6 changed files with 69 additions and 168 deletions

View File

@ -11,7 +11,6 @@ from src.model.view.bio_view_model import BioViewModel
from src.router.session_router import AuthenticatedRoute
from src.services.batch_status_service import BatchStatusService
from src.services.bio_view_service import BioViewService
from src.services.session_service import set_session
from src.system_var import constants
from src.templates import templates
@ -38,14 +37,9 @@ def bio_view(
logger.debug(f'UserId: {session.user_id}')
# 検索項目の取得
bio = bio_service.prepare_bio_view(session)
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'bioSearchList.html', {
'request': request,
@ -74,14 +68,9 @@ def search_bio(
bio: BioViewModel = bio_service.prepare_bio_view(session)
bio.bio_data = bio_sales_view_data
bio.form_data = bio_form
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'bioSearchList.html', {
'request': request,

View File

@ -3,7 +3,7 @@ from typing import Optional, Union
from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse
from src.depends.auth import verify_session
from src.depends.auth import get_current_session
from src.model.internal.session import UserSession
from src.model.view.logout_view_model import LogoutViewModel
from src.system_var import constants
@ -20,7 +20,7 @@ router = APIRouter()
def logout_view(
request: Request,
reason: Optional[str] = None,
session: Union[UserSession, None] = Depends(verify_session)
session: Union[UserSession, None] = Depends(get_current_session)
):
# どういうルートでログインしたかを判断するため、refererを取得
referer = request.headers.get('referer', '')

View File

@ -20,7 +20,6 @@ from src.model.view.table_override_view_model import TableOverrideViewModel
from src.router.session_router import AuthenticatedRoute
from src.services.batch_status_service import BatchStatusService
from src.services.master_mainte_service import MasterMainteService
from src.services.session_service import set_session
from src.system_var import constants
from src.templates import templates
@ -55,21 +54,16 @@ def menu_view(
# 画面表示用のモデル
menu = MasterMainteMenuViewModel()
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'masterMainteMenu.html',
{
'request': request,
'menu': menu
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -95,21 +89,16 @@ def inst_emp_csv_upload_view(
# 画面表示用のモデル
mainte_csv_up = InstEmpCsvUploadViewModel()
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instEmpCsvUL.html',
{
'request': request,
'mainte_csv_up': mainte_csv_up
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -157,21 +146,15 @@ async def inst_emp_csv_upload(
select_function=csv_upload_form.select_function,
select_table=csv_upload_form.select_table)
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instEmpCsvUL.html',
{
'request': request,
'mainte_csv_up': mainte_csv_up
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -199,21 +182,16 @@ def new_inst_result_view(
# 画面表示用のモデル
mainte_csv_up = master_mainte_service.prepare_mainte_new_inst_view(session.user_id, csv_upload_form)
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instEmpCsvUL.html',
{
'request': request,
'mainte_csv_up': mainte_csv_up
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -241,21 +219,16 @@ def inst_emp_csv_download_view(
mainte_csv_dl = InstEmpCsvDownloadViewModel(
is_search=False
)
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instEmpCsvDL.html',
{
'request': request,
'mainte_csv_dl': mainte_csv_dl
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -310,22 +283,15 @@ async def inst_emp_csv_download(
result_msg=result_msg
)
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instEmpCsvDL.html',
{
'request': request,
'mainte_csv_dl': mainte_csv_dl
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -351,21 +317,16 @@ def table_override_view(
# 画面表示用のモデル
table_override = TableOverrideViewModel()
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'tableOverride.html',
{
'request': request,
'table_override': table_override
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response
@ -393,20 +354,14 @@ def table_override_result_view(
# 画面表示用のモデル
table_override = master_mainte_service.copy_data_real_to_dummy()
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'tableOverride.html',
{
'request': request,
'table_override': table_override
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response

View File

@ -8,7 +8,6 @@ from src.model.view.menu_view_model import MenuViewModel
from src.model.view.user_view_model import UserViewModel
from src.router.session_router import AuthenticatedRoute
from src.services.batch_status_service import BatchStatusService
from src.services.session_service import set_session
from src.templates import templates
logger = get_logger('MeDaCA機能メニュー')
@ -43,20 +42,15 @@ def menu_view(
dump_status=dump_status,
user_model=user
)
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'menu.html',
{
'request': request,
'menu': menu
},
headers={'session_key': session.session_key}
headers={'session_key': session_key}
)
return templates_response

View File

@ -10,7 +10,6 @@ from src.model.request.ultmarc_inst import (UltmarcInstInfoModel,
UltmarcInstSearchModel)
from src.router.session_router import AuthenticatedRoute
from src.services.batch_status_service import BatchStatusService
from src.services.session_service import set_session
from src.services.ultmarc_view_service import UltmarcViewService
from src.templates import templates
@ -40,14 +39,8 @@ def ultmarc_inst_view(
ultmarc = ultmarc_service.prepare_ultmarc_inst_search_view()
ultmarc.is_batch_processing = is_batch_processing
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instSearch.html', {
'request': request,
@ -80,14 +73,8 @@ def search_inst(
# 画面表示用にエスケープを解除して返す
ultmarc.form_data = ultmarc_inst_form.unescape()
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instSearch.html', {
'request': request,
@ -124,14 +111,8 @@ def ultmarc_inst_info_view(
# ページ数表示するページNo
ultmarc.page_num = 0
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instInfo.html', {
'request': request,
@ -167,14 +148,8 @@ def ultmarc_inst_info_search(
# ページ数表示するページNo
ultmarc.page_num = ultmarc_inst_form.page_num
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'instInfo.html', {
'request': request,
@ -203,14 +178,8 @@ def ultmarc_doctor_view(
ultmarc = ultmarc_service.prepare_ultmarc_doctor_search_view()
ultmarc.is_batch_processing = is_batch_processing
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'docSearch.html', {
'request': request,
@ -243,14 +212,8 @@ def search_doc(
# 画面表示用にエスケープを解除して返す
ultmarc.form_data = ultmarc_doctor_form.unescape()
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'docSearch.html', {
'request': request,
@ -287,14 +250,8 @@ def ultmarc_doctor_info_view(
# ページ数表示するページNo
ultmarc.page_num = 0
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'docInfo.html', {
'request': request,
@ -329,14 +286,8 @@ def ultmarc_doctor_info_search(
# ページ数表示するページNo
ultmarc.page_num = ultmarc_doctor_form.page_num
# セッション書き換え
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
session_key = set_session(session)
# レスポンス
session_key = session.session_key
templates_response = templates.TemplateResponse(
'docInfo.html', {
'request': request,

View File

@ -10,6 +10,8 @@ from src.depends.auth import (check_session_expired, get_current_session,
verify_session)
from src.error.exceptions import DBException, UnexpectedException
from src.logging.get_logger import get_logger
from src.model.internal.session import UserSession
from src.services.session_service import set_session
from src.system_var import constants, environment
logger = get_logger('medaca_router')
@ -124,6 +126,16 @@ class AfterSetCookieSessionRoute(PrepareDatabaseRoute):
return response
del response.headers['session_key']
# セッションを更新
session = get_current_session(session_key)
session.update(
actions=[
UserSession.last_access_time.set(UserSession.new_last_access_time()),
UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
]
)
set_session(session)
# クッキーにセッションを設定
response.set_cookie(
key='session',