feat: 抽奖活动保存支持 JSON 型号并修复 py2 规则文案解析

- model_ids 兼容三种传法:model_ids[] 数组、多值、JSON 字符串
- _parse_rule_sections 改用 force_text:py2 下 str(unicode 中文) 抛
  UnicodeEncodeError,而它是 ValueError 的子类,会被外层 except 吞成
  400001「参数格式不正确」

FFIB 7 小时之前
父节点
当前提交
6e4eaafd43
共有 1 个文件被更改,包括 32 次插入 和 3 次删除
  1. 32 3
      api/sale/lottery_admin_views.py

+ 32 - 3
api/sale/lottery_admin_views.py

@@ -3,6 +3,7 @@
3 3
 import json
4 4
 
5 5
 from django.db import transaction
6
+from django.utils.encoding import force_text
6 7
 from django.db.models import Count, Q
7 8
 from django_logit import logit
8 9
 from django_response import response
@@ -17,6 +18,32 @@ DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
17 18
 RESULT_CHOICES = (LotteryEntryInfo.PENDING, LotteryEntryInfo.WON, LotteryEntryInfo.LOST)
18 19
 
19 20
 
21
+def _parse_model_ids(request):
22
+    """
23
+    解析参与型号,兼容三种传法:
24
+      model_ids[]=A&model_ids[]=B   数组(qs brackets)
25
+      model_ids=A&model_ids=B       多值
26
+      model_ids=["A","B"]           JSON 字符串
27
+    """
28
+    values = request.POST.getlist('model_ids[]') or request.POST.getlist('model_ids')
29
+
30
+    if len(values) == 1:
31
+        raw = force_text(values[0]).strip()
32
+
33
+        if raw.startswith('['):
34
+            try:
35
+                parsed = json.loads(raw)
36
+
37
+                if isinstance(parsed, list):
38
+                    values = parsed
39
+            except ValueError:
40
+                pass
41
+
42
+    cleaned = [text for text in (force_text(value).strip() for value in values) if text]
43
+
44
+    return cleaned or None
45
+
46
+
20 47
 def _parse_datetime(value):
21 48
     """'2026-10-29 00:00:00' → aware datetime;空串 → None"""
22 49
     value = (value or '').strip()
@@ -45,13 +72,15 @@ def _parse_rule_sections(value):
45 72
         if not isinstance(section, dict):
46 73
             raise ValueError('rule section must be an object')
47 74
 
48
-        title = (section.get('title') or '').strip()
75
+        title = force_text(section.get('title') or '').strip()
49 76
         items = section.get('items') or []
50 77
 
51 78
         if not isinstance(items, list):
52 79
             raise ValueError('rule section items must be a list')
53 80
 
54
-        items = [str(item).strip() for item in items if str(item).strip()]
81
+        # 不能用 str():py2 下 str(unicode 中文) 抛 UnicodeEncodeError,
82
+        # 而它是 ValueError 的子类,会被外层 except 吞成「参数格式不正确」
83
+        items = [text for text in (force_text(item).strip() for item in items) if text]
55 84
 
56 85
         if title or items:
57 86
             cleaned.append({'title': title, 'items': items})
@@ -128,7 +157,7 @@ def lottery_campaign_save_api(request, administrator):
128 157
         'start_at': start_at,
129 158
         'end_at': end_at,
130 159
         'draw_at': draw_at,
131
-        'model_ids': (request.POST.getlist('model_ids[]') or request.POST.getlist('model_ids')) or None,
160
+        'model_ids': _parse_model_ids(request),
132 161
         'rule_sections': rule_sections,
133 162
     }
134 163