@require_permission(Permissions.edit_data)
@requires_privilege_with_fallback(privileges.OUTBOUND_SMS)
def chat(request, domain, contact_id, vn_id=None):
    domain_obj = Domain.get_by_name(domain, strict=True)
    timezone = get_timezone_for_user(None, domain)

    # floored_utc_timestamp is the datetime in UTC representing
    # midnight today in local time. This is used to calculate
    # all message history choices' timestamps, so that choosing
    # "Yesterday", for example, gives you data from yesterday at
    # midnight local time.
    local_date = datetime.now(timezone).date()
    floored_utc_timestamp = UserTime(
        datetime.combine(local_date, time(0, 0)),
        timezone
    ).server_time().done()

    def _fmt(d):
        return json_format_datetime(timedelta(days=d) - floored_utc_timestamp)
    history_choices = [(_(x), _fmt(y)) for (x, y) in SMS_CHAT_HISTORY_CHOICES]
    history_choices.append(
        (_("All Time"), json_format_datetime(datetime(1970, 1, 1)))
    )

    context = {
        "domain": domain,
        "contact_id": contact_id,
        "contact": get_contact(contact_id),
        "use_message_counter": domain_obj.chat_message_count_threshold is not None,
        "message_count_threshold": domain_obj.chat_message_count_threshold or 0,
        "custom_case_username": domain_obj.custom_case_username,
        "history_choices": history_choices,
        "vn_id": vn_id,
    }
    template = settings.CUSTOM_CHAT_TEMPLATES.get(domain_obj.custom_chat_template) or "sms/chat.html"
    return render(request, template, context)
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/sms/views.py/chat/swapped operands of `-`