Ejemplo n.º 1
0
def transferItem():
    data = request.get_json()
    item_token = data['item_token']
    _, user = getUserFromRequest(request)

    item_token = data['item_token']
    item_ref = db.collection(u"items").document(item_token)
    old_family_ref = db.collection(u"families").document(user.currentfamily)
    new_family_ref = db.collection(u"families").document(data["family_token"])

    old_family = Family.from_dict(old_family_ref.get().to_dict())
    new_family = Family.from_dict(new_family_ref.get().to_dict())
    item = Item.from_dict(item_ref.get().to_dict())

    item.visibility = ["global"]

    old_family.items.remove(item_token)
    new_family.items.append(item_token)

    batch = db.batch()

    batch.set(item_ref, item.to_dict())
    batch.set(old_family_ref, old_family.to_dict())
    batch.set(new_family_ref, new_family.to_dict())

    batch.commit()

    return str(item.to_dict())
Ejemplo n.º 2
0
def getFamilyMembers():

    # extract uid from auth token
    id_token = request.headers['Authorization'].split(' ').pop()
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid']

    if not uid:
        return status.HTTP_401_UNAUTHORIZED

    # retrieve user from database
    user_ref = db.collection(u'users').document(uid)
    user = User.from_dict(user_ref.get().to_dict())

    # retrieve current family from database
    family_token = user.currentfamily
    family_ref = db.collection(u'families').document(family_token)
    family = Family.from_dict(family_ref.get().to_dict())

    # construct a list of family members and return it
    member_ids = family.members

    family_member_dict = {}

    # TODO filter out family membership field from other family members

    for member_id in member_ids:
        member_ref = db.collection(u'users').document(member_id)
        member = User.from_dict(member_ref.get().to_dict())
        family_member_dict[member_id] = member.to_dict()

    return str(family_member_dict)
Ejemplo n.º 3
0
def getFamilyInfo():

    data = request.get_json()
    family_token = data['family_token']
    uid, user = getUserFromRequest(request)

    if not uid:
        return status.HTTP_401_UNAUTHORIZED

    family_ref = db.collection(u'families').document(family_token)
    family = Family.from_dict(family_ref.get().to_dict())

    resp = {}
    resp['name'] = family.name
    resp['family_token'] = family_token

    names = []
    for member in family.members:
        u_ref = db.collection('users').document(member)
        name = u_ref.get().to_dict()['name']

        names.append(name)

    resp['members'] = names

    return str(resp)
Ejemplo n.º 4
0
def getCurrentFamily(user):
    current_family_token = user.currentfamily
    current_family_ref = db.collection(u'families').document(
        current_family_token)
    current_family = Family.from_dict(current_family_ref.get().to_dict())

    return current_family
Ejemplo n.º 5
0
def addItem():

    # retrieve request data and user ID from request
    data = request.get_json()

    _, user = getUserFromRequest(request)

    current_family = user.currentfamily
    family_ref = db.collection(u'families').document(current_family)
    family = Family.from_dict(family_ref.get().to_dict())

    # create the item object from the request data and
    # create its database reference
    item = Item.from_dict(data)
    item_ref = db.collection(u'items').document()

    batch = db.batch()

    batch.set(item_ref, item.to_dict())

    # item_ref.set(item.to_dict())

    family.items.append(item_ref.id)

    batch.set(family_ref, family.to_dict())
    # family_ref.set(family.to_dict())

    batch.commit()

    return str(item.to_dict())
Ejemplo n.º 6
0
def joinFamily():

    # capture request data and extract uid from token
    data = request.get_json()
    id_token = request.headers['Authorization'].split(' ').pop()
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid']
    if not uid:
        return status.HTTP_401_UNAUTHORIZED
    # extract family identifier from request
    family_token = data['family_token']

    # find family in the database. If the family does not exist,
    # return a not_found status code.
    family_ref = db.collection(u'families').document(family_token)
    doc = family_ref.get()

    if not doc.exists:
        return "Family not found!", status.HTTP_404_NOT_FOUND

    family = Family.from_dict(doc.to_dict())
    user_ref = db.collection(u'users').document(uid)
    # add the family id to the users list of families.
    user = User.from_dict(user_ref.get().to_dict())
    user_families = user.families
    user_families.append(family_token)

    # add the user to the family's list of members.
    members = family.members
    members.append(uid)

    # commit changes to the database in a batch
    batch = db.batch()
    batch.update(user_ref, {
        'currentfamily': family_token,
        'families': user_families
    })
    batch.update(family_ref, {'members': members})
    batch.commit()

    return str(family.to_dict())
Ejemplo n.º 7
0
def leaveFamily():

    data = request.get_json()
    id_token = request.headers['Authorization'].split(' ').pop()
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid']

    user_ref = db.collection(u'users').document(uid)

    user = User.from_dict(user_ref.get().to_dict())

    family_token = data['family_token']

    family_ref = db.collection(u'families').document(family_token)
    family = Family.from_dict(family_ref.get().to_dict())
    family.members.remove(uid)

    user.families.remove(family_token)

    if user.currentfamily == family_token:
        user.currentfamily = user.families[0]

    batch = db.batch()

    batch.set(family_ref, family.to_dict())
    batch.set(user_ref, user.to_dict())

    batch.commit()

    if len(family.members == 0):
        for item in family.items:
            item_ref = db.collection(u'items').document(item)
            item_ref.delete()
        family_ref.delete()

    return str(family.to_dict())
Ejemplo n.º 8
0
def deleteItem():

    data = request.get_json()
    item_id = data['item']
    _, user = getUserFromRequest(request)

    current_family = user.currentfamily

    family_ref = db.collection(u'families').document(current_family)
    item_ref = db.collection(u'items').document(item_id)

    # construct the family object
    family = Family.from_dict(family_ref.get().to_dict())

    family_item_list = family.items

    family_item_list.remove(item_id)

    item_field = {"items": family_item_list}

    family_ref.update(item_field)
    item_ref.delete()

    return str(item_field)
Ejemplo n.º 9
0
def createFamily():
    # capture request data and retrieve uid from token
    data = request.get_json()

    id_token = request.headers['Authorization'].split(' ').pop()
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid']
    if not uid:
        return status.HTTP_401_UNAUTHORIZED

    # retrieve the users current list of families
    user_data = db.collection(u'users').document(uid).get()
    user_data = user_data.to_dict()
    current_families = user_data['families']

    # create family object
    name = data['name']
    family = Family(name)

    family.members = [uid]
    family_token = secrets.token_urlsafe(8)
    family.token = family_token
    current_families.append(family_token)

    # batched write to database
    batch = db.batch()
    user_ref = db.collection(u'users').document(uid)
    batch.update(user_ref, {
        u'currentfamily': family_token,
        u'families': current_families
    })

    family_ref = db.collection(u'families').document(family_token)

    batch.set(family_ref, family.to_dict())

    batch.commit()

    return str(family.to_dict())