Kgshop commited on
Commit
a581378
·
verified ·
1 Parent(s): 0d76618

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -1652,7 +1652,8 @@ def catalog():
1652
  categories = data.get('categories', [])
1653
 
1654
  products_in_stock = [p for p in all_products if p.get('in_stock', True)]
1655
- products_sorted = sorted(products_in_stock, key=lambda p: (not p.get('is_top', False), p.get('name', '').lower()))
 
1656
 
1657
  return render_template_string(
1658
  CATALOG_TEMPLATE,
@@ -1668,7 +1669,8 @@ def product_detail(index):
1668
  data = load_data()
1669
  all_products = data.get('products', [])
1670
  products_in_stock = [p for p in all_products if p.get('in_stock', True)]
1671
- products_sorted = sorted(products_in_stock, key=lambda p: (not p.get('is_top', False), p.get('name', '').lower()))
 
1672
 
1673
  try:
1674
  product = products_sorted[index]
@@ -1778,8 +1780,11 @@ def admin():
1778
  if 'id' not in product:
1779
  product['id'] = str(uuid.uuid4())
1780
  needs_save = True
 
 
 
1781
  if needs_save:
1782
- logging.info("Assigning unique IDs to products that were missing them.")
1783
  data['products'] = products
1784
  save_data(data)
1785
 
@@ -1966,6 +1971,7 @@ def admin():
1966
 
1967
  new_product = {
1968
  'id': str(uuid.uuid4()),
 
1969
  'name': name, 'price': price, 'description': description,
1970
  'category': category if any(c['name'] == category for c in categories) else 'Без категории',
1971
  'subcategory': subcategory,
 
1652
  categories = data.get('categories', [])
1653
 
1654
  products_in_stock = [p for p in all_products if p.get('in_stock', True)]
1655
+ products_by_date = sorted(products_in_stock, key=lambda p: p.get('created_at', '1970-01-01 00:00:00'), reverse=True)
1656
+ products_sorted = sorted(products_by_date, key=lambda p: not p.get('is_top', False))
1657
 
1658
  return render_template_string(
1659
  CATALOG_TEMPLATE,
 
1669
  data = load_data()
1670
  all_products = data.get('products', [])
1671
  products_in_stock = [p for p in all_products if p.get('in_stock', True)]
1672
+ products_by_date = sorted(products_in_stock, key=lambda p: p.get('created_at', '1970-01-01 00:00:00'), reverse=True)
1673
+ products_sorted = sorted(products_by_date, key=lambda p: not p.get('is_top', False))
1674
 
1675
  try:
1676
  product = products_sorted[index]
 
1780
  if 'id' not in product:
1781
  product['id'] = str(uuid.uuid4())
1782
  needs_save = True
1783
+ if 'created_at' not in product:
1784
+ product['created_at'] = '2020-01-01 00:00:00'
1785
+ needs_save = True
1786
  if needs_save:
1787
+ logging.info("Assigning unique IDs and/or default creation dates to products.")
1788
  data['products'] = products
1789
  save_data(data)
1790
 
 
1971
 
1972
  new_product = {
1973
  'id': str(uuid.uuid4()),
1974
+ 'created_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
1975
  'name': name, 'price': price, 'description': description,
1976
  'category': category if any(c['name'] == category for c in categories) else 'Без категории',
1977
  'subcategory': subcategory,