fix: 规则文案解析在 Python 2 下误报参数错误

_parse_rule_sections 里用 str(item) 处理条目文本。py2 下 str(unicode 中文)
会抛 UnicodeEncodeError,而它是 ValueError 的子类,被外层
except (ValueError, TypeError) 吞掉,最终返回 400001「参数格式不正确」。
py3 下 str() 不做编码,所以本地开发正常、线上(2.7)失败。

改用 force_text(Django 1.x/3.x 均有),title 同样处理。

FFIB преди 8 часа
родител
ревизия
139c89b02d
променени са 1 файла, в които са добавени 5 реда и са изтрити 2 реда
  1. 5 2
      api/sale/lottery_admin_views.py

+ 5 - 2
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
@@ -45,13 +46,15 @@ def _parse_rule_sections(value):
45 46
         if not isinstance(section, dict):
46 47
             raise ValueError('rule section must be an object')
47 48
 
48
-        title = (section.get('title') or '').strip()
49
+        title = force_text(section.get('title') or '').strip()
49 50
         items = section.get('items') or []
50 51
 
51 52
         if not isinstance(items, list):
52 53
             raise ValueError('rule section items must be a list')
53 54
 
54
-        items = [str(item).strip() for item in items if str(item).strip()]
55
+        # 不能用 str():py2 下 str(unicode 中文) 抛 UnicodeEncodeError,
56
+        # 而它是 ValueError 的子类,会被外层 except 吞成「参数格式不正确」
57
+        items = [text for text in (force_text(item).strip() for item in items) if text]
55 58
 
56 59
         if title or items:
57 60
             cleaned.append({'title': title, 'items': items})