-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
135 lines (96 loc) · 3.33 KB
/
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
import requests
import crud
import model
import rec_helper
import format_helper
from flask_sqlalchemy import SQLAlchemy
def get_user_own_games(username):
"""Returns list of user's own games as dictionary, including its sell status"""
own_games = crud.get_user_current_own_games(username)
results = []
for own_game in own_games:
selling = False
try:
if own_game.listing.active:
selling = True
except:
pass
results.append(
{
"key": own_game.id,
"name": own_game.game.name,
"min_players": own_game.game.min_players,
"max_players": own_game.game.max_players,
"min_playtime": own_game.game.min_playtime,
"max_playtime": own_game.game.max_playtime,
"image_url": own_game.game.image_url,
"selling": selling
}
)
return results
def get_user_wanted_games(username):
"""Returns list of user's wanted games as dictionary"""
wanted_games = crud.get_user_wanted_games(username)
results = []
for wanted_game in wanted_games:
results.append(
{
"key": wanted_game.id,
"name": wanted_game.game.name,
"min_players": wanted_game.game.min_players,
"max_players": wanted_game.game.max_players,
"min_playtime": wanted_game.game.min_playtime,
"max_players": wanted_game.game.max_players,
"image_url": wanted_game.game.image_url
}
)
return results
def remove_game(remove_type, user_game_id):
"""Updates UserGame to own=False or deletes WantedGame"""
removed_game = False
if remove_type == "own":
deactivated = crud.update_listed_game_to_false(user_game_id)
removed_game = crud.update_user_game_to_false(user_game_id)
elif remove_type == "wishlist":
removed_game = crud.delete_wanted_game(user_game_id)
if removed_game:
return "Game was successfully removed"
else:
return "A problem has occurred"
def get_id_set(games):
"""Takes in list of game objects. Returns set of ids"""
id_set = set([])
for game in games:
id_set.add(game.game_id)
return id_set
def get_game_set(games):
"""Takes in list of UserGames or WantedGames and returns Games set"""
game_set = set([])
for game in games:
game_set.add(game.game)
return game_set
def get_game_details(id):
"""Takes in UserGame id and returns basic game details"""
user_game = crud.get_user_game_by_id(id)
game = user_game.game
(image_url, game_name, min_age, publish_year,
description, players, playtime, msrp, primary_publisher,
designers, mechanics, categories) = format_helper.format_game_details(game)
return {
"key": user_game.id,
"image_url": image_url,
"game_name": game_name,
"msrp": msrp,
"min_age": min_age,
"players": players,
"playtime": playtime,
"publisher": primary_publisher,
"designers": designers,
"publish_year": publish_year,
"game_description": description,
"mechanics": mechanics,
"categories": categories,
}
if __name__ == '__main__':
from server import app
model.connect_to_db(app)