-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_helper.py
160 lines (114 loc) · 3.87 KB
/
format_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import crud
import model
import helper
def format_players(game):
"""Takes in game object and returns formatted player count"""
min_players = game.min_players
max_players = game.max_players
num_players = []
if min_players:
num_players = min_players
if max_players and (max_players != min_players):
num_players = f"{min_players}-{max_players}"
return num_players
else:
return ""
def format_playtime(game):
"""Takes in game object and returns formatted playtime"""
min_playtime = game.min_playtime
max_playtime = game.max_playtime
playtime = []
if min_playtime:
playtime = f"{min_playtime} mins"
if max_playtime and (max_playtime != min_playtime):
playtime = f"{min_playtime}-{max_playtime} mins"
return playtime
else:
return ""
def format_msrp(game):
"""Takes in game object and returns formatted msrp"""
msrp = game.msrp
if msrp and msrp > 0:
formatted_msrp = "{:.2f}".format(msrp)
return formatted_msrp
else:
return ""
def format_comment(listed_game):
"""Takes in ListedGame object and returns formatted comment"""
comment = listed_game.comment
if comment:
return comment
else:
return ""
def format_publishers(game):
"""Takes in game object and returns primary publisher name"""
publishers_list = game.publishers
try:
return publishers_list[0].name
except:
return ""
def format_designers(game):
"""Takes in game object and returns list of designers as string"""
designers = game.designers
designer_names = []
for designer in designers:
if designer and designer.name:
designer_names.append(designer.name)
if len(designer_names) > 1:
designers_list_str = ", ".join(designer_names)
return designers_list_str
elif len(designer_names) == 1:
return designer_names[0]
else:
return ""
def format_mechanics(game):
"""Takes in game object and returns list of mechanics as string"""
mechanics = game.mechanics
mechanic_names = []
for mechanic in mechanics:
if mechanic and mechanic.name:
mechanic_names.append(mechanic.name)
if len(mechanic_names) > 1:
mechanics_list_str = ", ".join(mechanic_names)
return mechanics_list_str
elif len(mechanic_names) == 1:
return mechanic_names[0]
else:
return ""
def format_categories(game):
"""Takes in game object and returns list of categories as string"""
categories = game.categories
category_names = []
for category in categories:
if category and category.name:
category_names.append(category.name)
if len(category_names) > 1:
categories_list_str = ", ".join(category_names)
return categories_list_str
elif len(category_names) == 1:
return category_names[0]
else:
return ""
def format_price(price):
"""Takes in price and formats as str with two decimal places"""
formatted_price = "{:.2f}".format(price)
return formatted_price
def format_game_details(game):
"""Takes in Game object and returns formatted basic game details"""
image_url = game.image_url
game_name = game.name
min_age = game.min_age
publish_year = game.publish_year
description = game.description
players = format_players(game)
playtime = format_playtime(game)
msrp = format_msrp(game)
primary_publisher = format_publishers(game)
designers = format_designers(game)
mechanics = format_mechanics(game)
categories = format_categories(game)
return (image_url, game_name, min_age, publish_year, description, players,
playtime, msrp, primary_publisher, designers, mechanics, categories)
if __name__ == '__main__':
from server import app
model.connect_to_db(app)