-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
69 lines (58 loc) · 1.77 KB
/
views.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
# app.py
import os
from bson import ObjectId
from datetime import datetime
from pymongo import MongoClient
from flask import (
Flask,
request,
redirect,
url_for,
flash,
session,
abort,
jsonify,
send_file,
current_app,
Blueprint,
)
from werkzeug.utils import secure_filename
import multiprocessing
from server.constants import (
IMAGE_SUBMISSION_PATHS,
# AUDIO_ARCHIVE_PATH,
)
from server.slack import (
send_slack_moderation_messages,
slack_message_actions,
)
ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg", "gif"])
api = Blueprint('api', __name__, url_prefix='/api/v1')
# SLACK INTERACTION ENDPOINTS
@api.route("/slack/message_actions", methods=["POST"])
def message_actions():
return slack_message_actions(request)
@api.route("/upload", methods=["POST"])
def handle_incoming_photo():
image = request.files.get("file")
mongo = MongoClient(current_app.config["DB"])
submissions = mongo["youwont"]["submissions"]
download_codes = mongo["youwont"]["download_codes"]
extension = image.filename.split(".")[-1]
ts = datetime.now().timestamp()
filename = "image_{}.{}".format(ts, extension)
image_path = os.path.join(IMAGE_SUBMISSION_PATHS["PENDING"], secure_filename(filename))
image.save(image_path)
mongo_result = submissions.insert_one({
"filename": filename,
"added": datetime.now(),
"status": "PENDING",
})
download_code_result = download_codes.insert_one({
"valid": True
})
download_code = str(download_code_result.inserted_id)
_id = mongo_result.inserted_id
p = multiprocessing.Process(target=send_slack_moderation_messages, args=(image_path, _id,))
p.start()
return jsonify({"message": "success", "download_code": download_code})