-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdb_util.py
149 lines (114 loc) · 5.41 KB
/
db_util.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
import os
import settings
from boto3.dynamodb.conditions import Key
from jsonschema import ValidationError
from record_not_found_error import RecordNotFoundError
from not_authorized_error import NotAuthorizedError
class DBUtil:
@staticmethod
def exists_article(dynamodb, article_id, user_id=None, status=None):
article_info_table = dynamodb.Table(os.environ['ARTICLE_INFO_TABLE_NAME'])
article_info = article_info_table.get_item(Key={'article_id': article_id}).get('Item')
if article_info is None:
return False
if user_id is not None and article_info['user_id'] != user_id:
return False
if status is not None and article_info['status'] != status:
return False
return True
@staticmethod
def validate_article_existence(dynamodb, article_id, user_id=None, status=None):
article_info_table = dynamodb.Table(os.environ['ARTICLE_INFO_TABLE_NAME'])
article_info = article_info_table.get_item(Key={'article_id': article_id}).get('Item')
if article_info is None:
raise RecordNotFoundError('Record Not Found')
if user_id is not None and article_info['user_id'] != user_id:
raise NotAuthorizedError('Forbidden')
if status is not None and article_info['status'] != status:
raise RecordNotFoundError('Record Not Found')
return True
@staticmethod
def validate_article_history_existence(dynamodb, article_id):
article_history_table = dynamodb.Table(os.environ['ARTICLE_HISTORY_TABLE_NAME'])
query_params = {
'IndexName': 'article_id-index',
'KeyConditionExpression': Key('article_id').eq(article_id)
}
article_histories = article_history_table.query(**query_params)['Items']
if len(article_histories) != 0:
raise RecordNotFoundError('This article is not removable')
return True
@staticmethod
def validate_user_existence(dynamodb, user_id):
users_table = dynamodb.Table(os.environ['USERS_TABLE_NAME'])
user = users_table.get_item(Key={'user_id': user_id}).get('Item')
if user is None:
raise RecordNotFoundError('Record Not Found')
return True
@staticmethod
def comment_existence(dynamodb, comment_id):
table = dynamodb.Table(os.environ['COMMENT_TABLE_NAME'])
comment = table.get_item(Key={'comment_id': comment_id}).get('Item')
if comment is None:
return False
return True
@staticmethod
def validate_comment_existence(dynamodb, comment_id):
table = dynamodb.Table(os.environ['COMMENT_TABLE_NAME'])
comment = table.get_item(Key={'comment_id': comment_id}).get('Item')
if comment is None:
raise RecordNotFoundError('Record Not Found')
return True
@staticmethod
def validate_parent_comment_existence(dynamodb, comment_id):
table = dynamodb.Table(os.environ['COMMENT_TABLE_NAME'])
comment = table.get_item(Key={'comment_id': comment_id}).get('Item')
if comment is None or comment.get('parent_id'):
raise RecordNotFoundError('Record Not Found')
return True
@staticmethod
def get_validated_comment(dynamodb, comment_id):
table = dynamodb.Table(os.environ['COMMENT_TABLE_NAME'])
comment = table.get_item(Key={'comment_id': comment_id}).get('Item')
if comment is None:
raise RecordNotFoundError('Record Not Found')
return comment
@staticmethod
def items_values_empty_to_none(values):
for k, v in values.items():
if v == '':
values[k] = None
@staticmethod
def query_all_items(dynamodb_table, query_params):
response = dynamodb_table.query(**query_params)
items = response['Items']
while 'LastEvaluatedKey' in response:
query_params.update({'ExclusiveStartKey': response['LastEvaluatedKey']})
response = dynamodb_table.query(**query_params)
items.extend(response['Items'])
return items
@staticmethod
def validate_topic(dynamodb, topic_name):
topic_table = dynamodb.Table(os.environ['TOPIC_TABLE_NAME'])
query_params = {
'IndexName': 'index_hash_key-order-index',
'KeyConditionExpression': Key('index_hash_key').eq(settings.TOPIC_INDEX_HASH_KEY)
}
topics = topic_table.query(**query_params)['Items']
if topic_name not in [topic['name'] for topic in topics]:
raise ValidationError('Bad Request: Invalid topic')
return True
@staticmethod
def validate_user_existence_in_thread(dynamodb, replyed_user_id, parent_comment_id):
comment_table = dynamodb.Table(os.environ['COMMENT_TABLE_NAME'])
query_params = {
'IndexName': 'parent_id-sort_key-index',
'KeyConditionExpression': Key('parent_id').eq(parent_comment_id)
}
thread_comments = comment_table.query(**query_params)['Items']
thread_user_ids = [comment['user_id'] for comment in thread_comments]
parent_comment = comment_table.get_item(Key={'comment_id': parent_comment_id})['Item']
if replyed_user_id not in thread_user_ids + [parent_comment['user_id']]:
raise ValidationError("Bad Request: {replyed_user_id} doesn't exist in thread"
.format(replyed_user_id=replyed_user_id))
return True