-
Notifications
You must be signed in to change notification settings - Fork 1
/
listing_helper.py
127 lines (92 loc) · 3.63 KB
/
listing_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
import crud
import model
import helper
import format_helper
def get_user_listed_games(username):
"""Returns list of user's listed games as dictionary"""
listed_games = crud.get_user_listed_games(username)
results = []
for listed_game in listed_games:
price = format_helper.format_price(listed_game.price)
msrp = format_helper.format_msrp(listed_game.game)
comment = format_helper.format_comment(listed_game)
results.append(
{
"key": listed_game.user_games_id,
"name": listed_game.game.name,
"condition": listed_game.condition,
"price": price,
"username": listed_game.user.username,
"email": listed_game.user.email,
"comment": comment,
"image_url": listed_game.game.image_url,
"msrp": msrp
}
)
return results
def get_user_games_able_to_sell(username):
"""Returns list of user's games available to sell, as dictionary"""
own_games = crud.get_user_current_own_games(username)
listed_games = crud.get_user_listed_games(username)
listed_game_ids = []
for listed_game in listed_games:
listed_game_ids.append(listed_game.user_games_id)
results = []
for game in own_games:
if game.id not in listed_game_ids:
msrp = format_helper.format_msrp(game.game)
results.append(
{
"key": game.id,
"name": game.game.name,
"msrp": msrp,
"image_url": game.game.image_url
}
)
return results
def list_game(user_game_id, condition, price, comment):
"""Creates new listed game and returns listed game as dictionary"""
previously_listed = crud.get_listed_game_by_id(user_game_id)
if previously_listed:
reactivated = crud.update_listed_game_to_true(user_game_id)
listed_game = crud.update_listed_game(user_game_id, condition, price, comment)
else:
listed_game = crud.create_listed_game(user_game_id, condition, price, comment)
price = format_helper.format_price(listed_game.price)
msrp = format_helper.format_msrp(listed_game.game)
comment = format_helper.format_comment(listed_game)
return {
"key": listed_game.user_games_id,
"name": listed_game.game.name,
"condition": listed_game.condition,
"price": price,
"username": listed_game.user.username,
"email": listed_game.user.email,
"comment": comment,
"image_url": listed_game.game.image_url,
"msrp": msrp
}
def update_user_listed_game(user_game_id, condition, price, comment):
"""Updates listed game and returns updates as dictionary"""
updated_game = crud.update_listed_game(user_game_id, condition, price, comment)
price = format_helper.format_price(updated_game.price)
comment = format_helper.format_comment(updated_game)
return {
"key": updated_game.user_games_id,
"condition": updated_game.condition,
"price": price,
"comment": comment
}
def deactivate_listing(user_game_id):
"""Updates ListedGame active=False and returns removed ListedGame as dict"""
deactivated_listing = crud.update_listed_game_to_false(user_game_id)
msrp = format_helper.format_msrp(deactivated_listing.game)
return {
"key": deactivated_listing.user_games_id,
"name": deactivated_listing.game.name,
"msrp": msrp,
"image_url": deactivated_listing.game.image_url
}
if __name__ == '__main__':
from server import app
model.connect_to_db(app)