def get_member(cls: Type[MemberType],
                   userEmail: str,
                   userId: str,
                   db: firestore.firestore.Client,
                   readonly=False) -> MemberType:
        members_ref: firestore.firestore.DocumentReference = db.collection(
            "members").document(userEmail)
        member_snapshot: firestore.firestore.DocumentSnapshot = members_ref.get(
        )
        if not member_snapshot.exists:
            if not readonly:
                raise Exception("Email does not belong to HODP member")
            else:
                return None

        member_email = members_ref.id
        member_dict = member_snapshot.to_dict()

        member = cls(member_email, member_dict)

        if not readonly:
            if member.id is None:
                member.id = userId
                members_ref.update({"id": member.id})
            elif member.id != userId:
                raise Exception("id in databasae does not match current id")

        return member
예제 #2
0
def conduct_lottery(db : firestore.firestore.Client, count = 1, month = None, 
                        allow_repeats = False, default_entries = 1, require_demographics = True) -> List[str]:
    emails_ref = db.collection("emails")
    emails_docs : Iterable[firestore.firestore.DocumentSnapshot]= emails_ref.stream()
    entries : List[str] = []

    for doc in emails_docs:
        doc_dict = doc.to_dict()
        if require_demographics and not doc_dict.get("has_demographics", False):
            continue
        entries.extend([doc.id] * default_entries)
        if month is not None:
            doc_dict : dict = doc.to_dict()
            monthly_responses = doc_dict.get("total_responses", 0)
            entries.extend([doc.id] * monthly_responses)
    
    winners = []
    
    for _ in range(count):
        winner = random.choice(entries)
        if not allow_repeats:
            entries = [entry for entry in entries if entry != winner]
        winners.append(winner)

    return winners
예제 #3
0
def create_prediction_user(userEmail : str, userId : str, db : firestore.firestore.Client) -> firestore.firestore.DocumentSnapshot:
    prediction_users_ref = db.collection("prediction_users")
    user_info_ref = prediction_users_ref.document(userEmail)
    user_info_doc = user_info_ref.get()
    if userEmail is None:
        raise Exception("User email not defined")
    if userId is None:
        raise Exception("User ID not defined")
    # user already exists
    if is_authenticated(userEmail, userId, prediction_users_ref):
        return user_info_doc
    # user doesn't exist, have to create new
    else:
        if user_info_doc.exists:
            user_info_ref.update({
                u"id" : userId,
            })
        else:
            user_info_ref.set({
                u"id" : userId,
                u"current_score" : 0,
                u"date_created" : datetime.datetime.now(),
                u"can_display" : None
            })
        return user_info_ref.get()
 def save(self, db: firestore.firestore.Client):
     member_ref: firestore.firestore.DocumentReference = db.collection(
         "members").document(self.email)
     update_dict = {
         "full_name": self.full_name,
         "house": self.house,
         "contributions": self.contributions,
         "year": self.year,
         "description": self.description,
         "img_url": self.img_url
     }
     member_ref.update(update_dict)
예제 #5
0
def create_respondent(userEmail : str, userId : str, db : firestore.firestore.Client) -> firestore.firestore.DocumentSnapshot:
    emails_ref = db.collection("emails")
    responses_ref = db.collection("responses")
    user_response_ref = responses_ref.document(email_hash(userEmail))
    user_response_doc = user_response_ref.get()
    if userEmail is None:
        raise Exception("User email not defined")
    if userId is None:
        raise Exception("User ID not defined")
    # user already exists
    if is_authenticated(userEmail, userId, emails_ref):
        if not user_response_doc.exists:
            # only create responses doc if it doesn't already exist
            responses_ref.document(email_hash(userEmail)).set({
            u"demographics" : {}
        })
        return emails_ref.document(userEmail).get()
    # user doesn't exist, have to create everything
    else: 
        user_email_ref = emails_ref.document(userEmail)
        user_email_doc = user_email_ref.get()
        if user_email_doc.exists:
            user_email_ref.update({
                u"id" : userId,
            })
        else:
            user_email_ref.set({
                u"id" : userId, 
                u"has_demographics" : False,
                u"monthly_responses" : {},
                u"total_responses" : 0, 
                u"date_created" : datetime.datetime.now()
            })
        if not user_response_doc.exists:
            responses_ref.document(email_hash(userEmail)).set({
            u"demographics" : {}
        })

        return emails_ref.document(userEmail).get()
def add_members_to_firestore(db: firestore.firestore.Client,
                             peopleYml: List[dict]):
    members_ref = db.collection("members")
    people = peopleYml["people"]
    for person in people:
        if "email" in person:
            email = person["email"]
            name = person["name"]
            member_ref: firestore.firestore.DocumentReference = members_ref.document(
                email)
            member_doc: firestore.firestore.DocumentSnapshot = member_ref.get()
            if not member_doc.exists:
                member_ref.set({"full_name": name})
예제 #7
0
def get_responses_dict(userEmail : str, db : firestore.firestore.Client):
    responses_ref = db.collection("responses")
    return responses_ref.document(email_hash(userEmail)).get().to_dict()
예제 #8
0
def get_emails_dict(userEmail : str, db : firestore.firestore.Client):
    emails_ref = db.collection("emails")
    return emails_ref.document(userEmail).get().to_dict()
예제 #9
0
def get_all_template_paths(db : firestore.firestore.Client):
    template_dict = db.collection("responses").document(document_id="template").get().to_dict()
    return get_all_paths(template_dict)
예제 #10
0
def get_predictions_dict(userEmail : str, db : firestore.firestore.Client):
    predictions_ref = db.collection("prediction_users").document(userEmail).collection("predictions")
    docs = predictions_ref.stream()
    docs_dict = {doc.id : int(doc.to_dict()["guess"]) for doc in docs}
    return docs_dict