Markishime commited on
Commit
82b42ca
·
1 Parent(s): 9d3b8a9

Fix upload limit calculation logic

Browse files

- Calculate uploads_remaining as uploads_limit - uploads_used instead of trusting parent value
- Fix can_start_analysis() to recalculate remaining for accuracy
- Fix display to show correct remaining count (e.g., 0/2 used = 2 remaining, not 0)
- Ensure limit is only reached when uploads_used >= uploads_limit

Files changed (2) hide show
  1. modules/upload.py +3 -0
  2. utils/cropdrive_integration.py +43 -20
modules/upload.py CHANGED
@@ -1081,10 +1081,13 @@ def upload_section():
1081
  uploads_limit = limit_info.get('uploads_limit', 0)
1082
  uploads_remaining = limit_info.get('uploads_remaining', 0)
1083
 
 
1084
  if uploads_limit > 0:
1085
  if uploads_remaining == float('inf') or uploads_remaining == -1:
1086
  st.info(f"📊 **Analyses:** Unlimited")
1087
  else:
 
 
1088
  st.info(f"📊 **Analyses:** {uploads_used}/{uploads_limit} used ({uploads_remaining} remaining)")
1089
 
1090
  # Check if limit exceeded
 
1081
  uploads_limit = limit_info.get('uploads_limit', 0)
1082
  uploads_remaining = limit_info.get('uploads_remaining', 0)
1083
 
1084
+ # Only show limit info if limit is set (greater than 0)
1085
  if uploads_limit > 0:
1086
  if uploads_remaining == float('inf') or uploads_remaining == -1:
1087
  st.info(f"📊 **Analyses:** Unlimited")
1088
  else:
1089
+ # Display: X/Y used (Z remaining)
1090
+ # Example: 0/2 used (2 remaining), 1/2 used (1 remaining), 2/2 used (0 remaining)
1091
  st.info(f"📊 **Analyses:** {uploads_used}/{uploads_limit} used ({uploads_remaining} remaining)")
1092
 
1093
  # Check if limit exceeded
utils/cropdrive_integration.py CHANGED
@@ -197,7 +197,7 @@ def initialize_integration():
197
  uploads_used = query_params.get('uploadsUsed', '0')
198
  uploads_limit = query_params.get('uploadsLimit', '0')
199
  upload_limit_exceeded = query_params.get('uploadLimitExceeded', 'false')
200
- uploads_remaining = query_params.get('uploadsRemaining', '0')
201
 
202
  # Convert to appropriate types
203
  try:
@@ -215,14 +215,23 @@ def initialize_integration():
215
  except (AttributeError, ValueError):
216
  upload_limit_exceeded = False
217
 
218
- try:
219
- uploads_remaining_str = str(uploads_remaining)
220
- if uploads_remaining_str.lower() == 'infinity' or uploads_remaining_str == '-1':
221
- uploads_remaining = float('inf')
222
- else:
223
- uploads_remaining = int(uploads_remaining) if uploads_remaining else 0
224
- except (ValueError, TypeError):
225
- uploads_remaining = 0
 
 
 
 
 
 
 
 
 
226
 
227
  # Store user config in session state
228
  if 'user_config' not in st.session_state:
@@ -409,24 +418,33 @@ def can_start_analysis():
409
  Returns:
410
  True if analysis can start, False otherwise
411
  """
412
- # Check if upload limit is exceeded
 
 
413
  upload_limit_exceeded = st.session_state.get('upload_limit_exceeded', False)
 
 
 
414
  if upload_limit_exceeded:
415
  return False
416
 
417
- # Check remaining uploads
418
- uploads_remaining = st.session_state.get('uploads_remaining', 0)
 
 
 
 
419
 
420
- # If uploads_remaining is infinity or -1, allow unlimited analyses
421
- if uploads_remaining == float('inf') or uploads_remaining == -1:
422
  return True
423
 
424
- # If uploads_remaining is 0 or less, block analysis
425
- if uploads_remaining <= 0:
426
- return False
427
 
428
- # Otherwise, allow analysis
429
- return True
430
 
431
  def get_upload_limit_info():
432
  """
@@ -438,7 +456,12 @@ def get_upload_limit_info():
438
  uploads_used = st.session_state.get('uploads_used', 0)
439
  uploads_limit = st.session_state.get('uploads_limit', 0)
440
  upload_limit_exceeded = st.session_state.get('upload_limit_exceeded', False)
441
- uploads_remaining = st.session_state.get('uploads_remaining', 0)
 
 
 
 
 
442
 
443
  return {
444
  'uploads_used': uploads_used,
 
197
  uploads_used = query_params.get('uploadsUsed', '0')
198
  uploads_limit = query_params.get('uploadsLimit', '0')
199
  upload_limit_exceeded = query_params.get('uploadLimitExceeded', 'false')
200
+ uploads_remaining_param = query_params.get('uploadsRemaining', '')
201
 
202
  # Convert to appropriate types
203
  try:
 
215
  except (AttributeError, ValueError):
216
  upload_limit_exceeded = False
217
 
218
+ # Calculate uploads_remaining ourselves: limit - used
219
+ # This ensures correct calculation regardless of what parent sends
220
+ if uploads_limit == 0 or uploads_limit == -1:
221
+ # No limit or unlimited plan
222
+ uploads_remaining = float('inf')
223
+ else:
224
+ # Calculate remaining: limit - used
225
+ uploads_remaining = max(0, uploads_limit - uploads_used)
226
+
227
+ # Override with parent's value only if it indicates unlimited (-1 or infinity)
228
+ if uploads_remaining_param:
229
+ try:
230
+ uploads_remaining_str = str(uploads_remaining_param).lower()
231
+ if uploads_remaining_str == 'infinity' or uploads_remaining_str == '-1' or uploads_remaining_str == 'inf':
232
+ uploads_remaining = float('inf')
233
+ except (ValueError, TypeError):
234
+ pass # Use our calculated value
235
 
236
  # Store user config in session state
237
  if 'user_config' not in st.session_state:
 
418
  Returns:
419
  True if analysis can start, False otherwise
420
  """
421
+ # Get current values
422
+ uploads_used = st.session_state.get('uploads_used', 0)
423
+ uploads_limit = st.session_state.get('uploads_limit', 0)
424
  upload_limit_exceeded = st.session_state.get('upload_limit_exceeded', False)
425
+ uploads_remaining = st.session_state.get('uploads_remaining', 0)
426
+
427
+ # If upload_limit_exceeded flag is set, block analysis
428
  if upload_limit_exceeded:
429
  return False
430
 
431
+ # If no limit set (0 or -1), allow unlimited analyses
432
+ if uploads_limit == 0 or uploads_limit == -1:
433
+ return True
434
+
435
+ # Recalculate remaining to ensure accuracy: limit - used
436
+ calculated_remaining = max(0, uploads_limit - uploads_used)
437
 
438
+ # If calculated remaining is infinity, allow
439
+ if calculated_remaining == float('inf'):
440
  return True
441
 
442
+ # If calculated remaining is greater than 0, allow analysis
443
+ if calculated_remaining > 0:
444
+ return True
445
 
446
+ # Otherwise, block (remaining is 0 or less)
447
+ return False
448
 
449
  def get_upload_limit_info():
450
  """
 
456
  uploads_used = st.session_state.get('uploads_used', 0)
457
  uploads_limit = st.session_state.get('uploads_limit', 0)
458
  upload_limit_exceeded = st.session_state.get('upload_limit_exceeded', False)
459
+
460
+ # Calculate remaining: limit - used (ensure accuracy)
461
+ if uploads_limit == 0 or uploads_limit == -1:
462
+ uploads_remaining = float('inf')
463
+ else:
464
+ uploads_remaining = max(0, uploads_limit - uploads_used)
465
 
466
  return {
467
  'uploads_used': uploads_used,