[PR #5632] [CLOSED] Implemented WebRequestContext for emulating HTTP requests for ORM operations #13041

Closed
opened 2025-12-29 22:25:06 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/5632
Author: @lampwins
Created: 1/17/2021
Status: Closed

Base: developHead: 4932-web-request-context-manager


📝 Commits (1)

  • 7471adb closes #4932 - added WebRequestContext for emulating HTTP requests for ORM operations

📊 Changes

3 files changed (+138 additions, -0 deletions)

View changed files

📝 docs/administration/netbox-shell.md (+16 -0)
📝 netbox/extras/context_managers.py (+46 -0)
netbox/extras/tests/test_context_managers.py (+76 -0)

📄 Description

Fixes: #4932

This implements a new WebRequestContext context manager for emulating HTTP requests for ORM use. The primary need is for easily enabling change logging and webhooks when interacting with the ORM directly, and outside of the context of an HTTP request.

You will likely note the use of RequestFactory for creating the "fake" request object. I feel this is the safest way of doing this because it ensures we don't break anything in the future if we require more context from the request than just the user. When looking at the internals of RequestFactory, there is nothing specific to its primary usage in testing that should cause any issues here. I started down the path of doing pretty much the same thing it does to create a bare-bones WSGIRequest object and then decided this would be a better, cleaner approach.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/5632 **Author:** [@lampwins](https://github.com/lampwins) **Created:** 1/17/2021 **Status:** ❌ Closed **Base:** `develop` ← **Head:** `4932-web-request-context-manager` --- ### 📝 Commits (1) - [`7471adb`](https://github.com/netbox-community/netbox/commit/7471adb57251b409db0751327c1c873ba68130f4) closes #4932 - added WebRequestContext for emulating HTTP requests for ORM operations ### 📊 Changes **3 files changed** (+138 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `docs/administration/netbox-shell.md` (+16 -0) 📝 `netbox/extras/context_managers.py` (+46 -0) ➕ `netbox/extras/tests/test_context_managers.py` (+76 -0) </details> ### 📄 Description <!-- Thank you for your interest in contributing to NetBox! Please note that our contribution policy requires that a feature request or bug report be opened for approval prior to filing a pull request. This helps avoid wasting time and effort on something that we might not be able to accept. Please indicate the relevant feature request or bug report below. IF YOUR PULL REQUEST DOES NOT REFERENCE AN ACCEPTED BUG REPORT OR FEATURE REQUEST, IT WILL BE MARKED AS INVALID AND CLOSED. --> ### Fixes: #4932 <!-- Please include a summary of the proposed changes below. --> This implements a new `WebRequestContext` context manager for emulating HTTP requests for ORM use. The primary need is for easily enabling change logging and webhooks when interacting with the ORM directly, and outside of the context of an HTTP request. You will likely note the use of `RequestFactory` for creating the "fake" request object. I feel this is the safest way of doing this because it ensures we don't break anything in the future if we require more context from the request than just the user. When looking at the internals of `RequestFactory`, there is nothing specific to its primary usage in testing that should cause any issues here. I started down the path of doing pretty much the same thing it does to create a bare-bones `WSGIRequest` object and then decided this would be a better, cleaner approach. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 22:25:06 +01:00
adam closed this issue 2025-12-29 22:25:06 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#13041