refactor: model_ids 只接受 JSON 数组字符串

不再兼容 model_ids[] 数组传法,前端统一提交 ["A","B"] 形式;
解析放进 try,格式非法时与其它字段一样返回「参数格式不正确」。

FFIB 7 stundas atpakaļ
vecāks
revīzija
b237026b9f
1 mainītis faili ar 10 papildinājumiem un 18 dzēšanām
  1. 10 18
      api/sale/lottery_admin_views.py

+ 10 - 18
api/sale/lottery_admin_views.py

@@ -19,27 +19,18 @@ RESULT_CHOICES = (LotteryEntryInfo.PENDING, LotteryEntryInfo.WON, LotteryEntryIn
19 19
 
20 20
 
21 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')
22
+    """解析参与型号:前端统一传 JSON 数组字符串,例如 ["B091","B092"]"""
23
+    raw = force_text(request.POST.get('model_ids') or '').strip()
29 24
 
30
-    if len(values) == 1:
31
-        raw = force_text(values[0]).strip()
25
+    if not raw:
26
+        return None
32 27
 
33
-        if raw.startswith('['):
34
-            try:
35
-                parsed = json.loads(raw)
28
+    parsed = json.loads(raw)
36 29
 
37
-                if isinstance(parsed, list):
38
-                    values = parsed
39
-            except ValueError:
40
-                pass
30
+    if not isinstance(parsed, list):
31
+        raise ValueError('model_ids must be a json array')
41 32
 
42
-    cleaned = [text for text in (force_text(value).strip() for value in values) if text]
33
+    cleaned = [text for text in (force_text(item).strip() for item in parsed) if text]
43 34
 
44 35
     return cleaned or None
45 36
 
@@ -146,6 +137,7 @@ def lottery_campaign_save_api(request, administrator):
146 137
         draw_at = _parse_datetime(request.POST.get('draw_at', ''))
147 138
         winner_count = int(request.POST.get('winner_count') or 1)
148 139
         claim_days = int(request.POST.get('claim_days') or 14)
140
+        model_ids = _parse_model_ids(request)
149 141
     except (ValueError, TypeError):
150 142
         return response(ParamStatusCode.PARAM_INVALID)
151 143
 
@@ -157,7 +149,7 @@ def lottery_campaign_save_api(request, administrator):
157 149
         'start_at': start_at,
158 150
         'end_at': end_at,
159 151
         'draw_at': draw_at,
160
-        'model_ids': _parse_model_ids(request),
152
+        'model_ids': model_ids,
161 153
         'rule_sections': rule_sections,
162 154
     }
163 155