feat(pej): add admin CSV export route
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
971c5616c3
commit
4edab133d3
@ -21,6 +21,7 @@ from . import routes_zopk_dashboard # noqa: E402, F401
|
||||
from . import routes_zopk_news # noqa: E402, F401
|
||||
from . import routes_zopk_knowledge # noqa: E402, F401
|
||||
from . import routes_zopk_timeline # noqa: E402, F401
|
||||
from . import routes_pej # noqa: E402, F401
|
||||
from . import routes_users_api # noqa: E402, F401
|
||||
from . import routes_krs_api # noqa: E402, F401
|
||||
from . import routes_companies # noqa: E402, F401
|
||||
|
||||
62
blueprints/admin/routes_pej.py
Normal file
62
blueprints/admin/routes_pej.py
Normal file
@ -0,0 +1,62 @@
|
||||
"""PEJ admin routes — CSV export of companies for PEJ local content."""
|
||||
|
||||
import csv
|
||||
import io
|
||||
from datetime import date
|
||||
|
||||
from flask import Response
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from . import bp
|
||||
from database import db_session, ZOPKCompanyLink, Company
|
||||
from blueprints.pej_constants import get_nuclear_project_ids, LINK_TYPE_LABELS
|
||||
|
||||
|
||||
@bp.route('/pej/export')
|
||||
@login_required
|
||||
def pej_export_csv():
|
||||
"""Export PEJ-matched companies as CSV."""
|
||||
if not current_user.can_access_admin_panel():
|
||||
return "Brak uprawnień", 403
|
||||
|
||||
nuclear_ids = get_nuclear_project_ids()
|
||||
|
||||
results = db_session.query(ZOPKCompanyLink, Company).join(
|
||||
Company, ZOPKCompanyLink.company_id == Company.id
|
||||
).filter(
|
||||
ZOPKCompanyLink.project_id.in_(nuclear_ids),
|
||||
ZOPKCompanyLink.relevance_score >= 25,
|
||||
Company.status == 'active'
|
||||
).order_by(ZOPKCompanyLink.relevance_score.desc()).all()
|
||||
|
||||
output = io.StringIO()
|
||||
# UTF-8 BOM for Excel
|
||||
output.write('\ufeff')
|
||||
|
||||
writer = csv.writer(output, delimiter=';', quoting=csv.QUOTE_ALL)
|
||||
writer.writerow([
|
||||
'Nazwa firmy', 'Email', 'Telefon', 'Branża', 'PKD (główny)',
|
||||
'Usługi', 'Typ współpracy PEJ', 'Opis współpracy', 'Score', 'Miasto'
|
||||
])
|
||||
|
||||
for link, company in results:
|
||||
writer.writerow([
|
||||
company.name or '',
|
||||
company.email or '',
|
||||
company.phone or '',
|
||||
company.category.name if company.category else '',
|
||||
company.pkd_code or '',
|
||||
company.services_offered or '',
|
||||
LINK_TYPE_LABELS.get(link.link_type, link.link_type or ''),
|
||||
link.collaboration_description or '',
|
||||
link.relevance_score or 0,
|
||||
company.address_city or ''
|
||||
])
|
||||
|
||||
filename = f'pej-local-content-{date.today().isoformat()}.csv'
|
||||
|
||||
return Response(
|
||||
output.getvalue(),
|
||||
mimetype='text/csv; charset=utf-8',
|
||||
headers={'Content-Disposition': f'attachment; filename="{filename}"'}
|
||||
)
|
||||
Loading…
Reference in New Issue
Block a user