Ejemplo n.º 1
0
async def customers_update(uid: str, item: Customer):
    item.updated_at = get_now()
    item.uid = uid
    db.collection("customers").document(uid).update(
        item.dict(exclude={"id", "uid", "created_at"}))

    return item
Ejemplo n.º 2
0
async def customers_create(item: Customer):
    uid = get_uid()
    item.uid = uid
    db.collection("customers").document(uid).set(
        item.dict(exclude={"id", "uid"}))

    return item
Ejemplo n.º 3
0
def tv():
    """ check if tv is on and who has control """
    from datetime import datetime, timedelta
    data = get_tv_data()
    data_ref = db.collection(u'tv_control').document(u'data')

    # who has control, if anyone
    controller = data['controller']
    print('controller is', controller)

    # get current time
    now = datetime.now()
    # has control expired
    last_str = data['last_watched']
    # deserialize last watched date
    last_watched = datetime.strptime(last_str, "%d-%b-%Y (%H:%M:%S.%f)")
    # if control has expired
    if now > last_watched + timedelta(hours=1):
        controller = 'No one'
        # save change to controller
        data_ref.set({u'controller': 'No one'}, merge=True)
        data_ref.set({u'last_watched': now.strftime("%d-%b-%Y (%H:%M:%S.%f)")},
                     merge=True)

    return jsonify({'controller': controller, 'tv_status': data['tv_status']})
Ejemplo n.º 4
0
async def customers_list(limit: Optional[int] = Query(10, title="Page size"),
                         page: Optional[int] = Query(1, title="Display page")):
    offset = (page - 1) * limit
    _ref = db.collection("customers").limit(limit).offset(offset)

    return Collection(
        items=[Customer.from_firebase(doc) for doc in _ref.stream()],
        meta=CollectionMeta(limit=limit, page=page))
Ejemplo n.º 5
0
def power():
    """ toggle power on tv, potentially changing who has control """
    from datetime import datetime, timedelta
    # get action and viewer from clientr
    viewer = request.json['viewer']
    action = request.json['action']

    #get data from database
    data = get_tv_data()
    data_ref = db.collection(u'tv_control').document(u'data')
    # is tv on or off
    tv_status = action
    data_ref.set({u'tv_status': tv_status}, merge=True)
    # who has control, if anyone
    controller = data['controller']
    print('controller is', controller, 'and tv is being turned', action, 'by',
          viewer)

    # get current time
    now = datetime.now()
    # has control expired
    last_str = data['last_watched']
    # deserialize last watched date
    last_watched = datetime.strptime(last_str, "%d-%b-%Y (%H:%M:%S.%f)")
    # if control has expired
    if now > last_watched + timedelta(hours=1):
        controller = 'No one'
        # save change to controller
        data_ref.set({u'last_watched': now.strftime("%d-%b-%Y (%H:%M:%S.%f)")},
                     merge=True)
        data_ref.set({u'controller': 'No one'}, merge=True)
    if controller == viewer:
        # update last_watched time
        data_ref.set({u'last_watched': now.strftime("%d-%b-%Y (%H:%M:%S.%f)")},
                     merge=True)
        if action == 'off':
            # give up control
            controller = 'No one'
            data_ref.set({u'controller': 'No one'}, merge=True)
    if controller == 'No one' and action == 'on':
        # you gain control
        controller = viewer
        data_ref.set({u'controller': viewer}, merge=True)
        data_ref.set({u'last_watched': now.strftime("%d-%b-%Y (%H:%M:%S.%f)")},
                     merge=True)

    return controller
Ejemplo n.º 6
0
async def orders_delete(uid: str):
    db.collection("orders").document(uid).delete()
    return None
Ejemplo n.º 7
0
async def orders_get(uid: str):
    return Order.from_firebase(db.collection("orders").document(uid).get())
Ejemplo n.º 8
0
def get_tv_data():
    """ get data for tv control game """
    control_ref = db.collection(u'tv_control').document(u'data')
    doc = control_ref.get()
    print(doc.to_dict())
    return doc.to_dict()
Ejemplo n.º 9
0
async def accounts_list():
    _ref = db.collection("accounts")

    return Collection(
        items=[Account.from_firebase(doc) for doc in _ref.stream()],
        meta=CollectionMeta())
Ejemplo n.º 10
0
async def customers_delete(uid: str):
    db.collection("customers").document(uid).delete()
    return None
Ejemplo n.º 11
0
async def customers_get(uid: str):
    return Customer.from_firebase(
        db.collection("customers").document(uid).get())