style: format code with black

This commit is contained in:
Ankush Menat
2022-03-28 18:52:46 +05:30
parent 21e00da3d6
commit 494bd9ef78
1395 changed files with 91704 additions and 62532 deletions

View File

@@ -11,17 +11,27 @@ class Homepage(Document):
def validate(self):
if not self.description:
self.description = frappe._("This is an example website auto-generated from ERPNext")
delete_page_cache('home')
delete_page_cache("home")
def setup_items(self):
for d in frappe.get_all('Website Item', fields=['name', 'item_name', 'description', 'image', 'route'],
filters={'published': 1}, limit=3):
for d in frappe.get_all(
"Website Item",
fields=["name", "item_name", "description", "image", "route"],
filters={"published": 1},
limit=3,
):
doc = frappe.get_doc('Website Item', d.name)
doc = frappe.get_doc("Website Item", d.name)
if not doc.route:
# set missing route
doc.save()
self.append('products', dict(item_code=d.name,
item_name=d.item_name, description=d.description,
image=d.image, route=d.route))
self.append(
"products",
dict(
item_code=d.name,
item_name=d.item_name,
description=d.description,
image=d.image,
route=d.route,
),
)

View File

@@ -10,7 +10,7 @@ from frappe.website.serve import get_response
class TestHomepage(unittest.TestCase):
def test_homepage_load(self):
set_request(method='GET', path='home')
set_request(method="GET", path="home")
response = get_response()
self.assertEqual(response.status_code, 200)

View File

@@ -12,65 +12,79 @@ from frappe.website.serve import get_response
class TestHomepageSection(unittest.TestCase):
def test_homepage_section_card(self):
try:
frappe.get_doc({
'doctype': 'Homepage Section',
'name': 'Card Section',
'section_based_on': 'Cards',
'section_cards': [
{'title': 'Card 1', 'subtitle': 'Subtitle 1', 'content': 'This is test card 1', 'route': '/card-1'},
{'title': 'Card 2', 'subtitle': 'Subtitle 2', 'content': 'This is test card 2', 'image': 'test.jpg'},
],
'no_of_columns': 3
}).insert(ignore_if_duplicate=True)
frappe.get_doc(
{
"doctype": "Homepage Section",
"name": "Card Section",
"section_based_on": "Cards",
"section_cards": [
{
"title": "Card 1",
"subtitle": "Subtitle 1",
"content": "This is test card 1",
"route": "/card-1",
},
{
"title": "Card 2",
"subtitle": "Subtitle 2",
"content": "This is test card 2",
"image": "test.jpg",
},
],
"no_of_columns": 3,
}
).insert(ignore_if_duplicate=True)
except frappe.DuplicateEntryError:
pass
set_request(method='GET', path='home')
set_request(method="GET", path="home")
response = get_response()
self.assertEqual(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, 'html.parser')
sections = soup.find('main').find_all('section')
soup = BeautifulSoup(html, "html.parser")
sections = soup.find("main").find_all("section")
self.assertEqual(len(sections), 3)
homepage_section = sections[2]
self.assertEqual(homepage_section.h3.text, 'Card Section')
self.assertEqual(homepage_section.h3.text, "Card Section")
cards = homepage_section.find_all(class_="card")
self.assertEqual(len(cards), 2)
self.assertEqual(cards[0].h5.text, 'Card 1')
self.assertEqual(cards[0].a['href'], '/card-1')
self.assertEqual(cards[1].p.text, 'Subtitle 2')
self.assertEqual(cards[1].find(class_='website-image-lazy')['data-src'], 'test.jpg')
self.assertEqual(cards[0].h5.text, "Card 1")
self.assertEqual(cards[0].a["href"], "/card-1")
self.assertEqual(cards[1].p.text, "Subtitle 2")
self.assertEqual(cards[1].find(class_="website-image-lazy")["data-src"], "test.jpg")
# cleanup
frappe.db.rollback()
def test_homepage_section_custom_html(self):
frappe.get_doc({
'doctype': 'Homepage Section',
'name': 'Custom HTML Section',
'section_based_on': 'Custom HTML',
'section_html': '<div class="custom-section">My custom html</div>',
}).insert()
frappe.get_doc(
{
"doctype": "Homepage Section",
"name": "Custom HTML Section",
"section_based_on": "Custom HTML",
"section_html": '<div class="custom-section">My custom html</div>',
}
).insert()
set_request(method='GET', path='home')
set_request(method="GET", path="home")
response = get_response()
self.assertEqual(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
soup = BeautifulSoup(html, 'html.parser')
sections = soup.find('main').find_all(class_='custom-section')
soup = BeautifulSoup(html, "html.parser")
sections = soup.find("main").find_all(class_="custom-section")
self.assertEqual(len(sections), 1)
homepage_section = sections[0]
self.assertEqual(homepage_section.text, 'My custom html')
self.assertEqual(homepage_section.text, "My custom html")
# cleanup
frappe.db.rollback()

View File

@@ -8,40 +8,41 @@ from erpnext.e_commerce.shopping_cart.cart import get_debtors_account
def set_default_role(doc, method):
'''Set customer, supplier, student, guardian based on email'''
"""Set customer, supplier, student, guardian based on email"""
if frappe.flags.setting_role or frappe.flags.in_migrate:
return
roles = frappe.get_roles(doc.name)
contact_name = frappe.get_value('Contact', dict(email_id=doc.email))
contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
if contact_name:
contact = frappe.get_doc('Contact', contact_name)
contact = frappe.get_doc("Contact", contact_name)
for link in contact.links:
frappe.flags.setting_role = True
if link.link_doctype=='Customer' and 'Customer' not in roles:
doc.add_roles('Customer')
elif link.link_doctype=='Supplier' and 'Supplier' not in roles:
doc.add_roles('Supplier')
elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles:
doc.add_roles('Student')
elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles:
doc.add_roles('Guardian')
if link.link_doctype == "Customer" and "Customer" not in roles:
doc.add_roles("Customer")
elif link.link_doctype == "Supplier" and "Supplier" not in roles:
doc.add_roles("Supplier")
elif frappe.get_value("Student", dict(student_email_id=doc.email)) and "Student" not in roles:
doc.add_roles("Student")
elif frappe.get_value("Guardian", dict(email_address=doc.email)) and "Guardian" not in roles:
doc.add_roles("Guardian")
def create_customer_or_supplier():
'''Based on the default Role (Customer, Supplier), create a Customer / Supplier.
"""Based on the default Role (Customer, Supplier), create a Customer / Supplier.
Called on_session_creation hook.
'''
"""
user = frappe.session.user
if frappe.db.get_value('User', user, 'user_type') != 'Website User':
if frappe.db.get_value("User", user, "user_type") != "Website User":
return
user_roles = frappe.get_roles()
portal_settings = frappe.get_single('Portal Settings')
portal_settings = frappe.get_single("Portal Settings")
default_role = portal_settings.default_role
if default_role not in ['Customer', 'Supplier']:
if default_role not in ["Customer", "Supplier"]:
return
# create customer / supplier if the user has that role
@@ -59,34 +60,33 @@ def create_customer_or_supplier():
party = frappe.new_doc(doctype)
fullname = frappe.utils.get_fullname(user)
if doctype == 'Customer':
if doctype == "Customer":
cart_settings = get_shopping_cart_settings()
if cart_settings.enable_checkout:
debtors_account = get_debtors_account(cart_settings)
else:
debtors_account = ''
debtors_account = ""
party.update({
"customer_name": fullname,
"customer_type": "Individual",
"customer_group": cart_settings.default_customer_group,
"territory": get_root_of("Territory")
})
party.update(
{
"customer_name": fullname,
"customer_type": "Individual",
"customer_group": cart_settings.default_customer_group,
"territory": get_root_of("Territory"),
}
)
if debtors_account:
party.update({
"accounts": [{
"company": cart_settings.company,
"account": debtors_account
}]
})
party.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]})
else:
party.update({
"supplier_name": fullname,
"supplier_group": "All Supplier Groups",
"supplier_type": "Individual"
})
party.update(
{
"supplier_name": fullname,
"supplier_group": "All Supplier Groups",
"supplier_type": "Individual",
}
)
party.flags.ignore_mandatory = True
party.insert(ignore_permissions=True)
@@ -95,28 +95,27 @@ def create_customer_or_supplier():
if party_exists(alternate_doctype, user):
# if user is both customer and supplier, alter fullname to avoid contact name duplication
fullname += "-" + doctype
fullname += "-" + doctype
create_party_contact(doctype, fullname, user, party.name)
return party
def create_party_contact(doctype, fullname, user, party_name):
contact = frappe.new_doc("Contact")
contact.update({
"first_name": fullname,
"email_id": user
})
contact.append('links', dict(link_doctype=doctype, link_name=party_name))
contact.append('email_ids', dict(email_id=user))
contact.update({"first_name": fullname, "email_id": user})
contact.append("links", dict(link_doctype=doctype, link_name=party_name))
contact.append("email_ids", dict(email_id=user))
contact.flags.ignore_mandatory = True
contact.insert(ignore_permissions=True)
def party_exists(doctype, user):
# check if contact exists against party and if it is linked to the doctype
contact_name = frappe.db.get_value("Contact", {"email_id": user})
if contact_name:
contact = frappe.get_doc('Contact', contact_name)
contact = frappe.get_doc("Contact", contact_name)
doctypes = [d.link_doctype for d in contact.links]
return doctype in doctypes