From 01794a47c6775a6ffb5380cbd7ae37e4029efb4c Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 25 Jul 2025 17:46:50 +0530 Subject: [PATCH] feat: set a random color is no color or image is present --- frontend/src/components/CourseCard.vue | 2 +- lms/lms/doctype/lms_course/lms_course.json | 4 ++-- lms/lms/doctype/lms_course/lms_course.py | 19 +++++++++++++++++++ lms/lms/utils.py | 11 +++++++---- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/CourseCard.vue b/frontend/src/components/CourseCard.vue index ff730b09..0f66332f 100644 --- a/frontend/src/components/CourseCard.vue +++ b/frontend/src/components/CourseCard.vue @@ -145,7 +145,7 @@ const props = defineProps({ }) const getGradientColor = () => { - let color = props.course.card_gradient.toLowerCase() + let color = props.course.card_gradient?.toLowerCase() || 'blue' let colorMap = theme.backgroundColor[color] return `linear-gradient(to top right, black, ${colorMap[400]})` /* return `bg-gradient-to-br from-${color}-100 via-${color}-200 to-${color}-400` */ diff --git a/lms/lms/doctype/lms_course/lms_course.json b/lms/lms/doctype/lms_course/lms_course.json index 6bf40925..34e9352d 100644 --- a/lms/lms/doctype/lms_course/lms_course.json +++ b/lms/lms/doctype/lms_course/lms_course.json @@ -277,7 +277,7 @@ "fieldname": "card_gradient", "fieldtype": "Select", "label": "Card Gradient", - "options": "Red\nBlue\nGreen\nAmber\nCyan\nOrange\nPink\nPurple\nTeal\nViolet\nYellow" + "options": "Red\nBlue\nGreen\nAmber\nCyan\nOrange\nPink\nPurple\nTeal\nViolet\nYellow\nGray" } ], "is_published_field": "published", @@ -296,7 +296,7 @@ } ], "make_attachments_public": 1, - "modified": "2025-07-23 18:40:28.707756", + "modified": "2025-07-25 17:30:48.553707", "modified_by": "sayali@frappe.io", "module": "LMS", "name": "LMS Course", diff --git a/lms/lms/doctype/lms_course/lms_course.py b/lms/lms/doctype/lms_course/lms_course.py index b05f8e38..a9f4e21b 100644 --- a/lms/lms/doctype/lms_course/lms_course.py +++ b/lms/lms/doctype/lms_course/lms_course.py @@ -21,6 +21,7 @@ class LMSCourse(Document): self.validate_certification() self.validate_amount_and_currency() self.image = validate_image(self.image) + self.validate_card_gradient() def validate_published(self): if self.published and not self.published_on: @@ -73,6 +74,24 @@ class LMSCourse(Document): if self.paid_certificate and (cint(self.course_price) <= 0 or not self.currency): frappe.throw(_("Amount and currency are required for paid certificates.")) + def validate_card_gradient(self): + if not self.image and not self.card_gradient: + colors = [ + "Red", + "Blue", + "Green", + "Yellow", + "Orange", + "Pink", + "Amber", + "Violet", + "Cyan", + "Teal", + "Gray", + "Purple", + ] + self.card_gradient = random.choice(colors) + def on_update(self): if not self.upcoming and self.has_value_changed("upcoming"): self.send_email_to_interested_users() diff --git a/lms/lms/utils.py b/lms/lms/utils.py index 77de5e78..17ba1b35 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -565,10 +565,13 @@ def get_courses_under_review(): def validate_image(path): if path and "/private" in path: - file = frappe.get_doc("File", {"file_url": path}) - file.is_private = 0 - file.save() - return file.file_url + frappe.db.set_value( + "File", + {"file_url": path}, + "is_private", + 0, + ) + return path.replace("/private", "") return path