-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.py
More file actions
261 lines (224 loc) · 7.75 KB
/
handler.py
File metadata and controls
261 lines (224 loc) · 7.75 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
WarriorBeatGraphQL
GraphQL Resolvers
"""
try:
import unzip_requirements
except ImportError:
pass
import uuid
from datetime import datetime
from itertools import chain
import requests
from bs4 import BeautifulSoup
from data import DynamoDB, S3Storage
from slugify import slugify
def get_utc_now():
"""returns UTC now with zulu suffix"""
utc_now = datetime.utcnow()
utc_zulu = utc_now.strftime(
'%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
return utc_zulu
def handle_media_delete(*args, **kwargs):
"""
Deletes file from S3 and DynamoDB
Returns Media
"""
media_table = DynamoDB('media')
media_s3 = S3Storage('media')
media_id = kwargs.get("id")
media_obj = media_table.get_item(media_id)
media_s3_key = media_obj['key']
media_s3.delete(media_s3_key)
media_table.delete_item(media_id)
return kwargs
def handle_media_create(*args, **kwargs):
"""
Creates file in S3 from source
Create media item entry in database
returns Media
"""
media_table = DynamoDB('media')
media_s3 = S3Storage('media')
create_date = get_utc_now()
input_args = kwargs.get("media")
media = {
'id': str(uuid.uuid4()),
'authorId': input_args.get('authorId'),
'source': input_args.get('source'),
'credits': input_args.get('credits', ''),
'caption': input_args.get('caption', ''),
'createdOn': create_date,
'lastUpdated': create_date,
'key': '',
'url': ''
}
base_key = f"{media['authorId']}_{media['id']}"
media['url'], media['key'] = media_s3.upload_from_url(
media['source'], key=base_key)
media_table.add_item(media)
return media
def handle_article_by_category(*args, **kwargs):
"""Returns Articles sorted by Category"""
article_table = DynamoDB('article')
category = kwargs.get("categoryId")
articles = [
art for art in article_table.all if category in art['categories']]
return articles
def handle_article_like(*args, **kwargs):
"""handles user liking article, returns article"""
post_id = kwargs.get('id')
user_id = kwargs.get('userId')
likes_table = DynamoDB("article_likes")
article_table = DynamoDB("article")
article = article_table.get_item(post_id)
query_filter = ("postId", post_id)
likes = likes_table.query(user_id, key="userId",
filters=query_filter, index="user-index")
if any(likes):
like = likes[0]
likes_table.delete_item(like['id'])
return article
like = {
"id": str(uuid.uuid4()),
"postId": post_id,
"userId": user_id
}
likes_table.add_item(like)
return article
def handle_slug(*args, **kwargs):
"""returns slugified version of toSlug kwargs"""
text = kwargs.get("toSlug")
slug = slugify(text, lowercase=True, separator='_')
return slug
def handle_paginate(*args, **kwargs):
"""returns paginated resource scan"""
resource = kwargs.get("typeResource")
sort_by = kwargs.get("sortOrder", None)
limit = kwargs.get("limit", 20)
next_token = kwargs.get("nextToken", None)
table = DynamoDB(resource)
items = table.scan(limit=limit, next_token=next_token)
if sort_by:
key = sort_by.get('key')
values = sort_by.get('values')
sort_key = list(map(lambda x: values.index(
x[key]) if x[key] in values else len(items)+1, items))
items = [i for _, i in sorted(
zip(sort_key, items), key=lambda p: p[0])]
return {"items": items}
def handle_author_title(*args, **kwargs):
"""returns formatted Author role"""
title = kwargs.get("roles")
ignored_roles = ['administrator', 'author', 'contributor',
'customer', 'editor', 'shop_manager', 'subscriber']
titles = [t for t in title if not t in ignored_roles]
for t in titles:
t = t.split('_')
t = [i.capitalize() for i in t]
title = " ".join(t)
return title
def handle_author_subscribe(*args, **kwargs):
"""handles user sub to author, returns author"""
author_id = kwargs.get('id')
user_id = kwargs.get('userId')
subs_table = DynamoDB('user_subs')
author_table = DynamoDB('author')
author = author_table.get_item(author_id)
query_filter = ("authorId", author_id)
subs = subs_table.query(user_id, key="userId",
filters=query_filter, index="user-index")
if any(subs):
subscription = subs[0]
subs_table.delete_item(subscription['id'])
return author
subscription = {
"id": str(uuid.uuid4()),
"userId": user_id,
"authorId": author_id
}
subs_table.add_item(subscription)
return author
def handle_poll_has_voted(*args, **kwargs):
"""checks if user has voted on poll, returns PollOption"""
user_id = kwargs.get("userId")
poll = kwargs.get("poll")
options_table = DynamoDB("poll_options")
votes_table = DynamoDB("poll_votes")
options = options_table.query(
poll['id'], key="pollId", index="pollId-index")
option_ids = [opt.get('id') for opt in options]
user_votes = list(chain.from_iterable([votes_table.query(
user_id, key="userId", range_key=('optionId', i), index="userId-index") for i in option_ids]))
print("User Votes:", user_votes)
if len(user_votes) >= 1:
voted_option = next(
i for i in options if i['id'] == user_votes[0]['optionId'])
print("Voted Option:", voted_option)
return voted_option
return None
def handle_get_about_meta(*args, **kwargs):
"""Returns About Us Meta Info"""
url = "https://ogwarriorbeat.com/about/"
page = requests.get(url)
html = BeautifulSoup(page.content, 'html.parser')
content = html.find("div", class_="postarea")
[s.extract() for s in content('h1')]
cont = content.find('a').parent
cont.extract()
return {
"key": "about",
"content": str(content)
}
def handle_get_meta(*args, **kwargs):
"""Handles Meta Info"""
meta_resolver = {
'about': handle_get_about_meta
}
key = kwargs.get("key")
resolve = meta_resolver[key]
return resolve(*args, **kwargs)
def handle_user_likes(*args, **kwargs):
"""returns liked user articles"""
user_id = kwargs.get("id")
likes_table = DynamoDB("article_likes")
article_table = DynamoDB("article")
likes = likes_table.query(user_id, key="userId", index="user-index")
if not any(likes):
return []
posts = [i.get('postId') for i in likes]
articles = [article_table.get_item(i) for i in posts]
return articles
def handle_user_subscriptions(*args, **kwargs):
"""returns subscribed authors"""
user_id = kwargs.get("id")
subs_table = DynamoDB("user_subs")
authors_table = DynamoDB("author")
subs = subs_table.query(user_id, key="userId", index="user-index")
if not any(subs):
return []
author_ids = [i.get('authorId') for i in subs]
authors = [authors_table.get_item(i) for i in author_ids]
return authors
resolvers = {
'mediaCreate': handle_media_create,
'mediaDelete': handle_media_delete,
'articleGetByCategory': handle_article_by_category,
'articleLike': handle_article_like,
'category_slug': handle_slug,
'categoryList': handle_paginate,
'author_title': handle_author_title,
'authorSubscribe': handle_author_subscribe,
'poll_hasVoted': handle_poll_has_voted,
'resolveMeta': handle_get_meta,
'user_likes': handle_user_likes,
'user_subscriptions': handle_user_subscriptions,
}
def lambda_handler(event, context):
print("Got an Invoke Request")
print(f"Event: {event}")
print(f"Context: {context.__dict__}")
field = event['field']
args = event['args']
resolve = resolvers[field]
return resolve(**args)