Fix: CSV UI with user name (products page)
Browse files- templates/products.html +6 -0
- web/products.py +22 -9
- web/routes.py +26 -12
templates/products.html
CHANGED
|
@@ -63,6 +63,9 @@
|
|
| 63 |
<tr>
|
| 64 |
<th>Product</th>
|
| 65 |
<th>Version</th>
|
|
|
|
|
|
|
|
|
|
| 66 |
<th class="text-center">Attestations</th>
|
| 67 |
<th class="text-center">Policy Results</th>
|
| 68 |
<th class="text-center">Badges</th>
|
|
@@ -74,6 +77,9 @@
|
|
| 74 |
<tr>
|
| 75 |
<td>{{ p.product }}</td>
|
| 76 |
<td>{{ p.version }}</td>
|
|
|
|
|
|
|
|
|
|
| 77 |
<td class="text-center">{{ p.attestation_count or 0 }}</td>
|
| 78 |
<td class="text-center">{{ p.policy_result_count or 0 }}</td>
|
| 79 |
<td class="text-center">
|
|
|
|
| 63 |
<tr>
|
| 64 |
<th>Product</th>
|
| 65 |
<th>Version</th>
|
| 66 |
+
{% if is_admin %}
|
| 67 |
+
<th>User</th>
|
| 68 |
+
{% endif %}
|
| 69 |
<th class="text-center">Attestations</th>
|
| 70 |
<th class="text-center">Policy Results</th>
|
| 71 |
<th class="text-center">Badges</th>
|
|
|
|
| 77 |
<tr>
|
| 78 |
<td>{{ p.product }}</td>
|
| 79 |
<td>{{ p.version }}</td>
|
| 80 |
+
{% if is_admin %}
|
| 81 |
+
<td>{{ p.username or '' }}</td>
|
| 82 |
+
{% endif %}
|
| 83 |
<td class="text-center">{{ p.attestation_count or 0 }}</td>
|
| 84 |
<td class="text-center">{{ p.policy_result_count or 0 }}</td>
|
| 85 |
<td class="text-center">
|
web/products.py
CHANGED
|
@@ -2,10 +2,16 @@ from flask import Blueprint, render_template, request, redirect, url_for, sessio
|
|
| 2 |
from models import db, Attestation, PolicyResult, Product
|
| 3 |
from sqlalchemy import func, text
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Only consume data from products table
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
query = query.filter(Product.user_id == user_id)
|
| 10 |
if filter_query:
|
| 11 |
like_pattern = f'%{filter_query}%'
|
|
@@ -15,12 +21,18 @@ def get_products_with_stats(filter_query=None, version_filter=None, user_id=None
|
|
| 15 |
if version_filter:
|
| 16 |
version_like = f'%{version_filter}%'
|
| 17 |
query = query.filter(Product.product_version.ilike(version_like))
|
| 18 |
-
# Paging support
|
| 19 |
-
page = request.args.get('page', 1, type=int)
|
| 20 |
-
per_page = request.args.get('per_page', 20, type=int)
|
| 21 |
-
pagination = query.order_by(Product.product_key).paginate(page=page, per_page=per_page, error_out=False)
|
| 22 |
products = []
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
badges = []
|
| 25 |
if product.badges:
|
| 26 |
for badge_name, badge_data in product.badges.items():
|
|
@@ -52,7 +64,8 @@ def get_products_with_stats(filter_query=None, version_filter=None, user_id=None
|
|
| 52 |
'policy_result_count': policy_result_count,
|
| 53 |
'badges': badges,
|
| 54 |
'product_metadata': product.product_metadata,
|
| 55 |
-
'user_id': product.user_id
|
|
|
|
| 56 |
})
|
| 57 |
return products, pagination
|
| 58 |
|
|
|
|
| 2 |
from models import db, Attestation, PolicyResult, Product
|
| 3 |
from sqlalchemy import func, text
|
| 4 |
|
| 5 |
+
from sqlalchemy.orm import joinedload
|
| 6 |
+
|
| 7 |
+
from sqlalchemy.orm import joinedload
|
| 8 |
+
|
| 9 |
+
def get_products_with_stats(filter_query=None, version_filter=None, user_id=None, show_all=False, badge_filter=None, paginate=True):
|
| 10 |
# Only consume data from products table
|
| 11 |
+
from models import User
|
| 12 |
+
# Eagerly load user relationship to ensure username is always available
|
| 13 |
+
query = Product.query.options(joinedload(Product.user)).filter(Product.deleted == False)
|
| 14 |
+
if user_id is not None and not show_all:
|
| 15 |
query = query.filter(Product.user_id == user_id)
|
| 16 |
if filter_query:
|
| 17 |
like_pattern = f'%{filter_query}%'
|
|
|
|
| 21 |
if version_filter:
|
| 22 |
version_like = f'%{version_filter}%'
|
| 23 |
query = query.filter(Product.product_version.ilike(version_like))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
products = []
|
| 25 |
+
if paginate:
|
| 26 |
+
# Paging support
|
| 27 |
+
page = request.args.get('page', 1, type=int)
|
| 28 |
+
per_page = request.args.get('per_page', 20, type=int)
|
| 29 |
+
pagination = query.order_by(Product.product_key).paginate(page=page, per_page=per_page, error_out=False)
|
| 30 |
+
items = pagination.items
|
| 31 |
+
else:
|
| 32 |
+
items = query.order_by(Product.product_key).all()
|
| 33 |
+
pagination = None
|
| 34 |
+
for product in items:
|
| 35 |
+
username = product.user.username if product.user else '-'
|
| 36 |
badges = []
|
| 37 |
if product.badges:
|
| 38 |
for badge_name, badge_data in product.badges.items():
|
|
|
|
| 64 |
'policy_result_count': policy_result_count,
|
| 65 |
'badges': badges,
|
| 66 |
'product_metadata': product.product_metadata,
|
| 67 |
+
'user_id': product.user_id,
|
| 68 |
+
'username': username
|
| 69 |
})
|
| 70 |
return products, pagination
|
| 71 |
|
web/routes.py
CHANGED
|
@@ -230,12 +230,11 @@ def products_page():
|
|
| 230 |
per_page = request.args.get('per_page', 20, type=int)
|
| 231 |
user_id = session.get('user_id')
|
| 232 |
show_all = False
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
show_all = request.args.get('show_all', '0') == '1'
|
| 239 |
from models import Badge
|
| 240 |
# Show all visible badges for user or global
|
| 241 |
badge_query = Badge.query.filter(
|
|
@@ -258,36 +257,51 @@ def download_products_csv():
|
|
| 258 |
per_page = request.args.get('per_page', 20, type=int)
|
| 259 |
custom_filename = request.args.get('filename', '')
|
| 260 |
user_id = session.get('user_id')
|
|
|
|
| 261 |
show_all = request.args.get('show_all') and session.get('is_admin', False)
|
| 262 |
|
|
|
|
| 263 |
products, pagination = get_products_with_stats(
|
| 264 |
-
filter_query=filter_query,
|
| 265 |
version_filter=version_filter,
|
| 266 |
user_id=user_id,
|
| 267 |
show_all=show_all,
|
| 268 |
-
badge_filter=badge_filter
|
|
|
|
| 269 |
)
|
| 270 |
from models import Badge
|
| 271 |
all_badges = Badge.query.order_by(Badge.name).all()
|
| 272 |
output = io.StringIO()
|
| 273 |
writer = csv.writer(output)
|
| 274 |
# CSV header
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
for p in products:
|
| 279 |
# Only include badges relevant to this product
|
| 280 |
if p['badges']:
|
| 281 |
for b in p['badges']:
|
| 282 |
-
|
| 283 |
p['product'],
|
| 284 |
p['version'],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
p.get('attestation_count', 0),
|
| 286 |
p.get('policy_result_count', 0),
|
| 287 |
b['name'],
|
| 288 |
b.get('count', ''),
|
| 289 |
b.get('visible', True)
|
| 290 |
])
|
|
|
|
| 291 |
output.seek(0)
|
| 292 |
|
| 293 |
# Use custom filename if provided, otherwise generate a default filename
|
|
|
|
| 230 |
per_page = request.args.get('per_page', 20, type=int)
|
| 231 |
user_id = session.get('user_id')
|
| 232 |
show_all = False
|
| 233 |
+
# Use canonical admin check, not session variable
|
| 234 |
+
from .routes import is_admin_user_web as _is_admin_user_web
|
| 235 |
+
is_admin = _is_admin_user_web(user_id) if user_id else False
|
| 236 |
+
if is_admin:
|
| 237 |
+
show_all = request.args.get('show_all', '0') == '1'
|
|
|
|
| 238 |
from models import Badge
|
| 239 |
# Show all visible badges for user or global
|
| 240 |
badge_query = Badge.query.filter(
|
|
|
|
| 257 |
per_page = request.args.get('per_page', 20, type=int)
|
| 258 |
custom_filename = request.args.get('filename', '')
|
| 259 |
user_id = session.get('user_id')
|
| 260 |
+
is_admin = is_admin_user_web(user_id) if user_id else False
|
| 261 |
show_all = request.args.get('show_all') and session.get('is_admin', False)
|
| 262 |
|
| 263 |
+
# For CSV export, get all products (no pagination)
|
| 264 |
products, pagination = get_products_with_stats(
|
| 265 |
+
filter_query=filter_query,
|
| 266 |
version_filter=version_filter,
|
| 267 |
user_id=user_id,
|
| 268 |
show_all=show_all,
|
| 269 |
+
badge_filter=badge_filter,
|
| 270 |
+
paginate=False
|
| 271 |
)
|
| 272 |
from models import Badge
|
| 273 |
all_badges = Badge.query.order_by(Badge.name).all()
|
| 274 |
output = io.StringIO()
|
| 275 |
writer = csv.writer(output)
|
| 276 |
# CSV header
|
| 277 |
+
# Add User column for admin when show_all is enabled
|
| 278 |
+
if is_admin:
|
| 279 |
+
writer.writerow([
|
| 280 |
+
'Product', 'Version', 'User', 'Attestation Count', 'Policy Result Count', 'Badge Name', 'Badge Value', 'Visible'
|
| 281 |
+
])
|
| 282 |
+
else:
|
| 283 |
+
writer.writerow([
|
| 284 |
+
'Product', 'Version', 'Attestation Count', 'Policy Result Count', 'Badge Name', 'Badge Value', 'Visible'
|
| 285 |
+
])
|
| 286 |
for p in products:
|
| 287 |
# Only include badges relevant to this product
|
| 288 |
if p['badges']:
|
| 289 |
for b in p['badges']:
|
| 290 |
+
row = [
|
| 291 |
p['product'],
|
| 292 |
p['version'],
|
| 293 |
+
]
|
| 294 |
+
if is_admin:
|
| 295 |
+
# Use '-' if username is missing for clarity
|
| 296 |
+
row.append(p.get('username') or '-')
|
| 297 |
+
row.extend([
|
| 298 |
p.get('attestation_count', 0),
|
| 299 |
p.get('policy_result_count', 0),
|
| 300 |
b['name'],
|
| 301 |
b.get('count', ''),
|
| 302 |
b.get('visible', True)
|
| 303 |
])
|
| 304 |
+
writer.writerow(row)
|
| 305 |
output.seek(0)
|
| 306 |
|
| 307 |
# Use custom filename if provided, otherwise generate a default filename
|