PyCTX for Django
================
**django-pyctx** is a context package to use data between function calls, use timers and log it.
For detailed documentation please visit [Wiki](https://github.com/molcay/django-pyctx/wiki).
Quick Start
-----------
1. Add **django_pyctx** to your ``INSTALLED_APPS`` setting like this:
```python
INSTALLED_APPS = [
#...,
"django_pyctx",
]
```
2. Add **django_pyctx.middlewares.RequestCTXMiddleware** to your ``MIDDLEWARE`` setting like this:
```python
MIDDLEWARE = [
"django_pyctx.middlewares.RequestCTXMiddleware",
# ...,
]
```
> Please add "django_pyctx.middlewares.RequestCTXMiddleware" to at the beginning of the MIDDLEWARE list.
3. Start the development server and enjoy :)
Sample Usage
------------
- You can reach `RequestContext` instance in **views** from `request`: `request.ctx`
- Example django function-based `view`:
```python
from django.http import JsonResponse
def index(request):
y = 5
with request.ctx.log.timeit('index_timer'):
request.ctx.log.set_data('isEven', y % 2)
request.ctx.log.set_data('y', y)
request.ctx.log.start_timer('timer1')
import time
time.sleep(0.3)
request.ctx.log.stop_timer('timer1')
time.sleep(0.8)
return JsonResponse({})
```
You can see the stdout. You are probably seeing something like this:
```json
{
"type": "REQ",
"ctxId": "a9b66113-aa96-4419-b9ec-961ce0ebf3ae",
"startTime": "2019-08-23 13:47:46.146172",
"endTime": "2019-08-23 13:47:47.258287",
"data": {
"isEven": 1,
"y": 5
},
"timers": {
"ALL": 1.112128,
"request": 1.112115,
"index_timer": 1.107513,
"timer1": 0.302767
},
"http": {
"request": {
"method": "GET",
"path": "/ctxtest",
"qs": "",
"full_path": "/ctxtest",
"is_secure": false,
"is_xhr": false,
"headers": {
"Content-Length": "",
"Content-Type": "text/plain",
"Host": "localhost:8000",
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Dnt": "1",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Sec-Fetch-Site": "none",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "tr,en-GB;q=0.9,en;q=0.8,en-US;q=0.7",
"Cookie": "Pycharm-358d8f24=40efd37d-3767-43c2-8704-8abdbc8e441c; hblid=2S0d7GIKtYrYxbaF3m39N0M07TEBJbrW; olfsk=olfsk09308937734654421; Pycharm-358d92e3=f744a971-3d23-48a3-8188-7818d8efeb90; jenkins-timestamper-offset=-10800000; Pycharm-358d92e4=39469e28-3138-45a1-8133-16b05a158037; __test=1; csrftoken=qAbZmh519QGb6c1h702qe3YOtL8Q0ADakbXqqj4o5G5UznTybJVPigGG1mDBTtgP; Idea-535a2bcb=d87ec75d-65c5-46dd-a04b-6e914b434b5a; lang=en-US; iconSize=32x32; JSESSIONID.3e560a2e=node015mpq963ev6tulzcbplgyu8i1438.node0"
}
},
"client": {
"ip": "127.0.0.1",
"host": "",
"agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
},
"status": {
"code": 200,
"phrase": "OK"
},
"server": {
"name": "1.0.0.127.in-addr.arpa",
"port": "8000"
},
"view": "run"
}
}
```
> NOTE: this output formatted
Raw data
{
"_id": null,
"home_page": null,
"name": "django-pyctx",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "context, django, django-pyctx, django-req-ctx, pyctx",
"author": null,
"author_email": "\"M. Olcay TERCANLI\" <molcay@mail.com>",
"download_url": "https://files.pythonhosted.org/packages/75/f9/763a9f5126e4d1649c5507425103d0cc0053fd0ad928f5f30c070a54fd6e/django_pyctx-0.6.0.tar.gz",
"platform": null,
"description": "PyCTX for Django\n================\n\n**django-pyctx** is a context package to use data between function calls, use timers and log it.\n\nFor detailed documentation please visit [Wiki](https://github.com/molcay/django-pyctx/wiki).\n\nQuick Start\n-----------\n\n1. Add **django_pyctx** to your ``INSTALLED_APPS``\u00a0 setting like this:\n\n ```python\n INSTALLED_APPS = [\n #...,\n \"django_pyctx\",\n ]\n ```\n\n2. Add **django_pyctx.middlewares.RequestCTXMiddleware** to your ``MIDDLEWARE`` setting like this:\n\n ```python\n MIDDLEWARE = [\n \"django_pyctx.middlewares.RequestCTXMiddleware\",\n # ...,\n ]\n ```\n\n> Please add \"django_pyctx.middlewares.RequestCTXMiddleware\" to at the beginning of the MIDDLEWARE list.\n\n3. Start the development server and enjoy :)\n\nSample Usage\n------------\n\n- You can reach `RequestContext` instance in **views** from `request`: `request.ctx`\n\n- Example django function-based `view`:\n\n ```python\n from django.http import JsonResponse\n \n \n def index(request):\n y = 5\n with request.ctx.log.timeit('index_timer'):\n request.ctx.log.set_data('isEven', y % 2)\n request.ctx.log.set_data('y', y)\n request.ctx.log.start_timer('timer1')\n import time\n time.sleep(0.3)\n request.ctx.log.stop_timer('timer1')\n time.sleep(0.8)\n return JsonResponse({})\n ```\n\nYou can see the stdout. You are probably seeing something like this:\n\n```json\n{\n \"type\": \"REQ\",\n \"ctxId\": \"a9b66113-aa96-4419-b9ec-961ce0ebf3ae\",\n \"startTime\": \"2019-08-23 13:47:46.146172\",\n \"endTime\": \"2019-08-23 13:47:47.258287\",\n \"data\": {\n \"isEven\": 1,\n \"y\": 5\n },\n \"timers\": {\n \"ALL\": 1.112128,\n \"request\": 1.112115,\n \"index_timer\": 1.107513,\n \"timer1\": 0.302767\n },\n \"http\": {\n \"request\": {\n \"method\": \"GET\",\n \"path\": \"/ctxtest\",\n \"qs\": \"\",\n \"full_path\": \"/ctxtest\",\n \"is_secure\": false,\n \"is_xhr\": false,\n \"headers\": {\n \"Content-Length\": \"\",\n \"Content-Type\": \"text/plain\",\n \"Host\": \"localhost:8000\",\n \"Connection\": \"keep-alive\",\n \"Cache-Control\": \"max-age=0\",\n \"Upgrade-Insecure-Requests\": \"1\",\n \"User-Agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36\",\n \"Sec-Fetch-Mode\": \"navigate\",\n \"Sec-Fetch-User\": \"?1\",\n \"Dnt\": \"1\",\n \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\",\n \"Sec-Fetch-Site\": \"none\",\n \"Accept-Encoding\": \"gzip, deflate, br\",\n \"Accept-Language\": \"tr,en-GB;q=0.9,en;q=0.8,en-US;q=0.7\",\n \"Cookie\": \"Pycharm-358d8f24=40efd37d-3767-43c2-8704-8abdbc8e441c; hblid=2S0d7GIKtYrYxbaF3m39N0M07TEBJbrW; olfsk=olfsk09308937734654421; Pycharm-358d92e3=f744a971-3d23-48a3-8188-7818d8efeb90; jenkins-timestamper-offset=-10800000; Pycharm-358d92e4=39469e28-3138-45a1-8133-16b05a158037; __test=1; csrftoken=qAbZmh519QGb6c1h702qe3YOtL8Q0ADakbXqqj4o5G5UznTybJVPigGG1mDBTtgP; Idea-535a2bcb=d87ec75d-65c5-46dd-a04b-6e914b434b5a; lang=en-US; iconSize=32x32; JSESSIONID.3e560a2e=node015mpq963ev6tulzcbplgyu8i1438.node0\"\n }\n },\n \"client\": {\n \"ip\": \"127.0.0.1\",\n \"host\": \"\",\n \"agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36\"\n },\n \"status\": {\n \"code\": 200,\n \"phrase\": \"OK\"\n },\n \"server\": {\n \"name\": \"1.0.0.127.in-addr.arpa\",\n \"port\": \"8000\"\n },\n \"view\": \"run\"\n }\n}\n```\n> NOTE: this output formatted\n",
"bugtrack_url": null,
"license": null,
"summary": "Context package to use data between function calls, use timers and log it for Django.",
"version": "0.6.0",
"project_urls": {
"Homepage": "https://github.com/molcay/django-pyctx",
"Issues": "https://github.com/molcay/django-pyctx/issues"
},
"split_keywords": [
"context",
" django",
" django-pyctx",
" django-req-ctx",
" pyctx"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b6a71f857aa73d46a9be40a22d6238a38690abc810a3dddac1194a97efa0e592",
"md5": "def9b9dd877ee47e57e52d9b47871e1c",
"sha256": "0b91401d381f03b28611465ef5630965954472b7344f657ffe6c91644a6fde8c"
},
"downloads": -1,
"filename": "django_pyctx-0.6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "def9b9dd877ee47e57e52d9b47871e1c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 7600,
"upload_time": "2025-01-16T20:04:01",
"upload_time_iso_8601": "2025-01-16T20:04:01.150776Z",
"url": "https://files.pythonhosted.org/packages/b6/a7/1f857aa73d46a9be40a22d6238a38690abc810a3dddac1194a97efa0e592/django_pyctx-0.6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "75f9763a9f5126e4d1649c5507425103d0cc0053fd0ad928f5f30c070a54fd6e",
"md5": "2e457a741d1734e7062084cb32b9dc0d",
"sha256": "c03caff65e9e821e132c7fa711f87b7ed3109115bfe03f13f6d5355ea79f81ba"
},
"downloads": -1,
"filename": "django_pyctx-0.6.0.tar.gz",
"has_sig": false,
"md5_digest": "2e457a741d1734e7062084cb32b9dc0d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12760,
"upload_time": "2025-01-16T20:04:02",
"upload_time_iso_8601": "2025-01-16T20:04:02.212805Z",
"url": "https://files.pythonhosted.org/packages/75/f9/763a9f5126e4d1649c5507425103d0cc0053fd0ad928f5f30c070a54fd6e/django_pyctx-0.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-16 20:04:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "molcay",
"github_project": "django-pyctx",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-pyctx"
}