Skip to content

Commit

Permalink
Added support for uploading course preview image
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed Nov 27, 2021
1 parent ac720f4 commit a20d387
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions build/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import os
from pathlib import Path
from urllib.parse import urljoin

import frontmatter
from frappeclient import FrappeClient
Expand Down Expand Up @@ -73,6 +74,19 @@ def save_document(self, doctype, name, doc):
print(doctype, name, "saving...")
return self.invoke_method("mon_school.api.save_document", data=data)

def add_attachment(self, filename, doctype, docname, fieldname):
files = {"file": open(filename, "rb")}
data = {
"is_private": 0,
"doctype": doctype,
"docname": docname,
"fieldname": fieldname,
"folder": "Home"
}
url = urljoin(self.frappe.url, "/api/method/upload_file")
r = self.frappe.session.post(url, files=files, data=data)
return r.json()['message']['file_url']

def trim_tables(self, doc):
def trim(d):
ignore = "name owner creation modified modified_by parent parentfield parenttype idx docstatus doctype".split()
Expand Down Expand Up @@ -117,6 +131,8 @@ def update_course(self, course: Course):
for chapter in course.chapters:
self.update_chapter(chapter)

self.update_course_preview_image(course)

def update_chapter(self, chapter: Chapter):
doc = chapter.get_doc()
self.ensure_doc("Course Chapter", doc['name'], course=doc['course'])
Expand Down Expand Up @@ -149,3 +165,13 @@ def ensure_doc(self, doctype, name, **fields):
if not self.exists(doctype, name):
doc = dict(DEFAULTS[doctype], **fields)
self.save_document(doctype, name, doc)

def update_course_preview_image(self, course: Course):
if course.preview_image:
path = str(course.root / course.preview_image)
print("upload preview_image", path)
file_url = self.add_attachment(path,
doctype="LMS Course",
docname=course.name,
fieldname="image")
self.save_document("LMS Course", course.name, {"image": file_url})
2 changes: 2 additions & 0 deletions build/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def read_course(self, name: str) -> Course:
description=data['description'],
instructor=data['instructor'],
draft=data.get('draft', False),
preview_image=data.get("preview_image", None),
is_published=data['is_published'],
upcoming=data['upcoming'],
tags=data['tags'],
Expand Down Expand Up @@ -81,6 +82,7 @@ class Course:
video_link: str
chapters: List[Chapter]
draft: bool = False
preview_image: str = None

def get_doc(self) -> dict:
doc = CourseDoc(
Expand Down
1 change: 1 addition & 0 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
draft: false,
is_published: true
upcoming: true
preview_image: null
tags:
- python
- beginner
Expand Down

0 comments on commit a20d387

Please sign in to comment.