Skip to content

Commit

Permalink
Fix some linting
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Jul 11, 2024
1 parent 0d2917f commit b269195
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 73 deletions.
4 changes: 4 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ lint.select = [
lint.ignore = [
# DoNotAssignLambda
"E731",
"S602",
"SIM102",
"S314",
"S324"
]

[tool.ruff.format]
Expand Down
4 changes: 1 addition & 3 deletions backend/scripts/create_site.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from AccessControl.SecurityManagement import newSecurityManager
from collective.volto.formsupport.interfaces import (
ICollectiveVoltoFormsupportLayer,
)
from collective.volto.formsupport.interfaces import ICollectiveVoltoFormsupportLayer
from Products.CMFPlone.factory import _DEFAULT_PROFILE
from Products.CMFPlone.factory import addPloneSite
from Products.GenericSetup.tool import SetupTool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class EmailConfirmView(BrowserView):
def __call__(self, token="alksdjfakls", *args, **kwargs):
def __call__(self, token="alksdjfakls", *args, **kwargs): # noqa: S107
self.token = token

return super().__call__(*args, **kwargs)
Expand Down
6 changes: 4 additions & 2 deletions backend/src/collective/volto/formsupport/captcha/honeypot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def verify(self, data):
context=self.request,
)
# first check if volto-form-block send the compiled token
# (because by default it does not insert the honeypot field into the submitted form)
# (because by default it does not insert the honeypot field into the submitted
# form)
if not data:
# @submit-form has been called not from volto-form-block so do the standard validation.
# @submit-form has been called not from volto-form-block so do the standard
# validation.
form_data = json_body(self.request).get("data", [])
form = {x["label"]: x["value"] for x in form_data}
if found_honeypot(form, required=True):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@implementer(ICatalogFactory)
class FormDataSoupCatalogFactory:
def __call__(self, context):
#  do not set any index here..maybe on each form
# do not set any index here..maybe on each form
catalog = Catalog()
block_id_indexer = NodeAttributeIndexer("block_id")
catalog["block_id"] = CatalogFieldIndex(block_id_indexer)
Expand Down Expand Up @@ -61,7 +61,8 @@ def get_form_fields(self):

subblocks = form_block.get("subblocks", [])

# Add the 'custom_field_id' field back in as this isn't stored with each subblock
# Add the 'custom_field_id' field back in as this isn't stored with each
# subblock
for index, field in enumerate(subblocks):
if form_block.get(field["field_id"]):
subblocks[index]["custom_field_id"] = form_block.get(field["field_id"])
Expand All @@ -72,7 +73,7 @@ def add(self, data):
form_fields = self.get_form_fields()
if not form_fields:
logger.error(
f'Block with id {self.block_id} and type "form" not found in context: {self.context.absolute_url()}.'
f'Block with id {self.block_id} and type "form" not found in context: {self.context.absolute_url()}.' # noqa: E501
)
return None

Expand Down
17 changes: 3 additions & 14 deletions backend/src/collective/volto/formsupport/locales/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ def locale_folder_setup():
else:
lc_messages_path = lang + "/LC_MESSAGES/"
os.mkdir(lc_messages_path)
cmd = "msginit --locale={} --input={}.pot --output={}/LC_MESSAGES/{}.po".format( # NOQA: E501
lang,
domain,
lang,
domain,
)
cmd = f"msginit --locale={lang} --input={domain}.pot --output={lang}/LC_MESSAGES/{domain}.po" # noqa: E501
subprocess.call(
cmd,
shell=True,
Expand All @@ -39,21 +34,15 @@ def locale_folder_setup():


def _rebuild():
cmd = "{i18ndude} rebuild-pot --pot {locale_path}/{domain}.pot --exclude {excludes} --create {domain} {target_path}".format( # NOQA: E501
i18ndude=i18ndude,
locale_path=locale_path,
domain=domain,
target_path=target_path,
excludes=excludes,
)
cmd = f"{i18ndude} rebuild-pot --pot {locale_path}/{domain}.pot --exclude {excludes} --create {domain} {target_path}" # noqa: E501
subprocess.call(
cmd,
shell=True,
)


def _sync():
cmd = f"{i18ndude} sync --pot {locale_path}/{domain}.pot {locale_path}*/LC_MESSAGES/{domain}.po"
cmd = f"{i18ndude} sync --pot {locale_path}/{domain}.pot {locale_path}*/LC_MESSAGES/{domain}.po" # noqa: E501
subprocess.call(
cmd,
shell=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ def __init__(self, context, request):

def get_ordered_keys(self, record):
"""
We need this method because we want to maintain the fields order set in the form.
The form can also change during time, and each record can have different fields stored in it.
We need this method because we want to maintain the fields order set in
the form.
The form can also change during time, and each record can have different
fields stored in it.
"""
record_order = record.attrs.get("fields_order", [])
if record_order:
Expand All @@ -42,8 +44,9 @@ def get_ordered_keys(self, record):
for k in self.form_fields_order:
if k in record.attrs:
order.append(k)
# finally append the keys stored in the record but that are not in the form (maybe the form changed during time)
for k in record.attrs.keys():
# finally append the keys stored in the record but that are not in the form
# (maybe the form changed during time)
for k in record.attrs:
if k not in order and k not in SKIP_ATTRS:
order.append(k)
return order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def reply(self):
message = translate(
_(
"mail_send_exception",
default="Unable to send confirm email. Please retry later or contact site administrator.",
default="Unable to send confirm email. Please retry later or contact site administrator.", # noqa: E501
),
context=self.request,
)
Expand Down Expand Up @@ -137,7 +137,7 @@ def validate_form(self):
translate(
_(
"block_form_not_found_label",
default='Block with @type "form" and id "$block" not found in this context: $context',
default='Block with @type "form" and id "$block" not found in this context: $context', # noqa: E501
mapping={
"block": self.block_id,
"context": self.context.absolute_url(),
Expand All @@ -152,7 +152,7 @@ def validate_form(self):
translate(
_(
"missing_action",
default='You need to set at least one form action between "send" and "store".', # noqa
default='You need to set at least one form action between "send" and "store".', # noqa: E501
),
context=self.request,
)
Expand Down Expand Up @@ -327,7 +327,8 @@ def get_subject(self):
if not field_id:
continue

# Handle this kind of id format: `field_name_123321, whichj is used by frontend package logics
# Handle this kind of id format: `field_name_123321,
# which is used by frontend package logics
pattern = r"\$\{[^}]+\}"
matches = re.findall(pattern, subject)

Expand Down
6 changes: 3 additions & 3 deletions backend/src/collective/volto/formsupport/scripts/cleansing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@click.command(
help="bin/instance -OPlone run bin/formsupport_data_cleansing [--dryrun|--no-dryrun]",
help="bin/instance -OPlone run bin/formsupport_data_cleansing [--dryrun|--no-dryrun]", # noqa: E501
context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
Expand All @@ -22,7 +22,7 @@
default=True,
help="--dryrun (default) simulate, --no-dryrun actually save the changes",
)
def main(dryrun):
def main(dryrun): # noqa: C901
# import pdb;pdb.set_trace()
if dryrun:
print("CHECK ONLY")
Expand Down Expand Up @@ -59,7 +59,7 @@ def main(dryrun):
deleted += 1
if deleted:
print(
f"[INFO] removed {deleted} records from {brain.getPath()} block: {block_id}"
f"[INFO] removed {deleted} records from {brain.getPath()} block: {block_id}" # noqa: E501
)
if not dryrun:
print("COMMIT")
Expand Down
34 changes: 19 additions & 15 deletions backend/src/collective/volto/formsupport/tests/test_captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_recaptcha_no_settings(self):
self.assertEqual(response.status_code, 500)
self.assertEqual(
response.json()["message"],
"No recaptcha private key configured. Go to path/to/site/@@recaptcha-settings "
"No recaptcha private key configured. Go to path/to/site/@@recaptcha-settings " # noqa: E501
"to configure.",
)

Expand Down Expand Up @@ -379,13 +379,15 @@ def test_norobots(self):
}
transaction.commit()

captcha_token = json.dumps({
"value": "5",
"id": "question0",
"id_check": md5(
"Write five using ciphers".encode("ascii", "ignore")
).hexdigest(),
})
captcha_token = json.dumps(
{
"value": "5",
"id": "question0",
"id_check": md5(
"Write five using ciphers".encode("ascii", "ignore")
).hexdigest(),
}
)

response = self.submit_form(
data={
Expand Down Expand Up @@ -432,13 +434,15 @@ def test_norobots_wrong_captcha(self):
}
transaction.commit()

captcha_token = json.dumps({
"value": "15",
"id": "question0",
"id_check": md5(
"Write five using ciphers".encode("ascii", "ignore")
).hexdigest(),
})
captcha_token = json.dumps(
{
"value": "15",
"id": "question0",
"id_check": md5(
"Write five using ciphers".encode("ascii", "ignore")
).hexdigest(),
}
)

response = self.submit_form(
data={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def tearDown(self):

def test_deserializer_cleanup_data_in_send_message_field(self):
new_blocks = deepcopy(self.document.blocks)
new_blocks["form-id"]["send_message"] = (
"<b onmouseover=\"alert('XSS testing!')\">click here</b><p><i>keep tags</i></p>"
)
new_blocks["form-id"][
"send_message"
] = "<b onmouseover=\"alert('XSS testing!')\">click here</b><p><i>keep tags</i></p>" # noqa: E501
self.api_session.patch(
self.document_url,
json={"blocks": new_blocks},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def test_honeypot_field_in_form_compiled_fail_validation(self):

def test_form_submitted_from_volto_valid(self):
"""
when you compile the form from volto, the honey field value is passed into captcha value
when you compile the form from volto, the honey field value is passed into
captcha value
"""
self.document.blocks = {
"text-id": {"@type": "text"},
Expand Down Expand Up @@ -245,7 +246,8 @@ def test_form_submitted_from_volto_valid(self):

def test_form_submitted_from_volto_invalid_because_missing_value(self):
"""
when you compile the form from volto, the honey field value is passed into captcha value
when you compile the form from volto, the honey field value is passed into
captcha value
"""
self.document.blocks = {
"text-id": {"@type": "text"},
Expand Down Expand Up @@ -295,7 +297,8 @@ def test_form_submitted_from_volto_invalid_because_missing_value(self):

def test_form_submitted_from_volto_invalid_because_compiled(self):
"""
when you compile the form from volto, the honey field value is passed into captcha value
when you compile the form from volto, the honey field value is passed into
captcha value
"""
self.document.blocks = {
"text-id": {"@type": "text"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ def test_email_not_send_if_block_id_is_incorrect_or_not_present(self):
self.assertEqual(response.status_code, 400)
self.assertEqual(
res["message"],
'Block with @type "form" and id "unknown" not found in this context: {}'.format( # noqa
self.document_url
),
f'Block with @type "form" and id "unknown" not found in this context: {self.document_url}', # noqa: E501
)

response = self.submit_form(
Expand All @@ -120,9 +118,7 @@ def test_email_not_send_if_block_id_is_incorrect_or_not_present(self):
self.assertEqual(response.status_code, 400)
self.assertEqual(
res["message"],
'Block with @type "form" and id "text-id" not found in this context: {}'.format( # noqa
self.document_url
),
f'Block with @type "form" and id "text-id" not found in this context: {self.document_url}', # noqa: E501
)

def test_email_not_send_if_no_action_set(self):
Expand Down Expand Up @@ -814,7 +810,7 @@ def test_send_only_acknowledgement(self):
"send": ["acknowledgement"],
"acknowledgementFields": "contact",
"acknowledgementMessage": {
"data": "<p>This message will be sent to the person filling in the form.</p><p>It is <strong>Rich Text</strong></p>"
"data": "<p>This message will be sent to the person filling in the form.</p><p>It is <strong>Rich Text</strong></p>" # noqa: E501
},
"subblocks": [
{
Expand Down Expand Up @@ -877,7 +873,7 @@ def test_send_recipient_and_acknowledgement(self):
"send": ["recipient", "acknowledgement"],
"acknowledgementFields": "contact",
"acknowledgementMessage": {
"data": "<p>This message will be sent to the person filling in the form.</p><p>It is <strong>Rich Text</strong></p>"
"data": "<p>This message will be sent to the person filling in the form.</p><p>It is <strong>Rich Text</strong></p>" # noqa: E501
},
"subblocks": [
{
Expand Down Expand Up @@ -1339,12 +1335,12 @@ def test_cleanup_html_in_submitted_data(self):
{
"field_id": "message",
"label": "Message",
"value": "<b onmouseover=\"alert('XSS testing!')\">click here</b><p><i>keep tags</i></p>",
"value": "<b onmouseover=\"alert('XSS testing!')\">click here</b><p><i>keep tags</i></p>", # noqa: E501
},
{
"field_id": "name",
"label": "Name",
"value": "<script>alert(XSS)</script> foo",
"value": "<script>alert('XSS')</script> foo",
},
],
"subject": "test subject",
Expand All @@ -1366,7 +1362,7 @@ def test_cleanup_html_in_submitted_data(self):
{
"field_id": "name",
"label": "Name",
"value": "alert(XSS) foo",
"value": "alert('XSS') foo",
},
]
},
Expand Down
4 changes: 2 additions & 2 deletions backend/src/collective/volto/formsupport/upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _get_all_content_with_blocks():
logger.info(f"Progress: {i + 1}/{total}")
item = brain.getObject()
for schema in iterSchemata(item.aq_base):
for name, field in getFields(schema).items():
for name, _ in getFields(schema).items():
if name == "blocks":
if _has_block_form(getattr(item, "blocks", {})):
content.append(item)
Expand Down Expand Up @@ -212,7 +212,7 @@ def update_send_from_bool_to_list_for_content(item):
new_send_value = ["recipient"] if block.get("send") else []
block["send"] = new_send_value
logger.info(
f"[CONVERTED] - {item} form send value from {send} to {new_send_value}"
f"[CONVERTED] - {item} form send value from {send} to {new_send_value}" # noqa: E501
)

item.blocks = blocks
Expand Down
12 changes: 7 additions & 5 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@


globals().update(
fixtures_factory((
(ACCEPTANCE_TESTING, "acceptance"),
(FUNCTIONAL_TESTING, "functional"),
(INTEGRATION_TESTING, "integration"),
))
fixtures_factory(
(
(ACCEPTANCE_TESTING, "acceptance"),
(FUNCTIONAL_TESTING, "functional"),
(INTEGRATION_TESTING, "integration"),
)
)
)

0 comments on commit b269195

Please sign in to comment.