예제 #1
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)
예제 #2
0
def getUserFromRequest(request):

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

    # retrieve current user and current family
    user_ref = db.collection(u'users').document(uid)
    user = User.from_dict(user_ref.get().to_dict())

    return uid, user
예제 #3
0
def signUp():

    id_token = request.headers['Authorization'].split(' ').pop()
    data = request.get_json()

    # retrieve and decode the users token
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid']

    # ad an empty list of items to the user object
    # data['items'] = []

    # remove the token from the user object
    user = User(data['name'], data['email'])
    dp_status = data['image']

    if dp_status == "true":
        user.image = "true"

    # add the user to the db, index by their UID
    db.collection(u'users').document(uid).set(user.to_dict())

    return str(data)
예제 #4
0
def getAllFamilies():
    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_list = list(user.families)

    family_dict = {}

    for token in family_token_list:
        family_ref = db.collection(u'families').document(token)

        family_dict[token] = family_ref.get().to_dict()

    return str(family_dict)
예제 #5
0
def getFamilyToken():

    # Capture request data and extract user id from 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
    doc_ref = db.collection(u'users').document(uid)
    # build the user object
    doc = doc_ref.get()
    user = User.from_dict(doc.to_dict())

    # if the token exists, return it. Otherwise return a 404
    token = user.currentfamily
    if token:
        return str(token)
    else:
        return "Error: user is not part of a family.", status.HTTP_404_NOT_FOUND
예제 #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())
예제 #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())