Ejemplo n.º 1
0
def remove_players_from_room(room_id, users_ids):
    """Remove one or many players from a room

    Attributes:
        room_id -- Id of the room document
        users_ids -- Array of user id
    """
    room_id = room_id.lower()
    doc = db.collection("rooms").document(room_id).get()

    if not doc.exists:
        raise DocumentWasNotFoundError("rooms", room_id)

    try:
        batch = db.batch()
        users_ids_ref = []
        for user_id in users_ids:
            user_id = user_id.lower()

            room_ref = db.collection("rooms").document(room_id)
            batch.update(
                db.collection("users_rooms").document(user_id),
                {"rooms": firestore.ArrayRemove([room_ref])})
            users_ids_ref.append(db.collection("users").document(user_id))

        batch.update(
            db.collection("rooms_users").document(room_id), {
                "users": firestore.ArrayRemove(users_ids_ref),
                "admin": firestore.ArrayRemove(users_ids_ref)
            })
        batch.commit()
    except NotFound as ex:
        raise ValueError("At least one of the player was not found")
Ejemplo n.º 2
0
def delete_player_tile(game_id, player_id, tile):
    client = firestore.Client()
    tile_dict_list = [tile.to_dict()]

    player_secrets = client.document(
        f'game_state_secrets/{game_id}/player_secrets/{player_id}')
    player_secrets.update({'tiles': firestore.ArrayRemove(tile_dict_list)})
    def update_repos(self, pid, data):
        project = self._db.collection('projects').document(pid)

        print(data)

        if project.get().exists:
            if not self.__is_project_owner(project.get().to_dict(), self._uid):
                return None, status_code.OK

            action = data['repositories']['action']
            if action == 'update':
                project.update({
                    'repositories.Github':
                    firestore.ArrayUnion(data['repositories']['Github']),
                    'updated':
                    firestore.SERVER_TIMESTAMP
                })
            elif action == 'remove':
                project.update({
                    'repositories.Github':
                    firestore.ArrayRemove(data['repositories']['Github']),
                    'updated':
                    firestore.SERVER_TIMESTAMP
                })
            else:
                return 'missing action', status_code.BAD_REQUEST
            return None, status_code.OK
        else:
            return None, status_code.NOT_FOUND
Ejemplo n.º 4
0
def update_image_metadata(bucket_name, image_name):
    labels = request.get_json(force=True)
    doc_ref = db.collection('images').document(bucket_name).collection(
        'labels').document(image_name)
    doc = doc_ref.get()
    curr_label_groups = list(doc.get('labels'))
    curr_labels = [lab.get('group') for lab in doc.get('labels')]
    if len(labels) == 0:
        doc_ref.update({'valid': False})
    else:
        doc_ref.update({'valid': True})
    for label in labels:
        # each label looks like { groupName: 'group-name', value: val }
        if label['groupId'] in curr_labels:
            ind = curr_labels.index(label['groupId'])
            tmp = curr_label_groups.pop(ind)
            doc_ref.update({'labels': firestore.ArrayRemove([tmp])})
        doc_ref.update({
            'labels':
            firestore.ArrayUnion([{
                'group': label['groupId'],
                'values': label['value']
            }])
        })
    doc_ref.update({'seen': True, 'lastSeen': None})

    return {}
Ejemplo n.º 5
0
def delete_list_orphans(deploy_stage):
    orphaned = reverse_orphans(deploy_stage)
    for xp in orphaned:
        for status in orphaned[xp]:
            if len(orphaned[xp][status]) > 0:
                pool_ref = DB.collection(deploy_stage + "pool").document(xp)
                pool_ref.update(
                    {status: firestore.ArrayRemove(orphaned[xp][status])})
Ejemplo n.º 6
0
def delete_course_from_user(usr, dept, cno):
    course = get_course_by_bc_ref(dept, cno)
    refs = get_course_names_for_user(usr)
    for re in refs:
        if re[0] == course[0]['name']:
            ref = db.collection(u'users').document(usr)
            ref.update({
                u'courses': firestore.ArrayRemove([db.document('department/' + course[0]['dept'] + '/Courses/' + course[1])])
            })
Ejemplo n.º 7
0
def remove_follower(team_id, chat_id):
    print(f"Removing follower with {chat_id} from {team_id}.")
    team_document_ref = db.collection("teams").document(str(team_id))
    team_document_ref.set(merge=True,
                          document_data={
                              u"followers":
                              firestore.ArrayRemove([{
                                  "chat_id": chat_id
                              }])
                          })
Ejemplo n.º 8
0
def update_doc_array():
    db = firestore.Client()
    # [START fs_update_doc_array]
    city_ref = db.collection(u'cities').document(u'DC')

    # Atomically add a new region to the 'regions' array field.
    city_ref.update({u'regions': firestore.ArrayUnion([u'greater_virginia'])})

    # // Atomically remove a region from the 'regions' array field.
    city_ref.update({u'regions': firestore.ArrayRemove([u'east_coast'])})
    # [END fs_update_doc_array]
    city = city_ref.get()
    print(u'Updated the regions field of the DC. {}'.format(city.to_dict()))
Ejemplo n.º 9
0
def deal_tile_to_player(game_id, player_id, tile):
    client = firestore.Client()
    batch = client.batch()
    tile_dict_list = [tile.to_dict()]

    game_secrets = client.document(f'game_state_secrets/{game_id}')
    player_secrets = client.document(
        f'game_state_secrets/{game_id}/player_secrets/{player_id}')

    batch.update(game_secrets,
                 {'tiles': firestore.ArrayRemove(tile_dict_list)})
    batch.update(player_secrets,
                 {'tiles': firestore.ArrayUnion(tile_dict_list)})

    batch.commit()
Ejemplo n.º 10
0
def update_doc_array():
    db = firestore.Client()
    db.collection('cities').document('DC').set(
        City('Washington D.C.', None, 'USA', True, 680000,
             ['east_coast']).to_dict())

    # [START fs_update_doc_array]
    city_ref = db.collection('cities').document('DC')

    # Atomically add a new region to the 'regions' array field.
    city_ref.update({'regions': firestore.ArrayUnion(['greater_virginia'])})

    # // Atomically remove a region from the 'regions' array field.
    city_ref.update({'regions': firestore.ArrayRemove(['east_coast'])})
    # [END fs_update_doc_array]
    city = city_ref.get()
    print('Updated the regions field of the DC. {}'.format(city.to_dict()))
Ejemplo n.º 11
0
def update_doc_array():
    db = firestore.Client()
    db.collection(u'cities').document(u'DC').set(
        City(u'Washington D.C.', None, u'USA', True, 680000,
             [u'east_coast']).to_dict())

    # [START firestore_data_set_array_operations]
    city_ref = db.collection(u'cities').document(u'DC')

    # Atomically add a new region to the 'regions' array field.
    city_ref.update({u'regions': firestore.ArrayUnion([u'greater_virginia'])})

    # // Atomically remove a region from the 'regions' array field.
    city_ref.update({u'regions': firestore.ArrayRemove([u'east_coast'])})
    # [END firestore_data_set_array_operations]
    city = city_ref.get()
    print(f'Updated the regions field of the DC. {city.to_dict()}')
Ejemplo n.º 12
0
async def update_doc_array():
    db = firestore.AsyncClient()
    await db.collection("cities").document("DC").set(
        City("Washington D.C.", None, "USA", True, 680000,
             ["east_coast"]).to_dict())

    # [START firestore_data_set_array_operations_async]
    city_ref = db.collection("cities").document("DC")

    # Atomically add a new region to the 'regions' array field.
    await city_ref.update(
        {"regions": firestore.ArrayUnion(["greater_virginia"])})

    # // Atomically remove a region from the 'regions' array field.
    await city_ref.update({"regions": firestore.ArrayRemove(["east_coast"])})
    # [END firestore_data_set_array_operations_async]
    city = await city_ref.get()
    print(f"Updated the regions field of the DC. {city.to_dict()}")
Ejemplo n.º 13
0
def ticker_array_action(document: str,
                        ticker: str,
                        action: str,
                        data_type='tick',
                        user=None,
                        account=None):
    """Add or remove ticker from ticker list in firestore document

    Args:
        document: firestore document
        ticker: ticker
        action: remove or delete
        data_type: data type/resolution
        user: user
        account: IB account

    Returns:
        ticker list

    Raises:
        AssertionError
    """

    assert document in ['todo', 'doing', 'maintain', 'bad_contract']
    assert data_type in ['tick', '1s,' '30s']
    assert user in [None, 'jules', 'marcel']
    assert action in ['ADD', 'DEL']
    assert account in [None, 'A', 'B']

    doc_ref = FS.collection(u'universes').document(f'{document}')
    array_action = firestore.ArrayUnion(
        [ticker]) if action is 'ADD' else firestore.ArrayRemove([ticker])

    if document is 'doing':
        doc_ref.update(
            {f'{user}': {
                f'{account}': {
                    f'{data_type}': array_action
                }
            }})

    else:
        doc_ref.update({f'{data_type}': array_action})
Ejemplo n.º 14
0
def add_players_to_room(room_id, users_ids, admin_rights=False):
    """Add one or many players to a room

    Attributes:
        room_id -- Id of the room document
        users_ids -- Array of user id
        admin_rights -- Wether or not added users should have admin rights
    """
    room_id = room_id.lower()
    doc = db.collection("rooms").document(room_id).get()

    if not doc.exists:
        raise DocumentWasNotFoundError("rooms", room_id)

    try:
        batch = db.batch()
        users_ids_ref = []
        for user_id in users_ids:
            user_id = user_id.lower()

            room_ref = db.collection("rooms").document(room_id)
            batch.update(
                db.collection("users_rooms").document(user_id),
                {"rooms": firestore.ArrayUnion([room_ref])})
            users_ids_ref.append(db.collection("users").document(user_id))

        if admin_rights:
            batch.update(
                db.collection("rooms_users").document(room_id), {
                    "users": firestore.ArrayRemove(users_ids_ref),
                    "admin": firestore.ArrayUnion(users_ids_ref)
                })
        else:
            batch.update(
                db.collection("rooms_users").document(room_id),
                {"users": firestore.ArrayUnion(users_ids_ref)})

        batch.commit()
    except NotFound as ex:
        raise ValueError("At least one of the player was not found")
def create_job(request):
    """
    Create a new job and associate files to import with it

    :params table_name: Load job destination table name
    :type table_name: str
    :params region: Load job BigQuery region
    :type region: str
    """

    job_id = str(uuid.uuid4())
    table_name = request.args.get("table_name")
    region = request.args.get("region")

    db = firestore.Client(project=project_id)
    files = db.collection("jobs").document("new").get(["files"
                                                       ]).to_dict()["files"]

    if files is not None and len(files) > 0:
        db.collection("jobs").document(job_id).set(
            {
                "table_name": table_name,
                "region": region,
                "job_type": JobType.LOAD_JOB.value,
                "files": firestore.ArrayUnion(files),
                "status": JobState.CREATED.value,
            },
            merge=True,
        )
        db.collection("jobs").document("new").update(
            {"files": firestore.ArrayRemove(files)})
    else:
        logging.info("No files to import")
        job_id = None

    return (json.dumps({"job_id": job_id}), 200, headers)
    def update_collaborator(self, pid, collaborator, action):
        project = self._db.collection('projects').document(pid)

        if project.get().exists:

            if action == 'add':
                info, code = self._userModel.get_user_info_by_email(
                    collaborator)
                if code == status_code.NOT_FOUND:
                    return info, code
                elif info['uid'] in project.get().to_dict()['collaborator']:
                    info, code = {'msg': '協作者已存在!'}, status_code.BAD_REQUEST
                elif info['uid'] == self._uid:
                    info, code = {'msg': '您已是專案擁有者!'}, status_code.BAD_REQUEST
                else:
                    project.update({
                        'collaborator':
                        firestore.ArrayUnion([info['uid']]),
                        'updated':
                        firestore.SERVER_TIMESTAMP
                    })
                    code = status_code.OK
                return info, code
            elif action == 'remove':
                project.update({
                    'collaborator':
                    firestore.ArrayRemove([collaborator]),
                    'updated':
                    firestore.SERVER_TIMESTAMP
                })
                return {'msg': '移除成功!'}, status_code.OK
            else:
                return {'msg': 'miss action'}, status_code.BAD_REQUEST

        else:
            return {'msg': 'Project Not Found'}, status_code.NOT_FOUND
Ejemplo n.º 17
0
def update_player_cache(data, context):
    """Maintains document /caches/allPlayers by reacting to any write in the 'players' collection.

    :param data: event data
    :param context: event context
    :return: None
    """
    print('data')
    print(data)
    print('context')
    print(context)
    old = data['oldValue']
    new = data['value']
    db = firestore.Client()
    ref: firestore.DocumentReference = db.collection('caches').document(
        'allPlayers')
    batch: firestore.WriteBatch = db.batch()
    if old and old.get('fields'):
        old_player = to_player(old)
        batch.update(ref, {'objects': firestore.ArrayRemove([old_player])})
    new_player = to_player(new)
    batch.update(ref, {'objects': firestore.ArrayUnion([new_player])})
    print('new player:', new_player)
    batch.commit()
Ejemplo n.º 18
0
 def delete_ng_list(self, word):
     doc_ref = db.collection(self.collection_name).document("NG_LIST")
     doc_ref.set({
         "negative": firestore.ArrayRemove([word])
     }, merge=True)
Ejemplo n.º 19
0
def ListRemove(values):
    return firestore.ArrayRemove(values)
Ejemplo n.º 20
0
new_people_doc = db.collection(u'users').document()
new_people_doc.set({
    'first': 'Arthur',
    'middle': 'Camille',
    'last': 'Mauvezin',
    'born': 1993,
    'age': datetime.datetime.now().year - 1993,
    'likes': ['rugby', 'games']
})

# Add values in array if not present
new_people_doc.update(
    {'likes': firestore.ArrayUnion(['piano', 'rugby', 'loosing'])})

# Remove values from array
new_people_doc.update({'likes': firestore.ArrayRemove(['loosing'])})

# Increment field number
new_people_doc.update({'age': firestore.Increment(1)})

# Delete field from doc
new_people_doc.update({'born': firestore.DELETE_FIELD})

users_ref = db.collection(u'users')
docs = users_ref.stream()

for doc in docs:
    print(u'{} => {}'.format(doc.id, doc.to_dict()))

# Transaction example
transaction = db.transaction()
Ejemplo n.º 21
0
 def remove_application_user(self, application_name, user_email):
     # find the record 
     user = self.get_user(user_email, False)
     user.reference.update({"applications" : firestore.ArrayRemove([application_name])})
Ejemplo n.º 22
0
def delete_as_reader(task, user_id):
    case_tasks = client.collection('tasks').where(
        u'case_id', u'==', task.get('case_id')).stream()
    for task in case_tasks:
        task_ref = client.collection('tasks').document(task.id)
        task_ref.update({'readers': firestore.ArrayRemove([user_id])})
Ejemplo n.º 23
0
def completion(event, context):
    path_parts = context.resource.split('/documents/')[1].split('/')
    collection_path = path_parts[0]
    document_path = '/'.join(path_parts[1:])
    doc_ref = client.collection(collection_path).document(document_path)
    task_ref = client.collection(collection_path).document(path_parts[1])
    doc = doc_ref.get().to_dict()
    task = task_ref.get().to_dict()

    # get schema data
    case_ref = client.collection('schema').document('structure').collection(
        'cases').document(task['case_type'])
    case = case_ref.get().to_dict()

    stage_ref = client.collection('schema').document('structure').collection(
        'cases').document(task['case_type']).collection('stages').document(
            task['case_stage_id'])
    stage = stage_ref.get().to_dict()

    if doc.get('status') == 'complete':
        task_ref.update({
            'is_complete': True,
            'completionTime': firestore.SERVER_TIMESTAMP
        })
        # add completed task to algolia index
        add_to_algolia_index(client, task_ref)
        # upload to mongodb
        upload_to_mongodb(client, task_ref.get())

        # NEXT TASK
        if stage.get('next_task') and stage['next_task'] != '':
            next_stage_ref = client.collection('schema').document(
                'structure').collection('cases').document(
                    task['case_type']).collection('stages').document(
                        stage['next_task'])
            next_stage = next_stage_ref.get().to_dict()

            # --------
            # new task
            # --------
            new_task_id = uuid.uuid1().__str__()

            new_task_doc = {
                'title': next_stage['title'],
                'description': next_stage['description'],
                'type': 'interview',
                'assigned_users': [],
                'prefered_authors': [],
                'is_complete': False,
                'available': True,
                'case_id': task['case_id'],
                'case_stage_id': stage['next_task'],
                'case_type': task['case_type'],
                'debug_prev_task_id': path_parts[1],
                'debug_prev_stage_id': task['case_stage_id'],
                'debug_auto_created': True
            }
            client.collection('tasks').document(new_task_id).set(new_task_doc)

            # ----------
            # prototasks
            # ----------

            # start queestions
            start_questions_docs = client.collection('schema').document(
                'structure').collection('prototasks').document(
                    next_stage['proto_task']).collection(u'start').stream()

            for start_question_doc in start_questions_docs:
                question_id = start_question_doc.id
                question_info = start_question_doc.to_dict()

                client.collection('tasks').document(new_task_id).collection(
                    'questions').document(question_id).set(question_info)

            # stage questions
            stage_questions_docs = client.collection('schema').document(
                'structure').collection('cases').document(
                    task['case_type']).collection('stages').document(
                        stage['next_task']).collection(u'questions').stream()

            for stage_question_doc in stage_questions_docs:
                question_id = stage_question_doc.id
                question_info = stage_question_doc.to_dict()

                client.collection('tasks').document(new_task_id).collection(
                    'questions').document(question_id).set(question_info)

            # end questions
            end_questions_docs = client.collection('schema').document(
                'structure').collection('prototasks').document(
                    next_stage['proto_task']).collection(u'end').stream()

            for end_question_doc in end_questions_docs:
                question_id = end_question_doc.id
                question_info = end_question_doc.to_dict()

                client.collection('tasks').document(new_task_id).collection(
                    'questions').document(question_id).set(question_info)

            # user_editable
            client.collection('tasks').document(new_task_id).collection(
                'user_editable').document('user_editable').set(
                    {'status': 'open'})

            # responses
            response_id = uuid.uuid1().__str__()
            client.collection('tasks').document(new_task_id).collection(
                'responses').document(response_id).set({'test': 123})

        # NEXT TASK (ANN and KATE) NEW LOGIC!!!
        if stage.get('nextLogic') and stage['nextLogic'] != '':
            # create big object
            responses_generator = client.collection(collection_path) \
                .document(path_parts[1]) \
                .collection("responses").stream()
            responses = dict()
            for res in responses_generator:
                # make a clean responses dict
                content = res.to_dict()['contents'] if res.to_dict().get(
                    "contents") else res.to_dict()
                responses[res.id] = content

            big_object = {
                "stage": stage_ref.id,
                "userId": task['assigned_users'],
                "source": {
                    "stage": stage_ref.id,
                    "userId": task['assigned_users'],
                    "region": responses.get('region')
                }
            }
            big_object.update(responses)

            print("BIG OBJECT", big_object)

            # apply nextLogic and create new task
            nextRule = eval(stage['nextLogic'])
            print("nextRule: ", nextRule)
            nextData = big_object
            logic_output = jsonLogic(nextRule, nextData)
            print(task['case_stage_id'])
            print("output: ", logic_output)

            # if task['case_stage_id'] == 'emergency_form_filling':
            #     nextTaskString = re.sub("}", "},", logic_output[1:][:-1], 1)
            #     nextTaskList = json.loads(nextTaskString)
            #     for variant in nextTaskList:
            #         create_task(variant, big_object, task, stage)
            # elif (task['case_stage_id'] == 'emergency_form_complaint_submit' or
            #     task['case_stage_id'] == 'emergency_form_publication'):
            #     pass
            # else:
            #     create_task(json.loads(logic_output), big_object, task, stage)

            # put logic output item inside a list
            if logic_output[0] != "[":
                logic_output = f"[{logic_output}]"
            """ 
            OLD
            if task['case_stage_id'] == 'emergency_form_filling':
                nextTaskString = re.sub("}", "},", logic_output[1:][:-1], 1)
                nextTaskList = json.loads(nextTaskString)
                for variant in nextTaskList:
                    create_task(variant, big_object, task, stage)
                    pass
            else:
                create_task(json.loads(logic_output), variant, big_object, task, stage)"""
            nextTaskList = json.loads(logic_output)
            for variant in nextTaskList:
                if variant is not None:
                    create_task(variant, big_object, task, stage,
                                collection_path)
                else:
                    print("ERROR! No variant in next tasks list!!!")

    else:
        print('task is not complete yet')

    if doc.get('status') == 'released':
        user_id = task['assigned_users'][0]

        # print(event)
        # print('USER_ID: ' + user_id)
        # print('PATH: ' + doc_ref.get().reference.path)
        task_ref.update({
            'is_complete': False,
            'assigned_users': firestore.ArrayRemove([user_id])
        })

        if doc.get(
                'release_status'
        ) == 'Задание выполнить невозможно, так как не хватает исходных данных':
            task_ref.update({'available': False})

        delete_as_reader(task, user_id)
Ejemplo n.º 24
0
 def delete_ng_session(self, session_id):
     doc_ref = db.collection(self.collection_name).document("NG_LIST")
     doc_ref.set({
         "session_id": firestore.ArrayRemove([session_id])
     }, merge=True)
Ejemplo n.º 25
0
 def delete_ng_ip(self, ip):
     doc_ref = db.collection(self.collection_name).document("NG_LIST")
     doc_ref.set({
         "ip": firestore.ArrayRemove([ip])
     }, merge=True)