-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
156 lines (122 loc) · 4.1 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
import hashlib
from database_connector import get_table_connection
from abc import ABCMeta, abstractproperty
class AbstractModel(metaclass=ABCMeta):
@abstractproperty
def table_name(self):
pass
@abstractproperty
def primary_key(self):
pass
@abstractproperty
def global_secondary_index(self):
pass
@abstractproperty
def global_secondary_name(self):
pass
class Model(AbstractModel):
''' Acts as a mini ORM for getting model properties and saving to a database'''
attributes = {}
table_name = ''
primary_key = ''
global_secondary_index = ''
global_secondary_name = ''
def __init__(self, attributes = {}):
self.attributes = attributes
def set_attributes(self, attributes):
self.attributes = attributes
def set_attribute(self, attribute, value):
self.attributes[attribute] = value
def get_attributes(self):
return self.attributes
def get(self, attribute):
if attribute in self.get_attributes():
return self.attributes[attribute]
else:
return None
def set(self, attribute, value):
self.attributes[attribute] = value
def find(self, key = False, gsi = False):
"""
Performs a looks of a primary key (hash) or GSI (username) and sets the
database attributes into the model object
----------
arg1 : key
hash for the item
arg2 : gsi
global secondary Index
Returns
-------
mixed
Only makes a boolean return when something goes wrong
"""
try:
ClientError
except NameError:
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key, Attr
try:
if key and gsi == False:
attributes = get_table_connection(self.table_name).get_item(
Key={
self.primary_key : key,
}
)
if 'Item' in attributes:
attributes = attributes['Item']
elif key and gsi == True:
attributes = get_table_connection(self.table_name).query(
IndexName=self.global_secondary_name,
KeyConditionExpression=Key(self.global_secondary_index).eq(key)
)
if 'Items' in attributes and len(attributes['Items']) == 1:
attributes = attributes['Items'][0]
else:
return False
self.set_attributes(attributes)
except ClientError:
self.set_attributes({})
def save(self):
"""
Saves to the database
Returns
-------
None
"""
get_table_connection(self.table_name).put_item(Item=self.get_attributes())
class User(Model):
table_name = 'usersTable'
primary_key = 'id'
global_secondary_index = 'username'
global_secondary_name = 'userName'
def check_password(self, request_password):
"""
validates password against the database
----------
arg1 : request_password
password passed in on the request
Returns
-------
boolean
Returns the result of the password check
"""
password, salt = self.get('password').split(':')
return password == hashlib.sha256(salt.encode() + request_password.encode()).hexdigest()
def get_current_step(self):
"""
Pulls up the current registation step
Returns
-------
String
Returns link of to the current registation step
"""
if self.get('rsvp_step_status') == None:
return 'register-rsvp'
elif self.get('profile_step_status') == None:
return 'register-profile'
elif self.get('activities_step_status') == None:
return 'register-activities'
elif self.get('hotel_step_status') == None:
return 'register-hotel'
else:
return 'register-complete'