Django tool
Project description
shopcloud-django-toolbox
install
$ pip install shopcloud-django-toolbox
Models
Add the GID as identifier to all django models
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
DJ_TOOLBOX_SIGNER = {
'VERSION': "1",
}
dump values dict:
from shopcloud django.toolbox import signer
signing_key = signer.dumps({"foo":"bar"}, expire_in_hours:12)
load values dict:
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:
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
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
)
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.
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
from shopcloud-django-toolbox import hash_for_file
hash_for_file("./some-file.txt")
deploy
# 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__
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for shopcloud_django_toolbox-1.12.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ed00dabc839dad635ef5f4727614b673e873631fa0f1aeccbc499ce8cad95db |
|
MD5 | 26a188bcdd6bc5a467839606e025b9ea |
|
BLAKE2b-256 | 2edd130281e911b902af3ad234aa5785b1f866c9ba4b0b6b44b89ead239df932 |
Close
Hashes for shopcloud_django_toolbox-1.12.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5127b486a86ada4bccb24e55b8040c3cdcf7284f3ba66a579f6682322f01964c |
|
MD5 | 5567b43a03ca4260490455f3f8abb96e |
|
BLAKE2b-256 | b0926405e2e38cda94871e0e330e3f5af29565637811e1a54e4fdac88a95245f |