-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerchants.py
62 lines (55 loc) · 2.31 KB
/
merchants.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
''' merchants.py
Description of the purpose of this file
'''
from flask import jsonify, request
from flask_restful import Resource
from helpers import decorate_all_methods, return_status
import json
from db import database
from db import MerchantsSchema
from marshmallow import ValidationError
from firebase_admin import auth
from functools import wraps
def check_headers(func):
@wraps(func)
def wrapper(*args, **kw):
assert 'Authorization' in request.headers, 'Missing Authorization header'
decoded_token = auth.verify_id_token(request.headers['Authorization'])
uid = decoded_token['uid']
return func(*args, uid=uid, **kw)
return wrapper
# Merchants RESTful resource Controller
@decorate_all_methods(return_status)
@decorate_all_methods(check_headers)
class Merchant(Resource):
# Check if merchant doc exist
def get(self, uid=None):
doc_ref = database.collection(u'merchants').document(uid)
doc = doc_ref.get()
doc_dict = doc.to_dict()
try:
if doc_dict is None or 'paymentInfo' not in doc_dict:
return {'docExists':str(doc.exists), 'docPopulated':'false'}
result = MerchantsSchema().load(doc_dict['paymentInfo'])
except ValidationError as err:
return {'docExists':str(doc.exists), 'docPopulated':'false'}
return {'docExists':str(doc.exists), 'docPopulated':'true'}
# Create a merchant doc
def post(self, uid=None):
print("merchants request: ",request.data)
result = MerchantsSchema().load(json.loads(request.data))
doc_ref = database.collection(u'merchants').document(uid)
doc_ref.set({'paymentInfo':result, 'totalTips':'0'})
def delete(self, uid=None):
doc_ref = database.collection(u'merchants').document(uid)
doc_ref.delete()
def put(self, uid=None):
result = MerchantsSchema().load(json.loads(request.data))
doc_ref = database.collection(u'merchants').document(uid)
doc_ref.delete()
doc_ref.set({'paymentInfo':result, 'totalTips':'0'})
def patch(self, uid=None):
in_json = json.loads(request.data)
assert in_json <= list(MerchantsSchema().__dict__.keys()), 'Invalid keys'
doc_ref = database.collection(u'merchants').document(uid)
doc_ref.update({'paymentInfo':in_json})