-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
172 lines (140 loc) · 4.93 KB
/
models.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
161
162
163
164
165
166
167
168
169
170
171
172
import os
from time import time
from marshmallow import ValidationError
from pynamodb.attributes import UnicodeAttribute, UTCDateTimeAttribute
from pynamodb.connection import Connection
from pynamodb.exceptions import TransactWriteError
from pynamodb.indexes import AllProjection, GlobalSecondaryIndex
from pynamodb.models import Model
from pynamodb.transactions import TransactWrite
class GSI1(GlobalSecondaryIndex):
class Meta:
read_capacity_units = 2
write_capacity_units = 1
# All attributes are projected
projection = AllProjection()
gsi1PK = UnicodeAttribute(hash_key=True)
gsi1SK = UnicodeAttribute(range_key=True)
class GSI2(GlobalSecondaryIndex):
class Meta:
read_capacity_units = 2
write_capacity_units = 1
# All attributes are projected
projection = AllProjection()
gsi2PK = UnicodeAttribute(hash_key=True)
gsi2SK = UnicodeAttribute(range_key=True)
class BaseEventManagerModel(Model):
class Meta:
table_name = os.getenv("APP_TABLE_NAME", "undefined")
host = os.getenv("DYNAMODB_HOST", "undefined")
PK = UnicodeAttribute(hash_key=True)
SK = UnicodeAttribute(range_key=True)
entity_type = UnicodeAttribute()
ID = UnicodeAttribute(null=True)
def to_dict(self):
rval = {}
for key in self.attribute_values:
rval[key] = self.__getattribute__(key)
return rval
@classmethod
def prepare_key(cls, key):
raise NotImplementedError()
class Event(BaseEventManagerModel):
title = UnicodeAttribute()
description = UnicodeAttribute()
date = UTCDateTimeAttribute()
city = UnicodeAttribute()
zip_code = UnicodeAttribute()
created_by = UnicodeAttribute()
gsi1 = GSI1()
gsi1PK = UnicodeAttribute()
gsi1SK = UnicodeAttribute()
gsi2 = GSI2()
gsi2PK = UnicodeAttribute()
gsi2SK = UnicodeAttribute()
@classmethod
def prepare_key(cls, key):
return f"EVENT#{key}"
class User(BaseEventManagerModel):
email = UnicodeAttribute()
password = UnicodeAttribute()
first_name = UnicodeAttribute(null=True)
last_name = UnicodeAttribute(null=True)
@classmethod
def prepare_key(cls, key):
return f"USER#{key}"
@classmethod
def put_user(cls, user, old_user):
new_email_key: str = Email.prepare_key(user.email)
new_email: Email = Email(
PK=new_email_key,
SK=new_email_key,
entity_type="Email",
user=user.ID
)
connection: Connection = Connection(
host=os.getenv("DYNAMODB_HOST", "undefined")
)
try:
with TransactWrite(
connection=connection,
client_request_token=str(time())
) as transaction:
transaction.save(user)
if old_user:
if old_user.email != user.email:
old_email_key: str = Email.prepare_key(old_user.email)
transaction.delete(
Email.get(old_email_key, old_email_key)
)
transaction.save(
new_email, condition=Email.PK.does_not_exist()
)
else:
transaction.save(
new_email, condition=Email.PK.does_not_exist()
)
except TransactWriteError as err:
if (
err.cancellation_reasons and (
err.cancellation_reasons[1]
or err.cancellation_reasons[2]
)
):
raise ValidationError(
{"email": "Email already belongs to another user."}
)
else:
raise ValidationError(
"Something went wrong. Please try again."
)
@classmethod
def delete_user(cls, user):
connection: Connection = Connection(
host=os.getenv("DYNAMODB_HOST", "undefined")
)
try:
with TransactWrite(
connection=connection,
client_request_token=str(time())
) as transaction:
email_key = Email.prepare_key(user.email)
transaction.delete(Email.get(email_key, email_key))
transaction.delete(user)
except TransactWriteError:
raise ValidationError("Something went wrong. Please try again.")
class Email(BaseEventManagerModel):
user = UnicodeAttribute()
@classmethod
def prepare_key(cls, key):
return f"EMAIL#{key}"
class Registration(BaseEventManagerModel):
user = UnicodeAttribute()
event = UnicodeAttribute()
registration_time = UTCDateTimeAttribute()
gsi1 = GSI1()
gsi1PK = UnicodeAttribute()
gsi1SK = UnicodeAttribute()
@classmethod
def prepare_key(cls, key):
return f"REGISTRATION#{key}"