示例#1
0
def process_document_ts_write(data, context):
    """
    Updates create and modified timestamps on documents.
    """
    with setup(data, context):
        from phenoback.functions import documents

        collection_path = get_collection_path(context)
        document_id = get_document_id(context)

        if is_create_event(data):
            log.info(
                "update created ts on document %s (%s)",
                context.resource,
                get_field(data, "source", expected=False),
            )
            documents.update_created_document(collection_path, document_id)
        elif is_update_event(data) and not is_field_updated(
                data, documents.MODIFIED_KEY):
            log.info(
                "update modified ts on document %s %s (%s)",
                context.resource,
                get_fields_updated(data),
                get_field(data, "source", expected=False),
            )
            documents.update_modified_document(collection_path, document_id)
        elif is_delete_event(data):
            log.info("document %s was deleted", context.resource)
        else:
            log.debug(
                "Nothing to do for document %s (%s)",
                context.resource,
                get_field(data, "source", old_value=True, expected=False),
            )
示例#2
0
def process_user_write(data, context):
    """
    Processes user related documents if a user is created, modified or deleted.
    """
    with setup(data, context):
        from phenoback.functions import users

        user_id = get_document_id(context)

        if is_update_event(data) and is_field_updated(data, "nickname"):
            log.info("update nickname for %s", user_id)
            users.process_update_nickname(
                user_id,
                get_field(data, "nickname", old_value=True),
                get_field(data, "nickname"),
            )
        elif is_delete_event(data):
            log.info("delete user %s", user_id)
            users.process_delete_user(
                user_id, get_field(data, "nickname", old_value=True))
        elif is_create_event(data):
            log.info("create user %s", user_id)
            users.process_new_user(user_id, get_field(data, "nickname"))
        else:
            log.debug("Nothing to do for %s", user_id)
示例#3
0
def process_observation_delete_analytics(data, context):
    """
    Updates analytical values in Firestore if an observation was deleted.
    """
    with setup(data, context):
        observation_id = get_document_id(context)
        phenophase = get_field(data, "phenophase", old_value=True)
        if phenophase in ANALYTIC_PHENOPHASES:
            from phenoback.functions import analytics

            log.info("Remove observation %s", observation_id)
            analytics.process_remove_observation(
                observation_id,
                get_field(data, "individual_id", old_value=True),
                get_field(data, "source", old_value=True),
                get_field(data, "year", old_value=True),
                get_field(data, "species", old_value=True),
                phenophase,
            )
        else:
            log.debug(
                "No analytic values processed for %s, phenophase %s",
                observation_id,
                phenophase,
            )
示例#4
0
def _process_observation_activity(data, context, action):
    from phenoback.functions import activity

    delete_op = action == "delete"
    activity.process_observation(
        event_id=context.event_id,
        observation_id=get_document_id(context),
        individual_id=get_field(data, "individual_id", old_value=delete_op),
        user_id=get_field(data, "user", old_value=delete_op),
        phenophase=get_field(data, "phenophase", old_value=delete_op),
        source=get_field(data, "source", old_value=delete_op),
        species=get_field(data, "species", old_value=delete_op),
        individual=get_field(data, "individual", old_value=delete_op),
        action=action,
    )
示例#5
0
def process_invite_write(data, context):
    """
    Send email invites if invite is created or resend is set
    """
    with setup(data, context):
        from phenoback.functions.invite import invite

        # process if new invite or resend was changed but not deleted
        if is_create_event(data) or (is_field_updated(
                data, "resend") and get_field(data, "resend", expected=False)):
            invite.process(
                get_document_id(context),
                get_field(data, "email"),
                get_field(data, "locale"),
                get_field(data, "user"),
                get_field(data, "sent", expected=False),
            )
示例#6
0
def process_observation_create_analytics(data, context):
    """
    Updates analytic values in Firestore when an observation is created in Firestore.
    """
    with setup(data, context):
        from phenoback.functions import observation

        observation_id = get_document_id(context)
        phenophase = get_field(data, "phenophase")
        individual_id = get_field(data, "individual_id")
        source = get_field(data, "source")
        year = get_field(data, "year")
        species = get_field(data, "species")
        observation_date = get_field(data, "date")

        if phenophase in ANALYTIC_PHENOPHASES:
            from phenoback.functions import analytics

            log.info(
                "Process analytic values for %s, phenophase %s",
                observation_id,
                phenophase,
            )
            analytics.process_observation(
                observation_id,
                observation_date,
                individual_id,
                source,
                year,
                species,
                phenophase,
            )
        else:
            log.debug(
                "No analytic values processed for %s, phenophase %s",
                observation_id,
                phenophase,
            )
        # LAST OBSERVATION DATE
        log.info(
            "Process last observation date for %s, phenophase %s",
            observation_id,
            phenophase,
        )
        observation.update_last_observation(individual_id, phenophase,
                                            observation_date)
示例#7
0
def process_observation_write_activity(data, context):
    """
    Creates an activity when an observation is created, modified or deleted in
    Firestore **and** the user or individual of that observation is being followed.
    """
    with setup(data, context):
        observation_id = get_document_id(context)
        if is_create_event(data):
            log.info("Add create activity for observation %s", observation_id)
            _process_observation_activity(data, context, "create")
        elif is_field_updated(data, "date"):
            log.info("Add modify activity for observation %s", observation_id)
            _process_observation_activity(data, context, "modify")
        elif is_delete_event(data):
            log.info("Add delete activity for observation %s", observation_id)
            _process_observation_activity(data, context, "delete")
        else:
            log.debug("No activity to add")
示例#8
0
def process_user_write_update_invite(data, context):
    """
    Processes invite related documents if a user is created, modified or deleted.
    """
    with setup(data, context):
        from phenoback.functions.invite import register

        user_id = get_document_id(context)
        nickname = get_field(data, "nickname",
                             expected=False)  # don't warn on delete event

        if is_update_event(data) and is_field_updated(data, "nickname"):
            log.debug("update nickname on invites for user %s", user_id)
            register.change_nickname(user_id, nickname)
        elif is_delete_event(data):
            log.debug("delete invites for user %s", user_id)
            register.delete_user(user_id)
        elif is_create_event(data):
            log.debug("update invites for user %s", user_id)
            register.register_user(user_id, nickname)
        else:
            log.debug("Nothing to do for %s", user_id)
def test_get_document_id(expected, resource):
    context = Context
    context.resource = PropertyMock(return_value=resource)
    assert get_document_id(context) == expected