Skip to content

Commit b2c0d9d

Browse files
authored
Support Flex Message update 2 (#303)
* feat: add Background model * feat: support justifyContent and alignItem * update: spacer has been discontinued * feat: support background model * test: remove Spacer test case & establish background test cases * fix: flake8 error message * update: change to require property * chore: remove SpacerComponent remaining code * fix: linter
1 parent 5ab6ba2 commit b2c0d9d

File tree

7 files changed

+146
-36
lines changed

7 files changed

+146
-36
lines changed

examples/flask-kitchensink/app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
4545
MemberJoinedEvent, MemberLeftEvent,
4646
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
47-
TextComponent, SpacerComponent, IconComponent, ButtonComponent,
47+
TextComponent, IconComponent, ButtonComponent,
4848
SeparatorComponent, QuickReply, QuickReplyButton,
4949
ImageSendMessage)
5050

@@ -309,8 +309,6 @@ def handle_text_message(event):
309309
layout='vertical',
310310
spacing='sm',
311311
contents=[
312-
# callAction, separator, websiteAction
313-
SpacerComponent(size='sm'),
314312
# callAction
315313
ButtonComponent(
316314
style='link',

linebot/models/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
IconComponent,
7979
ImageComponent,
8080
SeparatorComponent,
81-
SpacerComponent,
8281
TextComponent,
8382
SpanComponent
8483
)
@@ -196,3 +195,8 @@
196195
ActionResult,
197196
Things,
198197
)
198+
199+
from .background import ( # noqa
200+
Background,
201+
LinearGradientBackground,
202+
)

linebot/models/background.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
"""linebot.models.emojis module."""
16+
17+
from __future__ import unicode_literals
18+
19+
from abc import ABCMeta
20+
21+
from future.utils import with_metaclass
22+
23+
from .base import Base
24+
25+
26+
class Background(with_metaclass(ABCMeta, Base)):
27+
"""Background."""
28+
29+
def __init__(self, **kwargs):
30+
"""__init__ method.
31+
32+
:param kwargs:
33+
"""
34+
super(Background, self).__init__(**kwargs)
35+
36+
self.type = None
37+
38+
39+
class LinearGradientBackground(Background):
40+
"""LinearGradientBackground."""
41+
42+
def __init__(self, angle, start_color, end_color,
43+
center_color=None, center_position=None, **kwargs):
44+
"""__init__ method.
45+
46+
:param str type: The type of background used
47+
:param str angle: The angle at which a linear gradient moves
48+
:param str start_color: The color at the gradient's starting point
49+
:param str end_color: The color at the gradient's ending point
50+
:param str center_color: The color in the middle of the gradient
51+
:param str center_position: The position of the intermediate color stop
52+
:param kwargs:
53+
"""
54+
super(LinearGradientBackground, self).__init__(**kwargs)
55+
self.type = 'linearGradient'
56+
self.angle = angle
57+
self.start_color = start_color
58+
self.end_color = end_color
59+
self.center_color = center_color
60+
self.center_position = center_position

linebot/models/flex_message.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from future.utils import with_metaclass
2222

23+
from .background import Background, LinearGradientBackground
2324
from .actions import get_action
2425
from .base import Base
2526
from .send_messages import SendMessage
@@ -234,6 +235,9 @@ def __init__(self,
234235
border_color=None,
235236
border_width=None,
236237
corner_radius=None,
238+
justify_content=None,
239+
align_items=None,
240+
background=None,
237241
width=None,
238242
height=None,
239243
flex=None,
@@ -260,6 +264,12 @@ def __init__(self,
260264
:param str border_color: Color of box border
261265
:param str border_width: Width of box border
262266
:param str corner_radius: Radius at the time of rounding the corners of the border
267+
:param str justify_content: How child elements are aligned along the main axis of
268+
the parent element
269+
:param str align_items: How child elements are aligned along the cross axis of
270+
the parent element
271+
:param background: Background object
272+
:type background: T <= :py:class:`linebot.models.background.Background`
263273
:param str width: Width of the box
264274
:param str height: Height of the box
265275
:param float flex: The ratio of the width or height of this box within the parent box
@@ -291,6 +301,8 @@ def __init__(self,
291301
self.border_color = border_color
292302
self.border_width = border_width
293303
self.corner_radius = corner_radius
304+
self.justify_content = justify_content
305+
self.align_items = align_items
294306
self.width = width
295307
self.height = height
296308
self.flex = flex
@@ -307,6 +319,9 @@ def __init__(self,
307319
self.offset_start = offset_start
308320
self.offset_end = offset_end
309321
self.action = get_action(action)
322+
self.background = Background.get_or_new_from_json_dict_with_types(
323+
background, {'linearGradient': LinearGradientBackground}
324+
)
310325

311326
new_contents = []
312327
if contents:
@@ -320,7 +335,6 @@ def __init__(self,
320335
'image': ImageComponent,
321336
'span': SpanComponent,
322337
'separator': SeparatorComponent,
323-
'spacer': SpacerComponent,
324338
'text': TextComponent
325339
}
326340
))
@@ -349,6 +363,7 @@ def __init__(self,
349363
style=None,
350364
color=None,
351365
gravity=None,
366+
adjust_mode=None,
352367
**kwargs):
353368
"""__init__ method.
354369
@@ -368,6 +383,7 @@ def __init__(self,
368383
Background color when the style property is primary or secondary.
369384
Use a hexadecimal color code
370385
:param str gravity: Vertical alignment style
386+
:param str adjust_mode: The method by which to adjust the text font size
371387
:param kwargs:
372388
"""
373389
super(ButtonComponent, self).__init__(**kwargs)
@@ -384,6 +400,7 @@ def __init__(self,
384400
self.style = style
385401
self.color = color
386402
self.gravity = gravity
403+
self.adjust_mode = adjust_mode
387404

388405

389406
class FillerComponent(FlexComponent):
@@ -541,26 +558,6 @@ def __init__(self, margin=None, color=None, **kwargs):
541558
self.color = color
542559

543560

544-
class SpacerComponent(FlexComponent):
545-
"""SpacerComponent.
546-
547-
https://developers.line.biz/en/reference/messaging-api/#spacer
548-
549-
This is an invisible component that places a fixed-size space
550-
at the beginning or end of the box
551-
"""
552-
553-
def __init__(self, size=None, **kwargs):
554-
"""__init__ method.
555-
556-
:param str size: Size of the space
557-
:param kwargs:
558-
"""
559-
super(SpacerComponent, self).__init__(**kwargs)
560-
self.type = 'spacer'
561-
self.size = size
562-
563-
564561
class SpanComponent(FlexComponent):
565562
"""SpanComponent.
566563

tests/models/serialize_test_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class SerializeTestCase(unittest.TestCase):
4141
AGE = "age"
4242
AREA = "area"
4343
SUBSCRIPTION_PERIOD = "subscriptionPeriod"
44-
SPACER = 'spacer'
4544
SPAN = 'span'
4645
BUBBLE = 'bubble'
4746
CAROUSEL = 'carousel'
@@ -60,6 +59,7 @@ class SerializeTestCase(unittest.TestCase):
6059
BUTTONS = 'buttons'
6160
CONFIRM = 'confirm'
6261
IMAGE_CAROUSEL = 'image_carousel'
62+
LINEAR_GRADIENT = 'linearGradient'
6363

6464
def serialize_as_dict(self, obj, type=None):
6565
if isinstance(obj, Base):

tests/models/test_background.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the 'License'); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from __future__ import unicode_literals, absolute_import
16+
17+
import unittest
18+
19+
from linebot.models import LinearGradientBackground
20+
from tests.models.serialize_test_case import SerializeTestCase
21+
22+
23+
class TestBackground(SerializeTestCase):
24+
def test_background(self):
25+
arg = {
26+
"type": "linearGradient",
27+
"angle": "0deg",
28+
"start_color": "#ff0000",
29+
"end_color": "#0000ff"
30+
}
31+
self.assertEqual(
32+
self.serialize_as_dict(arg, type=self.LINEAR_GRADIENT),
33+
LinearGradientBackground(**arg).as_json_dict()
34+
)
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

tests/models/test_flex_message.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
ButtonComponent,
3030
FillerComponent,
3131
IconComponent,
32-
SpacerComponent,
3332
SpanComponent,
3433
URIAction,
34+
LinearGradientBackground,
3535
)
3636
from tests.models.serialize_test_case import SerializeTestCase
3737

@@ -158,6 +158,28 @@ def test_box_component(self):
158158
BoxComponent(**arg).as_json_dict()
159159
)
160160

161+
def test_box_component_with_linear_gradient(self):
162+
arg = {
163+
'layout': 'vertical',
164+
'contents': [],
165+
'background_color': '#00000000',
166+
'border_width': 'light',
167+
'corner_radius': 'xs',
168+
'flex': 2,
169+
'background': LinearGradientBackground(
170+
angle='0deg',
171+
start_color='#ff0000',
172+
center_color='#0000ff',
173+
end_color='#00ff00',
174+
center_position='10%'
175+
)
176+
}
177+
178+
self.assertEqual(
179+
self.serialize_as_dict(arg, type=self.BOX),
180+
BoxComponent(**arg).as_json_dict()
181+
)
182+
161183
def test_button_component(self):
162184
arg = {
163185
'action':
@@ -212,15 +234,6 @@ def test_separator_component(self):
212234
SeparatorComponent(**arg).as_json_dict()
213235
)
214236

215-
def test_spacer_component(self):
216-
arg = {
217-
'size': 'md'
218-
}
219-
self.assertEqual(
220-
self.serialize_as_dict(arg, type=self.SPACER),
221-
SpacerComponent(**arg).as_json_dict()
222-
)
223-
224237
def test_span_component(self):
225238
arg = {
226239
'type': 'span',

0 commit comments

Comments
 (0)