fix(pej): use SessionLocal instead of db_session
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

The codebase uses SessionLocal() with try/finally pattern,
not db_session. Import error broke all blueprint registration.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-03-16 18:30:24 +01:00
parent f0358024e8
commit 39965f68ee
3 changed files with 189 additions and 165 deletions

View File

@ -8,7 +8,7 @@ from flask import Response
from flask_login import login_required, current_user
from . import bp
from database import db_session, ZOPKCompanyLink, Company
from database import SessionLocal, ZOPKCompanyLink, Company
from blueprints.pej_constants import get_nuclear_project_ids, LINK_TYPE_LABELS
@ -19,9 +19,11 @@ def pej_export_csv():
if not current_user.can_access_admin_panel():
return "Brak uprawnień", 403
nuclear_ids = get_nuclear_project_ids()
db = SessionLocal()
try:
nuclear_ids = get_nuclear_project_ids(db)
results = db_session.query(ZOPKCompanyLink, Company).join(
results = db.query(ZOPKCompanyLink, Company).join(
Company, ZOPKCompanyLink.company_id == Company.id
).filter(
ZOPKCompanyLink.project_id.in_(nuclear_ids),
@ -60,3 +62,5 @@ def pej_export_csv():
mimetype='text/csv; charset=utf-8',
headers={'Content-Disposition': f'attachment; filename="{filename}"'}
)
finally:
db.close()

View File

@ -1,6 +1,6 @@
"""Shared constants and helpers for PEJ section."""
from database import db_session, ZOPKProject
from database import SessionLocal, ZOPKProject
# Explicit slug list — easy to extend with SMR projects later
NUCLEAR_PROJECT_SLUGS = ['nuclear-plant']
@ -13,10 +13,18 @@ LINK_TYPE_LABELS = {
}
def get_nuclear_project_ids():
def get_nuclear_project_ids(db=None):
"""Return IDs of nuclear projects from ZOPK."""
projects = db_session.query(ZOPKProject.id).filter(
close = False
if db is None:
db = SessionLocal()
close = True
try:
projects = db.query(ZOPKProject.id).filter(
ZOPKProject.slug.in_(NUCLEAR_PROJECT_SLUGS),
ZOPKProject.project_type == 'energy'
).all()
return [p.id for p in projects]
finally:
if close:
db.close()

View File

@ -8,7 +8,7 @@ from sqlalchemy import func
from . import bp
from database import (
db_session, ZOPKNews, ZOPKMilestone,
SessionLocal, ZOPKNews, ZOPKMilestone,
ZOPKCompanyLink, Company, Announcement, Category
)
from blueprints.pej_constants import get_nuclear_project_ids, LINK_TYPE_LABELS
@ -18,38 +18,40 @@ from blueprints.pej_constants import get_nuclear_project_ids, LINK_TYPE_LABELS
@login_required
def pej_index():
"""PEJ landing page — hero, stats, news, timeline, top companies, announcements."""
nuclear_ids = get_nuclear_project_ids()
db = SessionLocal()
try:
nuclear_ids = get_nuclear_project_ids(db)
if not nuclear_ids:
abort(404)
# Stats
companies_count = db_session.query(func.count(ZOPKCompanyLink.id)).filter(
companies_count = db.query(func.count(ZOPKCompanyLink.id)).filter(
ZOPKCompanyLink.project_id.in_(nuclear_ids),
ZOPKCompanyLink.relevance_score >= 25
).scalar() or 0
news_count = db_session.query(func.count(ZOPKNews.id)).filter(
news_count = db.query(func.count(ZOPKNews.id)).filter(
ZOPKNews.project_id.in_(nuclear_ids),
ZOPKNews.status.in_(['approved', 'auto_approved'])
).scalar() or 0
milestones_count = db_session.query(func.count(ZOPKMilestone.id)).filter(
milestones_count = db.query(func.count(ZOPKMilestone.id)).filter(
ZOPKMilestone.category == 'nuclear'
).scalar() or 0
# Latest news (4)
news = db_session.query(ZOPKNews).filter(
news = db.query(ZOPKNews).filter(
ZOPKNews.project_id.in_(nuclear_ids),
ZOPKNews.status.in_(['approved', 'auto_approved'])
).order_by(ZOPKNews.published_at.desc()).limit(4).all()
# Nuclear milestones
milestones = db_session.query(ZOPKMilestone).filter(
milestones = db.query(ZOPKMilestone).filter(
ZOPKMilestone.category == 'nuclear'
).order_by(ZOPKMilestone.target_date.asc()).all()
# Top 6 companies by relevance
top_companies = db_session.query(ZOPKCompanyLink, Company).join(
top_companies = db.query(ZOPKCompanyLink, Company).join(
Company, ZOPKCompanyLink.company_id == Company.id
).filter(
ZOPKCompanyLink.project_id.in_(nuclear_ids),
@ -58,7 +60,7 @@ def pej_index():
).order_by(ZOPKCompanyLink.relevance_score.desc()).limit(6).all()
# PEJ announcements (status='published' in Announcement model)
announcements = db_session.query(Announcement).filter(
announcements = db.query(Announcement).filter(
Announcement.categories.contains(['pej']),
Announcement.status == 'published'
).order_by(Announcement.created_at.desc()).limit(3).all()
@ -73,13 +75,17 @@ def pej_index():
announcements=announcements,
link_type_labels=LINK_TYPE_LABELS
)
finally:
db.close()
@bp.route('/pej/local-content')
@login_required
def pej_local_content():
"""Full list of Norda companies matched to nuclear projects."""
nuclear_ids = get_nuclear_project_ids()
db = SessionLocal()
try:
nuclear_ids = get_nuclear_project_ids(db)
if not nuclear_ids:
abort(404)
@ -89,7 +95,7 @@ def pej_local_content():
link_type_filter = request.args.get('link_type', '')
search_query = request.args.get('q', '')
query = db_session.query(ZOPKCompanyLink, Company).join(
query = db.query(ZOPKCompanyLink, Company).join(
Company, ZOPKCompanyLink.company_id == Company.id
).filter(
ZOPKCompanyLink.project_id.in_(nuclear_ids),
@ -110,7 +116,7 @@ def pej_local_content():
).offset((page - 1) * per_page).limit(per_page).all()
# Get distinct categories for filter dropdown
category_ids = db_session.query(Company.category_id).join(
category_ids = db.query(Company.category_id).join(
ZOPKCompanyLink, Company.id == ZOPKCompanyLink.company_id
).filter(
ZOPKCompanyLink.project_id.in_(nuclear_ids),
@ -119,11 +125,11 @@ def pej_local_content():
Company.category_id.isnot(None)
).distinct().all()
category_ids = [c[0] for c in category_ids if c[0]]
categories = db_session.query(Category).filter(
categories = db.query(Category).filter(
Category.id.in_(category_ids)
).order_by(Category.name).all() if category_ids else []
link_types = db_session.query(ZOPKCompanyLink.link_type).filter(
link_types = db.query(ZOPKCompanyLink.link_type).filter(
ZOPKCompanyLink.project_id.in_(nuclear_ids),
ZOPKCompanyLink.relevance_score >= 25
).distinct().all()
@ -144,20 +150,24 @@ def pej_local_content():
link_type_filter=link_type_filter,
search_query=search_query
)
finally:
db.close()
@bp.route('/pej/aktualnosci')
@login_required
def pej_news():
"""Nuclear news list with pagination."""
nuclear_ids = get_nuclear_project_ids()
db = SessionLocal()
try:
nuclear_ids = get_nuclear_project_ids(db)
if not nuclear_ids:
abort(404)
page = request.args.get('page', 1, type=int)
per_page = 20
query = db_session.query(ZOPKNews).filter(
query = db.query(ZOPKNews).filter(
ZOPKNews.project_id.in_(nuclear_ids),
ZOPKNews.status.in_(['approved', 'auto_approved'])
).order_by(ZOPKNews.published_at.desc())
@ -173,3 +183,5 @@ def pej_news():
total=total,
total_pages=total_pages
)
finally:
db.close()