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

@@ -5,15 +5,18 @@ from erpnext.e_commerce.product_data_engine.filters import ProductFiltersBuilder
sitemap = 1
def get_context(context):
# Add homepage as parent
context.body_class = "product-page"
context.parents = [{"name": frappe._("Home"), "route":"/"}]
context.parents = [{"name": frappe._("Home"), "route": "/"}]
filter_engine = ProductFiltersBuilder()
context.field_filters = filter_engine.get_field_filters()
context.attribute_filters = filter_engine.get_attribute_filters()
context.page_length = cint(frappe.db.get_single_value('E Commerce Settings', 'products_per_page'))or 20
context.page_length = (
cint(frappe.db.get_single_value("E Commerce Settings", "products_per_page")) or 20
)
context.no_cache = 1
context.no_cache = 1

View File

@@ -11,38 +11,46 @@ no_cache = 1
def get_context(context):
is_enabled = frappe.db.get_single_value('Appointment Booking Settings', 'enable_scheduling')
is_enabled = frappe.db.get_single_value("Appointment Booking Settings", "enable_scheduling")
if is_enabled:
return context
else:
frappe.redirect_to_message(_("Appointment Scheduling Disabled"), _("Appointment Scheduling has been disabled for this site"),
http_status_code=302, indicator_color="red")
frappe.redirect_to_message(
_("Appointment Scheduling Disabled"),
_("Appointment Scheduling has been disabled for this site"),
http_status_code=302,
indicator_color="red",
)
raise frappe.Redirect
@frappe.whitelist(allow_guest=True)
def get_appointment_settings():
settings = frappe.get_doc('Appointment Booking Settings')
settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
settings = frappe.get_doc("Appointment Booking Settings")
settings.holiday_list = frappe.get_doc("Holiday List", settings.holiday_list)
return settings
@frappe.whitelist(allow_guest=True)
def get_timezones():
import pytz
return pytz.all_timezones
@frappe.whitelist(allow_guest=True)
def get_appointment_slots(date, timezone):
# Convert query to local timezones
format_string = '%Y-%m-%d %H:%M:%S'
query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string)
query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string)
format_string = "%Y-%m-%d %H:%M:%S"
query_start_time = datetime.datetime.strptime(date + " 00:00:00", format_string)
query_end_time = datetime.datetime.strptime(date + " 23:59:59", format_string)
query_start_time = convert_to_system_timezone(timezone, query_start_time)
query_end_time = convert_to_system_timezone(timezone, query_end_time)
now = convert_to_guest_timezone(timezone, datetime.datetime.now())
# Database queries
settings = frappe.get_doc('Appointment Booking Settings')
holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
settings = frappe.get_doc("Appointment Booking Settings")
holiday_list = frappe.get_doc("Holiday List", settings.holiday_list)
timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
# Filter and convert timeslots
@@ -58,15 +66,15 @@ def get_appointment_slots(date, timezone):
converted_timeslots.append(dict(time=converted_timeslot, availability=True))
else:
converted_timeslots.append(dict(time=converted_timeslot, availability=False))
date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date()
date_required = datetime.datetime.strptime(date + " 00:00:00", format_string).date()
converted_timeslots = filter_timeslots(date_required, converted_timeslots)
return converted_timeslots
def get_available_slots_between(query_start_time, query_end_time, settings):
records = _get_records(query_start_time, query_end_time, settings)
timeslots = []
appointment_duration = datetime.timedelta(
minutes=settings.appointment_duration)
appointment_duration = datetime.timedelta(minutes=settings.appointment_duration)
for record in records:
if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
current_time = _deltatime_to_datetime(query_start_time, record.from_time)
@@ -82,33 +90,35 @@ def get_available_slots_between(query_start_time, query_end_time, settings):
@frappe.whitelist(allow_guest=True)
def create_appointment(date, time, tz, contact):
format_string = '%Y-%m-%d %H:%M:%S'
format_string = "%Y-%m-%d %H:%M:%S"
scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
# Strip tzinfo from datetime objects since it's handled by the doctype
scheduled_time = scheduled_time.replace(tzinfo = None)
scheduled_time = scheduled_time.replace(tzinfo=None)
scheduled_time = convert_to_system_timezone(tz, scheduled_time)
scheduled_time = scheduled_time.replace(tzinfo = None)
scheduled_time = scheduled_time.replace(tzinfo=None)
# Create a appointment document from form
appointment = frappe.new_doc('Appointment')
appointment = frappe.new_doc("Appointment")
appointment.scheduled_time = scheduled_time
contact = json.loads(contact)
appointment.customer_name = contact.get('name', None)
appointment.customer_phone_number = contact.get('number', None)
appointment.customer_skype = contact.get('skype', None)
appointment.customer_details = contact.get('notes', None)
appointment.customer_email = contact.get('email', None)
appointment.status = 'Open'
appointment.customer_name = contact.get("name", None)
appointment.customer_phone_number = contact.get("number", None)
appointment.customer_skype = contact.get("skype", None)
appointment.customer_details = contact.get("notes", None)
appointment.customer_email = contact.get("email", None)
appointment.status = "Open"
appointment.insert()
return appointment
# Helper Functions
def filter_timeslots(date, timeslots):
filtered_timeslots = []
for timeslot in timeslots:
if(timeslot['time'].date() == date):
if timeslot["time"].date() == date:
filtered_timeslots.append(timeslot)
return filtered_timeslots
def convert_to_guest_timezone(guest_tz, datetimeobject):
guest_tz = pytz.timezone(guest_tz)
local_timezone = pytz.timezone(frappe.utils.get_time_zone())
@@ -116,15 +126,18 @@ def convert_to_guest_timezone(guest_tz, datetimeobject):
datetimeobject = datetimeobject.astimezone(guest_tz)
return datetimeobject
def convert_to_system_timezone(guest_tz,datetimeobject):
def convert_to_system_timezone(guest_tz, datetimeobject):
guest_tz = pytz.timezone(guest_tz)
datetimeobject = guest_tz.localize(datetimeobject)
system_tz = pytz.timezone(frappe.utils.get_time_zone())
datetimeobject = datetimeobject.astimezone(system_tz)
return datetimeobject
def check_availabilty(timeslot, settings):
return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
return frappe.db.count("Appointment", {"scheduled_time": timeslot}) < settings.number_of_agents
def _is_holiday(date, holiday_list):
for holiday in holiday_list.holidays:
@@ -136,7 +149,10 @@ def _is_holiday(date, holiday_list):
def _get_records(start_time, end_time, settings):
records = []
for record in settings.availability_of_slots:
if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
if (
record.day_of_week == WEEKDAYS[start_time.weekday()]
or record.day_of_week == WEEKDAYS[end_time.weekday()]
):
records.append(record)
return records
@@ -148,4 +164,4 @@ def _deltatime_to_datetime(date, deltatime):
def _datetime_to_deltatime(date_time):
midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
return (date_time-midnight)
return date_time - midnight

View File

@@ -8,11 +8,11 @@ def get_context(context):
context.success = False
return context
email = frappe.form_dict['email']
appointment_name = frappe.form_dict['appointment']
email = frappe.form_dict["email"]
appointment_name = frappe.form_dict["appointment"]
if email and appointment_name:
appointment = frappe.get_doc('Appointment',appointment_name)
appointment = frappe.get_doc("Appointment", appointment_name)
appointment.set_verified(email)
context.success = True
return context

View File

@@ -4,28 +4,27 @@ import erpnext.education.utils as utils
no_cache = 1
def get_context(context):
# Load Query Parameters
try:
program = frappe.form_dict['program']
content = frappe.form_dict['content']
content_type = frappe.form_dict['type']
course = frappe.form_dict['course']
topic = frappe.form_dict['topic']
program = frappe.form_dict["program"]
content = frappe.form_dict["content"]
content_type = frappe.form_dict["type"]
course = frappe.form_dict["course"]
topic = frappe.form_dict["topic"]
except KeyError:
frappe.local.flags.redirect_location = '/lms'
frappe.local.flags.redirect_location = "/lms"
raise frappe.Redirect
# Check if user has access to the content
has_program_access = utils.allowed_program_access(program)
has_content_access = allowed_content_access(program, content, content_type)
if frappe.session.user == "Guest" or not has_program_access or not has_content_access:
frappe.local.flags.redirect_location = '/lms'
frappe.local.flags.redirect_location = "/lms"
raise frappe.Redirect
# Set context for content to be displayer
context.content = frappe.get_doc(content_type, content).as_dict()
context.content_type = content_type
@@ -34,35 +33,43 @@ def get_context(context):
context.topic = topic
topic = frappe.get_doc("Topic", topic)
content_list = [{'content_type':item.content_type, 'content':item.content} for item in topic.topic_content]
content_list = [
{"content_type": item.content_type, "content": item.content} for item in topic.topic_content
]
# Set context for progress numbers
context.position = content_list.index({'content': content, 'content_type': content_type})
context.position = content_list.index({"content": content, "content_type": content_type})
context.length = len(content_list)
# Set context for navigation
context.previous = get_previous_content(content_list, context.position)
context.next = get_next_content(content_list, context.position)
def get_next_content(content_list, current_index):
try:
return content_list[current_index + 1]
except IndexError:
return None
def get_previous_content(content_list, current_index):
if current_index == 0:
return None
else:
return content_list[current_index - 1]
def allowed_content_access(program, content, content_type):
contents_of_program = frappe.db.sql("""select `tabTopic Content`.content, `tabTopic Content`.content_type
contents_of_program = frappe.db.sql(
"""select `tabTopic Content`.content, `tabTopic Content`.content_type
from `tabCourse Topic`,
`tabProgram Course`,
`tabTopic Content`
where `tabCourse Topic`.parent = `tabProgram Course`.course
and `tabTopic Content`.parent = `tabCourse Topic`.topic
and `tabProgram Course`.parent = %(program)s""", {'program': program})
and `tabProgram Course`.parent = %(program)s""",
{"program": program},
)
return (content, content_type) in contents_of_program

View File

@@ -4,23 +4,25 @@ import erpnext.education.utils as utils
no_cache = 1
def get_context(context):
try:
program = frappe.form_dict['program']
course_name = frappe.form_dict['name']
program = frappe.form_dict["program"]
course_name = frappe.form_dict["name"]
except KeyError:
frappe.local.flags.redirect_location = '/lms'
frappe.local.flags.redirect_location = "/lms"
raise frappe.Redirect
context.education_settings = frappe.get_single("Education Settings")
course = frappe.get_doc('Course', course_name)
course = frappe.get_doc("Course", course_name)
context.program = program
context.course = course
context.topics = course.get_topics()
context.has_access = utils.allowed_program_access(context.program)
context.has_access = utils.allowed_program_access(context.program)
context.progress = get_topic_progress(context.topics, course, context.program)
def get_topic_progress(topics, course, program):
progress = {topic.name: utils.get_topic_progress(topic, course.name, program) for topic in topics}
return progress

View File

@@ -4,10 +4,11 @@ import erpnext.education.utils as utils
no_cache = 1
def get_context(context):
context.education_settings = frappe.get_single("Education Settings")
if not context.education_settings.enable_lms:
frappe.local.flags.redirect_location = '/'
frappe.local.flags.redirect_location = "/"
raise frappe.Redirect
context.featured_programs = get_featured_programs()

View File

@@ -4,23 +4,34 @@ import erpnext.education.utils as utils
no_cache = 1
def get_context(context):
if frappe.session.user == "Guest":
frappe.local.flags.redirect_location = '/lms'
frappe.local.flags.redirect_location = "/lms"
raise frappe.Redirect
context.student = utils.get_current_student()
if not context.student:
context.student = frappe.get_doc('User', frappe.session.user)
context.student = frappe.get_doc("User", frappe.session.user)
context.progress = get_program_progress(context.student.name)
def get_program_progress(student):
enrolled_programs = frappe.get_all("Program Enrollment", filters={'student':student}, fields=['program'])
enrolled_programs = frappe.get_all(
"Program Enrollment", filters={"student": student}, fields=["program"]
)
student_progress = []
for list_item in enrolled_programs:
program = frappe.get_doc("Program", list_item.program)
progress = utils.get_program_progress(program)
completion = utils.get_program_completion(program)
student_progress.append({'program': program.program_name, 'name': program.name, 'progress':progress, 'completion': completion})
student_progress.append(
{
"program": program.program_name,
"name": program.name,
"progress": progress,
"completion": completion,
}
)
return student_progress

View File

@@ -5,11 +5,12 @@ import erpnext.education.utils as utils
no_cache = 1
def get_context(context):
try:
program = frappe.form_dict['program']
program = frappe.form_dict["program"]
except KeyError:
frappe.local.flags.redirect_location = '/lms'
frappe.local.flags.redirect_location = "/lms"
raise frappe.Redirect
context.education_settings = frappe.get_single("Education Settings")
@@ -18,12 +19,14 @@ def get_context(context):
context.has_access = utils.allowed_program_access(program)
context.progress = get_course_progress(context.courses, context.program)
def get_program(program_name):
try:
return frappe.get_doc('Program', program_name)
return frappe.get_doc("Program", program_name)
except frappe.DoesNotExistError:
frappe.throw(_("Program {0} does not exist.").format(program_name))
def get_course_progress(courses, program):
progress = {course.name: utils.get_course_progress(course, program) for course in courses}
return progress or {}

View File

@@ -4,20 +4,22 @@ import erpnext.education.utils as utils
no_cache = 1
def get_context(context):
try:
course = frappe.form_dict['course']
program = frappe.form_dict['program']
topic = frappe.form_dict['topic']
course = frappe.form_dict["course"]
program = frappe.form_dict["program"]
topic = frappe.form_dict["topic"]
except KeyError:
frappe.local.flags.redirect_location = '/lms'
frappe.local.flags.redirect_location = "/lms"
raise frappe.Redirect
context.program = program
context.course = course
context.topic = frappe.get_doc("Topic", topic)
context.contents = get_contents(context.topic, course, program)
context.has_access = utils.allowed_program_access(program)
context.has_access = utils.allowed_program_access(program)
def get_contents(topic, course, program):
student = utils.get_current_student()
@@ -27,19 +29,29 @@ def get_contents(topic, course, program):
progress = []
if contents:
for content in contents:
if content.doctype in ('Article', 'Video'):
if content.doctype in ("Article", "Video"):
if student:
status = utils.check_content_completion(content.name, content.doctype, course_enrollment.name)
else:
status = True
progress.append({'content': content, 'content_type': content.doctype, 'completed': status})
elif content.doctype == 'Quiz':
progress.append({"content": content, "content_type": content.doctype, "completed": status})
elif content.doctype == "Quiz":
if student:
status, score, result, time_taken = utils.check_quiz_completion(content, course_enrollment.name)
status, score, result, time_taken = utils.check_quiz_completion(
content, course_enrollment.name
)
else:
status = False
score = None
result = None
progress.append({'content': content, 'content_type': content.doctype, 'completed': status, 'score': score, 'result': result})
progress.append(
{
"content": content,
"content_type": content.doctype,
"completed": status,
"score": score,
"result": result,
}
)
return progress

View File

@@ -2,18 +2,23 @@ import frappe
no_cache = 1
def get_context(context):
if frappe.session.user != 'Guest':
if frappe.session.user != "Guest":
context.all_certifications = get_all_certifications_of_a_member()
context.show_sidebar = True
def get_all_certifications_of_a_member():
'''Returns all certifications'''
"""Returns all certifications"""
all_certifications = []
all_certifications = frappe.db.sql(""" select cc.name,cc.from_date,cc.to_date,ca.amount,ca.currency
all_certifications = frappe.db.sql(
""" select cc.name,cc.from_date,cc.to_date,ca.amount,ca.currency
from `tabCertified Consultant` cc
inner join `tabCertification Application` ca
on cc.certification_application = ca.name
where paid = 1 and email = %(user)s order by cc.to_date desc""" ,{'user': frappe.session.user},as_dict=True)
where paid = 1 and email = %(user)s order by cc.to_date desc""",
{"user": frappe.session.user},
as_dict=True,
)
return all_certifications

View File

@@ -3,6 +3,7 @@ from frappe import _
sitemap = 1
def get_context(context):
context.body_class = "product-page"
@@ -18,13 +19,9 @@ def get_context(context):
context.no_cache = 1
def get_slideshow(slideshow):
values = {
'show_indicators': 1,
'show_controls': 1,
'rounded': 1,
'slider_name': "Categories"
}
values = {"show_indicators": 1, "show_controls": 1, "rounded": 1, "slider_name": "Categories"}
slideshow = frappe.get_cached_doc("Website Slideshow", slideshow)
slides = slideshow.get({"doctype": "Website Slideshow Item"})
for index, slide in enumerate(slides, start=1):
@@ -37,9 +34,10 @@ def get_slideshow(slideshow):
return values
def get_tabs(categories):
tab_values = {
'title': _("Shop by Category"),
"title": _("Shop by Category"),
}
categorical_data = get_category_records(categories)
@@ -48,21 +46,19 @@ def get_tabs(categories):
# pre-render cards for each tab
tab_values[f"tab_{index + 1}_content"] = frappe.render_template(
"erpnext/www/shop-by-category/category_card_section.html",
{"data": categorical_data[tab], "type": tab}
{"data": categorical_data[tab], "type": tab},
)
return tab_values
def get_category_records(categories):
categorical_data = {}
for category in categories:
if category == "item_group":
categorical_data["item_group"] = frappe.db.get_all(
"Item Group",
filters={
"parent_item_group": "All Item Groups",
"show_in_website": 1
},
fields=["name", "parent_item_group", "is_group", "image", "route"]
filters={"parent_item_group": "All Item Groups", "show_in_website": 1},
fields=["name", "parent_item_group", "is_group", "image", "route"],
)
else:
doctype = frappe.unscrub(category)
@@ -73,4 +69,3 @@ def get_category_records(categories):
categorical_data[category] = frappe.db.get_all(doctype, fields=fields)
return categorical_data

View File

@@ -3,7 +3,7 @@ import frappe
def get_context(context):
context.no_cache = 1
context.align_greeting = ''
context.align_greeting = ""
setting = frappe.get_doc("Support Settings")
context.greeting_title = setting.greeting_title
@@ -16,18 +16,22 @@ def get_context(context):
if favorite_articles:
for article in favorite_articles:
name_list.append(article.name)
for record in (frappe.get_all("Help Article",
for record in frappe.get_all(
"Help Article",
fields=["title", "content", "route", "category"],
filters={"name": ['not in', tuple(name_list)], "published": 1},
order_by="creation desc", limit=(6-len(favorite_articles)))):
filters={"name": ["not in", tuple(name_list)], "published": 1},
order_by="creation desc",
limit=(6 - len(favorite_articles)),
):
favorite_articles.append(record)
context.favorite_article_list = get_favorite_articles(favorite_articles)
context.help_article_list = get_help_article_list()
def get_favorite_articles_by_page_view():
return frappe.db.sql(
"""
"""
SELECT
t1.name as name,
t1.title as title,
@@ -43,32 +47,42 @@ def get_favorite_articles_by_page_view():
GROUP BY route
ORDER BY count DESC
LIMIT 6;
""", as_dict=True)
""",
as_dict=True,
)
def get_favorite_articles(favorite_articles):
favorite_article_list=[]
favorite_article_list = []
for article in favorite_articles:
description = frappe.utils.strip_html(article.content)
if len(description) > 120:
description = description[:120] + '...'
description = description[:120] + "..."
favorite_article_dict = {
'title': article.title,
'description': description,
'route': article.route,
'category': article.category,
"title": article.title,
"description": description,
"route": article.route,
"category": article.category,
}
favorite_article_list.append(favorite_article_dict)
return favorite_article_list
def get_help_article_list():
help_article_list=[]
help_article_list = []
category_list = frappe.get_all("Help Category", fields="name")
for category in category_list:
help_articles = frappe.get_all("Help Article", fields="*", filters={"category": category.name, "published": 1}, order_by="modified desc", limit=5)
help_articles = frappe.get_all(
"Help Article",
fields="*",
filters={"category": category.name, "published": 1},
order_by="modified desc",
limit=5,
)
if help_articles:
help_aricles_per_caetgory = {
'category': category,
'articles': help_articles,
"category": category,
"articles": help_articles,
}
help_article_list.append(help_aricles_per_caetgory)
return help_article_list