(domain, form_id, user_id, archive)
return unfinished_archive_stub
Delete all other competing archive stubs for the same xform

    This makes sure to retain the same row / pk for successive retries of the same failure,
    but creates a new row (and deletes the existing one) for archive/unarchive attempts
    for which either the user or the archive action (archive versus unarchive) differs.
Delete all other competing archive stubs for the same xform
Delete all other competing archive stubs for the same xform
def _get_or_create_unfinished_archive_stub(domain, form_id, user_id, archive):
    """
    Delete all other competing archive stubs for the same xform

    This makes sure to retain the same row / pk for successive retries of the same failure,
    but creates a new row (and deletes the existing one) for archive/unarchive attempts
    for which either the user or the archive action (archive versus unarchive) differs.

    """
    from couchforms.models import UnfinishedArchiveStub
    defaults = dict(
        user_id=user_id,
        timestamp=datetime.datetime.utcnow(),
        history_updated=False,
        # if archive is False, this is an unarchive stub.
        archive=archive,
        domain=domain,
        attempts=0,
    )
    unfinished_archive_stub, created = UnfinishedArchiveStub.objects.get_or_create(
        xform_id=form_id, defaults=defaults
    )
    if created:
        pass
    elif unfinished_archive_stub.archive == archive \
            and unfinished_archive_stub.user_id == user_id:
        # if the stub already exists and it's "the same" stub
        # then we treat it as an auto retry.
        # It could be the same user repeating the operation,
        # but we'll track that as another attempt at the same operation,
        # not as a new operation. (It's just easier to do that, and it doesn't matter much.)
        unfinished_archive_stub.attempts = F('attempts') + 1
        unfinished_archive_stub.save()
    else:
        UnfinishedArchiveStub.objects.filter(xform_id=form_id).delete()
        unfinished_archive_stub = UnfinishedArchiveStub.objects.create(
            xform_id=form_id, **defaults
        )
    return unfinished_archive_stub
def _get_or_create_unfinished_archive_stub ( domain , form_id , user_id , archive ) : from couchforms . models import UnfinishedArchiveStub defaults = dict ( user_id = user_id , timestamp = datetime . datetime . utcnow ( ) , history_updated = False , # if archive is False, this is an unarchive stub. archive = archive , domain = domain , attempts = 0 , ) unfinished_archive_stub , created = UnfinishedArchiveStub . objects . get_or_create ( xform_id = form_id , defaults = defaults ) if created : pass elif unfinished_archive_stub . archive == archive and unfinished_archive_stub . user_id == user_id : # if the stub already exists and it's "the same" stub # then we treat it as an auto retry. # It could be the same user repeating the operation, # but we'll track that as another attempt at the same operation, # not as a new operation. (It's just easier to do that, and it doesn't matter much.) unfinished_archive_stub . attempts = F ( 'attempts' ) + 1 unfinished_archive_stub . save ( ) else : UnfinishedArchiveStub . objects . filter ( xform_id = form_id ) . delete ( ) unfinished_archive_stub = UnfinishedArchiveStub . objects . create ( xform_id = form_id , * * defaults ) return unfinished_archive_stub