# shopcloud-django-toolbox
## install
```sh
$ pip install shopcloud-django-toolbox
```
## Models
Add the GID as identifier to all django models
```python3
from shopcloud_django_toolbox import GID
class FooBarModel(models.Model, GID):
pass
```
## URL-Signing
generate or load signed base64 encoded dict containing versioned with expiration date for access-control
### Usage
`settings.py`
```python3
DJ_TOOLBOX_SIGNER = {
'VERSION': "1",
}
```
__dump values dict:__
```python3
from shopcloud django.toolbox import signer
signing_key = signer.dumps({"foo":"bar"}, expire_in_hours:12)
```
__load values dict:__
```python3
from shopcloud django.toolbox import signer
from django.http import HttpResponseForbidden
def some_view(request):
sign_str = request.GET.get("sign")
is_valid, data = signer.loads(sign_str)
if not is_valid(request):
return HttpResponseForbidden("Not allowed")
data.get("xxx")
#
## … some view logic
#
```
__alternative for a django Request:__
```python3
from shopcloud django.toolbox import signer
from django.http import HttpResponseForbidden
def some_view(request):
sign_str = request.GET.get("sign")
is_valid, data = signer.loads_from_request(sign_str)
if not is_valid(request):
return HttpResponseForbidden("Not allowed")
data.get("xxx")
#
## … some view logic
#
```
__Django View Decorator__
check if GET-parameter `sign` is set and has required keys in data objects
```
from shopcloud_django_toolbox import signer
from shopcloud_django_toolbox.decorators import
@require_toolbox_sign(needed_keys=['key_that_must_exist', 'foo', 'bar'])
def some_view(request):
is_valid, data = signer.loads_from_request(request)
#
## … some view logic
#
```
## Testing
### API standart tests
Standart template for the `tests.py` file in all modules with admin and REST API`s
```python3
from shopcloud_django_toolbox.tests import BaseTestAPIEndpointDoc
from shopcloud_django_toolbox import TestAdminTestCase
from shopcloud_django_toolbox.tests import BaseTestApiAuthorization
from shopcloud_django_toolbox import SetupClass
from shopcloud_django_toolbox.tests import Representation
class TestDocEntpoint(BaseTestAPIEndpointDoc):
pass
__
class TestRepresentation(utils.Representation):
def test_representation(self):
from . import seeds
self._test_representations_from_seeds(seeds)
class TestAdminPages(TestAdminTestCase):
MODULE = 'url-name'
def test_admin_easylineitem(self):
self.run_for_model(
'url-model-name',
is_check_add=False, # deactivate when add function is deactivated
is_check_template=False, # deactivate generic template check
is_check_search=True, # activate searchbar check
is_check_detail=False, # erzeugt ein Model-Object und ruft den Detail-View auf,
creation_parameters={}, # notwendig für den detailcheck da ein generisches erzeugen von spezeillen Models nicht mgl
)
class TestApiAuthorization(BaseTestApiAuthorization):
app_name = "url-module-name"
def test_model_foo(self):
self.run_test_endpoint("url-model-name")
def test_model_bar(self):
self.run_test_endpoint("url-model-name")
class YoutAPITest(SetupClass):
def test_api_endpint(self):
client = APIClient()
client.login(username=self.username, password=self.pwd)
r = client.get('/module/api/endpoint')
self.assertEqual(r.status_code, status.HTTP_201_CREATED)
```
## Event
To fire events and run tasks in prarallel, need [PROJECT] to receive and run the output from log and call the API.
```python3
from shopcloud_django_toolbox import Event
class FooBarModel(models.Model):
...
def do_event(self):
event = Event(
name="de.talk-point.platform/module/model/sync",
model=self,
)
event.add_task(
queue="default",
url=f"moduke/api/model/{self.id}/action/",
json={}
)
event.fire()
```
#### File Hash Generating
generate sha-384 file hash for sri with given filepath
```python3
from shopcloud-django-toolbox import hash_for_file
hash_for_file("./some-file.txt")
```
## deploy
```sh
# change version Number in setup.py ändern und dann erst releasen
# delete build and dist-directory
$ rm -rf build dist
$ pip3 install wheel twine
$ python3 setup.py sdist bdist_wheel
$ twine upload dist/*
- username __token__
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Talk-Point/shopcloud-django-toolbox",
"name": "shopcloud-django-toolbox",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Konstantin Stoldt",
"author_email": "konstantin.stoldt@talk-point.de",
"download_url": "https://files.pythonhosted.org/packages/f8/14/fd247ca6a76c71aa48a0b02b98a44475847c2f443d4f51dfd13c35de2fb6/shopcloud_django_toolbox-1.18.0.tar.gz",
"platform": null,
"description": "# shopcloud-django-toolbox\n\n## install\n\n```sh\n$ pip install shopcloud-django-toolbox\n```\n\n## Models\n\nAdd the GID as identifier to all django models\n\n```python3\nfrom shopcloud_django_toolbox import GID\n\nclass FooBarModel(models.Model, GID):\n pass\n```\n\n## URL-Signing\n\ngenerate or load signed base64 encoded dict containing versioned with expiration date for access-control\n\n### Usage\n\n`settings.py`\n```python3\nDJ_TOOLBOX_SIGNER = {\n 'VERSION': \"1\",\n}\n```\n\n__dump values dict:__\n```python3\nfrom shopcloud django.toolbox import signer\n\nsigning_key = signer.dumps({\"foo\":\"bar\"}, expire_in_hours:12)\n\n```\n\n__load values dict:__\n\n```python3\nfrom shopcloud django.toolbox import signer\nfrom django.http import HttpResponseForbidden\n\ndef some_view(request):\n sign_str = request.GET.get(\"sign\")\n is_valid, data = signer.loads(sign_str)\n if not is_valid(request):\n return HttpResponseForbidden(\"Not allowed\")\n data.get(\"xxx\")\n #\n ## \u2026 some view logic\n #\n```\n\n__alternative for a django Request:__\n\n```python3\nfrom shopcloud django.toolbox import signer\nfrom django.http import HttpResponseForbidden\n\ndef some_view(request):\n sign_str = request.GET.get(\"sign\")\n is_valid, data = signer.loads_from_request(sign_str)\n if not is_valid(request):\n return HttpResponseForbidden(\"Not allowed\")\n data.get(\"xxx\")\n #\n ## \u2026 some view logic\n #\n```\n\n__Django View Decorator__\n\ncheck if GET-parameter `sign` is set and has required keys in data objects\n\n```\nfrom shopcloud_django_toolbox import signer\nfrom shopcloud_django_toolbox.decorators import\n\n\n@require_toolbox_sign(needed_keys=['key_that_must_exist', 'foo', 'bar'])\ndef some_view(request):\n is_valid, data = signer.loads_from_request(request)\n #\n ## \u2026 some view logic\n #\n```\n\n\n## Testing\n\n### API standart tests\n\nStandart template for the `tests.py` file in all modules with admin and REST API`s\n\n```python3\nfrom shopcloud_django_toolbox.tests import BaseTestAPIEndpointDoc\nfrom shopcloud_django_toolbox import TestAdminTestCase\nfrom shopcloud_django_toolbox.tests import BaseTestApiAuthorization\nfrom shopcloud_django_toolbox import SetupClass\nfrom shopcloud_django_toolbox.tests import Representation\n\n\n\nclass TestDocEntpoint(BaseTestAPIEndpointDoc):\n pass\n__\n\n\nclass TestRepresentation(utils.Representation):\n def test_representation(self):\n from . import seeds\n self._test_representations_from_seeds(seeds)\n\n\nclass TestAdminPages(TestAdminTestCase):\n MODULE = 'url-name'\n\n def test_admin_easylineitem(self):\n self.run_for_model(\n 'url-model-name',\n is_check_add=False, # deactivate when add function is deactivated\n is_check_template=False, # deactivate generic template check\n is_check_search=True, # activate searchbar check\n is_check_detail=False, # erzeugt ein Model-Object und ruft den Detail-View auf,\n creation_parameters={}, # notwendig f\u00fcr den detailcheck da ein generisches erzeugen von spezeillen Models nicht mgl\n )\n\n\nclass TestApiAuthorization(BaseTestApiAuthorization):\n app_name = \"url-module-name\"\n\n def test_model_foo(self):\n self.run_test_endpoint(\"url-model-name\")\n\n def test_model_bar(self):\n self.run_test_endpoint(\"url-model-name\")\n\n\nclass YoutAPITest(SetupClass):\n def test_api_endpint(self):\n client = APIClient()\n client.login(username=self.username, password=self.pwd)\n\n r = client.get('/module/api/endpoint')\n self.assertEqual(r.status_code, status.HTTP_201_CREATED)\n\n\n```\n\n## Event\n\nTo fire events and run tasks in prarallel, need [PROJECT] to receive and run the output from log and call the API.\n\n```python3\nfrom shopcloud_django_toolbox import Event\n\n\nclass FooBarModel(models.Model):\n ...\n\n def do_event(self):\n event = Event(\n name=\"de.talk-point.platform/module/model/sync\",\n model=self,\n )\n event.add_task(\n queue=\"default\",\n url=f\"moduke/api/model/{self.id}/action/\",\n json={}\n )\n event.fire()\n```\n\n#### File Hash Generating\n\ngenerate sha-384 file hash for sri with given filepath\n\n```python3\nfrom shopcloud-django-toolbox import hash_for_file\n\n\nhash_for_file(\"./some-file.txt\")\n```\n\n## deploy\n\n```sh\n# change version Number in setup.py \u00e4ndern und dann erst releasen\n# delete build and dist-directory\n$ rm -rf build dist\n$ pip3 install wheel twine\n$ python3 setup.py sdist bdist_wheel\n$ twine upload dist/*\n - username __token__\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django tool",
"version": "1.18.0",
"project_urls": {
"Homepage": "https://github.com/Talk-Point/shopcloud-django-toolbox"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "af0f14d6714d2f3dcabbe1d95e52f0af00b9f36399a2887cb912654e6052176d",
"md5": "a7b77b987d01ce739cdca9b148231f57",
"sha256": "cb6da5cd570a015ebf75471db3a4e28995c3eea35256e38e3c55127620c62120"
},
"downloads": -1,
"filename": "shopcloud_django_toolbox-1.18.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a7b77b987d01ce739cdca9b148231f57",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 12198,
"upload_time": "2023-12-09T14:34:26",
"upload_time_iso_8601": "2023-12-09T14:34:26.931523Z",
"url": "https://files.pythonhosted.org/packages/af/0f/14d6714d2f3dcabbe1d95e52f0af00b9f36399a2887cb912654e6052176d/shopcloud_django_toolbox-1.18.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f814fd247ca6a76c71aa48a0b02b98a44475847c2f443d4f51dfd13c35de2fb6",
"md5": "9df1f17ab5a88377d4eebf87605d1d47",
"sha256": "0c1a0fa420450b175d817b487f891bc07d21c0e98474427c28eddb081bcf0776"
},
"downloads": -1,
"filename": "shopcloud_django_toolbox-1.18.0.tar.gz",
"has_sig": false,
"md5_digest": "9df1f17ab5a88377d4eebf87605d1d47",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11009,
"upload_time": "2023-12-09T14:34:28",
"upload_time_iso_8601": "2023-12-09T14:34:28.599514Z",
"url": "https://files.pythonhosted.org/packages/f8/14/fd247ca6a76c71aa48a0b02b98a44475847c2f443d4f51dfd13c35de2fb6/shopcloud_django_toolbox-1.18.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-09 14:34:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Talk-Point",
"github_project": "shopcloud-django-toolbox",
"github_not_found": true,
"lcname": "shopcloud-django-toolbox"
}