Skip to content

Commit d6e32a3

Browse files
" -> '
1 parent 78512ca commit d6e32a3

File tree

4 files changed

+72
-72
lines changed

4 files changed

+72
-72
lines changed

async_include/checksum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def make(string):
11-
key = "{0}-{1}".format(string, settings.SECRET_KEY)
11+
key = '{0}-{1}'.format(string, settings.SECRET_KEY)
1212

1313
try:
1414
return hashlib.md5(key).hexdigest()

async_include/crypto.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77

88
def encrypt(key, data):
9-
cipher = AES.new(key.encode("utf-8"), AES.MODE_EAX)
10-
encrypted_data, tag = cipher.encrypt_and_digest(data.encode("utf-8"))
9+
cipher = AES.new(key.encode('utf-8'), AES.MODE_EAX)
10+
encrypted_data, tag = cipher.encrypt_and_digest(data.encode('utf-8'))
1111
return cipher.nonce, encrypted_data, tag
1212

1313

1414
def decrypt(key, nonce, encrypted_data, tag):
1515
cipher = AES.new(
16-
key.encode("utf-8"), AES.MODE_EAX, nonce.encode("latin-1")
16+
key.encode('utf-8'), AES.MODE_EAX, nonce.encode('latin-1')
1717
)
1818
data = cipher.decrypt_and_verify(
19-
encrypted_data.encode("latin-1"), tag.encode("latin-1")
19+
encrypted_data.encode('latin-1'), tag.encode('latin-1')
2020
)
21-
return data.decode("utf-8")
21+
return data.decode('utf-8')

async_include/templatetags/async_include.py

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_unique_template_id():
2626
Get an unique id for a template as a string.
2727
:return: an unique id for a template.
2828
"""
29-
return uuid.uuid4().urn[9:].replace("-", "")
29+
return uuid.uuid4().urn[9:].replace('-', '')
3030

3131

3232
def slugify_template_path(template_path):
@@ -37,7 +37,7 @@ def slugify_template_path(template_path):
3737
:param template_path: string with the template path.
3838
:return: slug of the template path.
3939
"""
40-
return slugify(template_path.replace("/", "_").replace("-", "_"))
40+
return slugify(template_path.replace('/', '_').replace('-', '_'))
4141

4242

4343
# Async include template tag.
@@ -52,44 +52,44 @@ def async_include(context, template_path, *args, **kwargs):
5252
slugified_template_path = slugify_template_path(template_path)
5353

5454
# Unique block id (uniqueness based on UUID)
55-
block_id = "{0}__{1}".format(
55+
block_id = '{0}__{1}'.format(
5656
slugified_template_path, get_unique_template_id()
5757
)
5858

5959
# Give the possibility to customize the HTML tag
60-
html__tag = kwargs.pop("html__tag", "div")
60+
html__tag = kwargs.pop('html__tag', 'div')
6161

6262
# HTML tag class
63-
html__tag__class = kwargs.pop("html__tag__class", slugified_template_path)
63+
html__tag__class = kwargs.pop('html__tag__class', slugified_template_path)
6464

6565
# Shall we show a spinner?
66-
spinner__visible = kwargs.pop("spinner__visible", True)
66+
spinner__visible = kwargs.pop('spinner__visible', True)
6767

6868
# Spinner template path
6969
spinner__template_path = kwargs.pop(
70-
"spinner__template_path", "async_include/spinner.html"
70+
'spinner__template_path', 'async_include/spinner.html'
7171
)
7272

7373
# Recurrent requests
74-
request__frequency = kwargs.pop("request__frequency", "once")
74+
request__frequency = kwargs.pop('request__frequency', 'once')
7575

7676
replacements = {
77-
"template_path": template_path,
78-
"block_id": block_id,
79-
"html__tag": html__tag,
80-
"html__tag__class": html__tag__class,
81-
"spinner__visible": spinner__visible,
82-
"spinner__template_path": spinner__template_path,
83-
"request__frequency": request__frequency,
84-
"context": {}
77+
'template_path': template_path,
78+
'block_id': block_id,
79+
'html__tag': html__tag,
80+
'html__tag__class': html__tag__class,
81+
'spinner__visible': spinner__visible,
82+
'spinner__template_path': spinner__template_path,
83+
'request__frequency': request__frequency,
84+
'context': {}
8585
}
8686

8787
for context_object_name, context_object in kwargs.items():
8888
# For each passed parameter,
8989
# it can be a model object or a safe value (string or number)
9090
is_model_object = (
91-
hasattr(context_object, "id") and
92-
hasattr(context_object.__class__, "__name__")
91+
hasattr(context_object, 'id') and
92+
hasattr(context_object.__class__, '__name__')
9393
)
9494

9595
# We store a reference of the model object based on its model name,
@@ -101,15 +101,15 @@ def async_include(context, template_path, *args, **kwargs):
101101
app_name = (
102102
ContentType.objects.get_for_model(context_object).app_label
103103
)
104-
model_object_as_str = "{0}-{1}-{2}".format(
104+
model_object_as_str = '{0}-{1}-{2}'.format(
105105
app_name, model_name, object_id
106106
)
107-
replacements["context"][context_object_name] = {
108-
"type": "model",
109-
"id": object_id,
110-
"app_name": app_name,
111-
"model": model_name,
112-
"__checksum__": checksum.make(model_object_as_str)
107+
replacements['context'][context_object_name] = {
108+
'type': 'model',
109+
'id': object_id,
110+
'app_name': app_name,
111+
'model': model_name,
112+
'__checksum__': checksum.make(model_object_as_str)
113113
}
114114

115115
elif type(context_object) == QuerySet:
@@ -123,32 +123,32 @@ def async_include(context, template_path, *args, **kwargs):
123123
key=settings.SECRET_KEY[:16], data=sql_query
124124
)
125125

126-
replacements["context"][context_object_name] = {
127-
"type": "QuerySet",
128-
"query": encrypted_sql.decode("latin-1"),
129-
"params": params,
130-
"nonce": nonce.decode("latin-1"),
131-
"tag": tag.decode("latin-1"),
132-
"app_name": app_name,
133-
"model": model_name,
126+
replacements['context'][context_object_name] = {
127+
'type': 'QuerySet',
128+
'query': encrypted_sql.decode('latin-1'),
129+
'params': params,
130+
'nonce': nonce.decode('latin-1'),
131+
'tag': tag.decode('latin-1'),
132+
'app_name': app_name,
133+
'model': model_name,
134134
}
135135

136-
# Safe values are sent "as is" to the view
136+
# Safe values are sent as is to the view
137137
# that will render the template
138138
else:
139-
context_object_as_str = "{0}".format(context_object)
140-
replacements["context"][context_object_name] = {
141-
"type": "safe_value",
142-
"value": context_object,
143-
"value_as_str": context_object_as_str,
144-
"__checksum__": checksum.make(context_object_as_str)
139+
context_object_as_str = '{0}'.format(context_object)
140+
replacements['context'][context_object_name] = {
141+
'type': 'safe_value',
142+
'value': context_object,
143+
'value_as_str': context_object_as_str,
144+
'__checksum__': checksum.make(context_object_as_str)
145145
}
146146

147147
# Serialization of context that will be sent
148-
replacements["context"] = jsonpickle.dumps(replacements["context"])
148+
replacements['context'] = jsonpickle.dumps(replacements['context'])
149149

150150
# Pass language code
151-
replacements["language_code"] = get_language()
151+
replacements['language_code'] = get_language()
152152

153153
# Render the template of this template tag
154154
try:

async_include/views.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,44 @@
1919
def get_template(request):
2020

2121
# POST request is mandatory
22-
if request.method != "POST":
22+
if request.method != 'POST':
2323
return HttpResponse(status=400)
2424

2525
json_body = jsonpickle.loads(request.body.decode('utf-8'))
26-
path = json_body.get("path")
26+
path = json_body.get('path')
2727

2828
# Remote context
2929
# The caller has sent the model objects and
3030
# safe values (strings, numbers, etc.) as a dict with
3131
# the app_labels, model and id
32-
context = json_body.get("context")
32+
context = json_body.get('context')
3333

3434
# language
35-
language_code = json_body.get("language_code")
35+
language_code = json_body.get('language_code')
3636

3737
replacements = {}
3838

3939
# For each remote context value, we load it again
4040
for context_object_name, context_object_load_params in context.items():
4141
# Type of the value
42-
object_type = context_object_load_params["type"]
42+
object_type = context_object_load_params['type']
4343

4444
# If the value is a model, we load the model object and
4545
# include it in the template replacements
46-
if object_type == "model":
47-
app_name = context_object_load_params["app_name"]
48-
model_name = context_object_load_params["model"]
49-
object_id = context_object_load_params["id"]
46+
if object_type == 'model':
47+
app_name = context_object_load_params['app_name']
48+
model_name = context_object_load_params['model']
49+
object_id = context_object_load_params['id']
5050
# Loading the model
5151
model = apps.get_model(app_name, model_name)
5252
# Loading the object and including it as a replacement
5353
model_object = model.objects.get(id=object_id)
5454
# Checking if JSON has been tampered
55-
model_object_as_str = "{0}-{1}-{2}".format(
55+
model_object_as_str = '{0}-{1}-{2}'.format(
5656
app_name, model_name, object_id
5757
)
5858
if (
59-
context_object_load_params["__checksum__"] !=
59+
context_object_load_params['__checksum__'] !=
6060
checksum.make(model_object_as_str)
6161
):
6262
return HttpResponse(
@@ -68,21 +68,21 @@ def get_template(request):
6868
replacements[context_object_name] = model_object
6969

7070
# If the value is a QuerySet we include it in the template replacements
71-
elif object_type == "QuerySet":
71+
elif object_type == 'QuerySet':
7272
# Loading the model
73-
app_name = context_object_load_params["app_name"]
74-
model_name = context_object_load_params["model"]
73+
app_name = context_object_load_params['app_name']
74+
model_name = context_object_load_params['model']
7575
model = apps.get_model(app_name, model_name)
76-
params = context_object_load_params["params"]
77-
nonce = context_object_load_params["nonce"]
78-
tag = context_object_load_params["tag"]
76+
params = context_object_load_params['params']
77+
nonce = context_object_load_params['nonce']
78+
tag = context_object_load_params['tag']
7979

8080
try:
8181
# Decryption of the data
8282
raw_query = crypto.decrypt(
8383
key=settings.SECRET_KEY[:16],
8484
nonce=nonce,
85-
encrypted_data=context_object_load_params["query"],
85+
encrypted_data=context_object_load_params['query'],
8686
tag=tag
8787
)
8888

@@ -95,18 +95,18 @@ def get_template(request):
9595

9696
# If the value is a safe value,
9797
# we include it in the template replacements
98-
elif object_type == "safe_value":
99-
value = context_object_load_params["value"]
100-
value_as_str = context_object_load_params["value_as_str"]
98+
elif object_type == 'safe_value':
99+
value = context_object_load_params['value']
100+
value_as_str = context_object_load_params['value_as_str']
101101
# Checking if JSON has been tampered
102102
if (
103-
context_object_load_params["__checksum__"] !=
103+
context_object_load_params['__checksum__'] !=
104104
checksum.make(value_as_str)
105105
):
106106
return HttpResponse(
107107
status=403,
108-
content="JSON tampering detected when loading safe value "
109-
"for attribute '{0}'. Value: '{1}'".format(
108+
content='JSON tampering detected when loading safe value '
109+
'for attribute \'{0}\'. Value: \'{1}\''.format(
110110
context_object_name, value_as_str
111111
),
112112
content_type='text/plain'

0 commit comments

Comments
 (0)