예제 #1
0
def set_cleanliness_flags(domain, owner_id, force_full=False):
    """
    For a given owner ID, manually sets the cleanliness flag on that ID.
    """
    if not domain or len(domain) > 100:
        raise InvalidDomainError(
            u'Domain {} must be a non-empty string less than 100 characters'.
            format(domain))
    if not owner_id or len(owner_id) > 100:
        raise InvalidOwnerIdError(
            u'Owner ID {} must be a non-empty string less than 100 characters'.
            format(owner_id))
    cleanliness_object = OwnershipCleanlinessFlag.objects.get_or_create(
        owner_id=owner_id, domain=domain, defaults={'is_clean': False})[0]

    def needs_full_check(domain, cleanliness_obj):
        # if it already is clean we don't need to do anything since that gets invalidated on submission
        return (
            # if clean, only check if the toggle is not enabled since then it won't be properly invalidated
            # on submission
            cleanliness_obj.is_clean
            and not OWNERSHIP_CLEANLINESS.enabled(domain)
        ) or (
            # if dirty, first check the hint and only do a full check if it's not valid
            not cleanliness_object.is_clean and
            (not cleanliness_object.hint or
             not hint_still_valid(domain, owner_id, cleanliness_object.hint)))

    needs_check = needs_full_check(domain, cleanliness_object)
    previous_clean_flag = cleanliness_object.is_clean
    if force_full or needs_check:
        # either the hint wasn't set, wasn't valid or we're forcing a rebuild - rebuild from scratch
        cleanliness_flag = get_cleanliness_flag_from_scratch(domain, owner_id)
        cleanliness_object.is_clean = cleanliness_flag.is_clean
        cleanliness_object.hint = cleanliness_flag.hint

    if force_full and not needs_check and previous_clean_flag and not cleanliness_object.is_clean:
        # we went from clean to dirty and would not have checked except that we forced it
        # this seems to indicate a problem in the logic that invalidates the flag, unless the feature
        # flag was turned off for the domain. either way cory probably wants to know.
        try:
            document = get_db().get(owner_id)
        except ResourceNotFound:
            document = {'doc_type': 'unknown'}

        owner_doc_type = document.get('doc_type', None)
        # filter out docs where we expect this to be broken (currently just web users)
        if owner_doc_type != 'WebUser':
            _assert = soft_assert(to=['czue' + '@' + 'dimagi.com'],
                                  exponential_backoff=False,
                                  fail_if_debug=False)
            _assert(
                False,
                'Cleanliness flags out of sync for a {} with id {} in domain {}!'
                .format(owner_doc_type, owner_id, domain))

    else:
        cleanliness_object.last_checked = datetime.utcnow()
        cleanliness_object.save()
예제 #2
0
def set_cleanliness_flags(domain,
                          owner_id,
                          force_full=False,
                          raise_soft_assertions=True):
    """
    For a given owner ID, manually sets the cleanliness flag on that ID.
    """
    if not domain or len(domain) > 100:
        raise InvalidDomainError(
            u'Domain {} must be a non-empty string less than 100 characters'.
            format(domain))
    if not owner_id or len(owner_id) > 100:
        raise InvalidOwnerIdError(
            u'Owner ID {} must be a non-empty string less than 100 characters'.
            format(owner_id))
    cleanliness_object = OwnershipCleanlinessFlag.objects.get_or_create(
        owner_id=owner_id, domain=domain, defaults={'is_clean': False})[0]

    def needs_full_check(domain, cleanliness_obj):
        # if it already is clean we don't need to do anything since that gets invalidated on submission
        # if dirty, first check the hint and only do a full check if it's not valid
        return not cleanliness_obj.is_clean and (
            not cleanliness_obj.hint
            or not hint_still_valid(domain, cleanliness_obj.hint))

    needs_check = needs_full_check(domain, cleanliness_object)
    previous_clean_flag = cleanliness_object.is_clean
    if force_full or needs_check:
        # either the hint wasn't set, wasn't valid or we're forcing a rebuild - rebuild from scratch
        cleanliness_flag = get_cleanliness_flag_from_scratch(domain, owner_id)
        cleanliness_object.is_clean = cleanliness_flag.is_clean
        cleanliness_object.hint = cleanliness_flag.hint

    if force_full and not needs_check and previous_clean_flag and not cleanliness_object.is_clean:
        # we went from clean to dirty and would not have checked except that we forced it
        # this seems to indicate a problem in the logic that invalidates the flag, unless the feature
        # flag was turned off for the domain. either way cory probably wants to know.

        # filter out docs where we expect this to be broken (currently just web users)
        if not _is_web_user(owner_id) and raise_soft_assertions:
            _assert = soft_assert(notify_admins=True,
                                  exponential_backoff=False,
                                  fail_if_debug=False)
            _assert(
                False,
                'Cleanliness flags out of sync for user {} in domain {}!'.
                format(owner_id, domain))

    cleanliness_object.last_checked = datetime.utcnow()
    cleanliness_object.save()