-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamo.py
101 lines (79 loc) · 2.99 KB
/
dynamo.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
import boto3
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute, NumberAttribute
from datetime import datetime
import pick
class UserModel(Model):
"""
A DynamoDB User
"""
class Meta:
table_name = "dynamodb-user"
username = UnicodeAttribute(hash_key=True)
#last_updated = UTCDateTimeAttribute()
#created = UTCDateTimeAttribute(default=datetime.now)
class PickModel(Model):
class Meta:
table_name = "dynamodb-pick"
username = UnicodeAttribute()
pick = UnicodeAttribute(hash_key=True)
home = UnicodeAttribute()
away = UnicodeAttribute()
favorite = UnicodeAttribute(null=True)
line = NumberAttribute(default=0, null=True)
ou = NumberAttribute(default=0, null=True)
week = NumberAttribute(default=0)
pick_type = UnicodeAttribute()
outcome = UnicodeAttribute(default='UNDECIDED')
def create_user_table():
UserModel.create_table(read_capacity_units=1, write_capacity_units=1)
def create_pick_table():
PickModel.create_table(read_capacity_units=1, write_capacity_units=1)
def get_user(username):
try:
user = UserModel.get(username)
print('username: ' + user.username)
except UserModel.DoesNotExist:
print("User does not exist")
def insert_pick(username: str, pick: pick.Pick):
pick_record = PickModel(pick.pick)
pick_record.username = username
pick_record.home = pick.home
pick_record.away = pick.away
pick_record.favorite = pick.favorite
pick_record.line = pick.line
pick_record.ou = pick.ou
pick_record.week = pick.week
pick_record.pick_type = pick.pick_type
pick_record.outcome = pick.outcome
pick_record.save()
# import pick
# import dynamo
# picks = dynamo.get_picks_for_user_for_week('manno', 3)
# dynamo.delete_pick('manno', picks[0])
def delete_pick(username: str, pick: pick.Pick):
for item in PickModel.scan(PickModel.username.is_in(username) & (PickModel.week == pick.week) & (PickModel.home == pick.home) & (PickModel.away == pick.away) & (PickModel.pick_type == pick.pick_type) & (PickModel.ou == pick.ou) & (PickModel.line == pick.line)):
print("deleting: " + item.home + " , " + item.away)
item.delete()
def pick_record_to_pick_object(pick_record: PickModel):
return pick.Pick(
pick=pick_record.pick,
home=pick_record.home,
away=pick_record.away,
week=pick_record.week,
favorite=pick_record.favorite,
line=pick_record.line,
ou=pick_record.ou,
pick_type=pick_record.pick_type,
outcome=pick_record.outcome
)
def get_weeks_available(username):
weeks_available = set()
for item in PickModel.scan(PickModel.username == username):
weeks_available.add(item.week)
return weeks_available
def get_picks_for_user_for_week(username, week):
picks = []
for item in PickModel.scan(PickModel.username.is_in(username) & (PickModel.week == week)):
picks.append(pick_record_to_pick_object(item))
return picks