add endpoint for queueing passes, add PassValidator and PassQueue
This commit is contained in:
parent
ff7653b3fe
commit
2c9ebaa1cc
|
@ -1,7 +1,10 @@
|
||||||
from flask import Flask
|
from flask import Flask, request
|
||||||
import psycopg2 as psql
|
import psycopg2 as psql
|
||||||
from os import environ as env
|
from os import environ as env
|
||||||
import datetime
|
import datetime
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import uuid
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -15,6 +18,34 @@ db_params = {
|
||||||
|
|
||||||
db = psql.connect(**db_params)
|
db = psql.connect(**db_params)
|
||||||
|
|
||||||
|
class PassQueue:
|
||||||
|
def __init__(self):
|
||||||
|
self.queue = []
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
while True:
|
||||||
|
if not self.queue:
|
||||||
|
time.sleep(1)
|
||||||
|
continue
|
||||||
|
|
||||||
|
p = self.queue.pop(0)
|
||||||
|
c = db.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
c.execute("BEGIN")
|
||||||
|
c.execute(f"INSERT INTO public.pass (uuid, movie_uuid, user_first, user_last, user_email, pass_type, pass_price)"
|
||||||
|
f"VALUES ('{uuid.uuid4().hex}', '{p.movie_uuid}', '{p.first}', '{p.last}', '{p.email}', {p.type}, {p.price})")
|
||||||
|
c.execute("COMMIT")
|
||||||
|
db.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
c.execute("ROLLBACK")
|
||||||
|
db.rollback()
|
||||||
|
|
||||||
|
pq = PassQueue()
|
||||||
|
pq_thread = threading.Thread(target = pq.start, args = [])
|
||||||
|
pq_thread.start()
|
||||||
|
|
||||||
@app.route("/movies", methods = ['GET'])
|
@app.route("/movies", methods = ['GET'])
|
||||||
def movies():
|
def movies():
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
|
@ -120,3 +151,62 @@ def passes():
|
||||||
else v.strftime("%Y-%m-%d %H:%M:%S")
|
else v.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
for i, v in enumerate(r)}
|
for i, v in enumerate(r)}
|
||||||
for r in res]}
|
for r in res]}
|
||||||
|
|
||||||
|
class PassValidator:
|
||||||
|
def __init__(self):
|
||||||
|
self.first = None
|
||||||
|
self.last = None
|
||||||
|
self.email = None
|
||||||
|
self.type = None
|
||||||
|
self.price = None
|
||||||
|
self.movie_uuid = None
|
||||||
|
|
||||||
|
def validate(self):
|
||||||
|
if not self.first:
|
||||||
|
return "First name missing"
|
||||||
|
elif not self.last:
|
||||||
|
return "Last name missing"
|
||||||
|
elif not self.email:
|
||||||
|
return "Email missing"
|
||||||
|
elif not self.type:
|
||||||
|
return "Pass type missing"
|
||||||
|
elif not self.price:
|
||||||
|
return "Pass price missing"
|
||||||
|
elif not self.movie_uuid:
|
||||||
|
return "Movie uuid missing"
|
||||||
|
|
||||||
|
def queue(self):
|
||||||
|
pq.queue.append(self)
|
||||||
|
|
||||||
|
def fill_pass_from_form(p, f):
|
||||||
|
print(f)
|
||||||
|
|
||||||
|
def fill_pass_from_json(p, j):
|
||||||
|
fields = ("first",
|
||||||
|
"last",
|
||||||
|
"email",
|
||||||
|
"type",
|
||||||
|
"price",
|
||||||
|
"movie_uuid")
|
||||||
|
|
||||||
|
for i in fields:
|
||||||
|
exec(f"p.{i} = j.get('{i}')")
|
||||||
|
|
||||||
|
@app.route("/apply_for_pass", methods = ['POST'])
|
||||||
|
def apply_for_pass():
|
||||||
|
p = PassValidator()
|
||||||
|
|
||||||
|
if request.form:
|
||||||
|
fill_pass_from_form(p, request.form)
|
||||||
|
elif request.is_json:
|
||||||
|
fill_pass_from_json(p, request.json)
|
||||||
|
else:
|
||||||
|
return {"error": "only form and json interfaces are supported", "result": None}, 403
|
||||||
|
|
||||||
|
v = p.validate()
|
||||||
|
|
||||||
|
if not v:
|
||||||
|
p.queue()
|
||||||
|
return {"error": None, "result": "OK"}
|
||||||
|
else:
|
||||||
|
return {"error": v, "result": None}
|
||||||
|
|
Loading…
Reference in New Issue