Leveraging Portals in ERPNext

Parth Joshi
Sep 11, 2018 · 8 min read

What are the Use Cases for Portal?

Recently I landed upon couple customers which demanded a portal access. Which brought me to the idea of how powerful the portal mechanism is. And how still a lot more can be done by the ERPNext Developers (probably if someone can join me to contribute) for enhancing the Portal Pages facilities in Frappe Core.

  1. List of Word Order a Supplier (Services Provider) has to Complete
  2. Material Consumption by Third Party Supplier (But this cannot be done by portal, a Desk Access is required. It would be great if this is solved by the Portal in future)
  3. Student Profile
  4. Student Fees List
  5. Student Elective Selection in Program Enrolment (Again Desk is required)
  6. Student Assessment Result History

How to Get Started with Portal Pages ?

You need to create your a frappe app for the same. This goes naturally as you don’t want to customise ERPNext / Frappe code just for the pages which you are adding to the site.

$ bench new-app MyCustomApp
$ cat sites/current-site.txt
$ bench --site mysite.local install-app MyCustomApp

Understanding Portal Pages Basics

Lets say you want to create a page student profile. And you want to get this page displayed each time when following link is opened :

http://your-server-ip:your-port/studentprofile
<h1> Student Profile</h1>
<p>Some Data about student profile to be loaded here</p>
Screenshot of page When we open http://<your-sever:port>/studentprofile.html in browser
import frappedef get_context(context):
## load some data and put it in context
context.message = "Hello Portal!"
<h1> Student Profile</h1>
<p>{{message}}</p>
Screenshot of page When we open http://<your-sever:port>/studentprofile.html in the browser. Here the message Hello Portal! is coming from context variable set in Python File.

Loading Data from the Document using Frappe

Let’s see an example where we provide Student Login (with only website access and not System access):

  • Our Web Pages will detect the username (email) of the website user (Student) and will fetch student information from the Student Document.
logged_in_user = frappe.session.user
student = frappe.get_all('Student', filters={
'student_email_id': logged_in_user
}, fields=['name', 'student_mobile_number'])
context.name = student[0].namecontext.student_mobile_number = student[0].student_mobile_number
<p>Hello {{name}}</p>
Student profile page, where required information about the loggedin user (student with website user type) is loaded using Frappe API Objects.

Diving Further: Loading from Documents

Let’s write a code to load all the Fees for the corresponding student.

List of Fees of Student loaded by out Controller for the current logged in User with Website User Type and Student Role.

Using the Generator

Generators are useful to generate a WebView of a particular document. For creating a generator you need to:

...website_generators = ["Item Group", "Item", "BOM", "Sales 
Partner","Job Opening", "Student Admission", "Student"]
...
...class Student(WebsiteGenerator):...
...
website = frappe._dict(template =
"templates/generators/studentinfo.html",
page_title_field = "title")
...
def get_context(self, context):
context.no_cache = 1
context.show_sidebar = True
context.title = self.title
context.parents = [{'name': 'Student', 'title': _('Student'), 'route': 'student' }]
context.admission_no = self.admission_no
class Student(WebsiteGenerator):
...
def validate(self) :
if not self.route: #pylint: disable=E0203
self.route = "studentinfo/" + "-"
.join(self.title.split(" "))
{% block header %}
<h1>{{ title }}</h1>
{% endblock %}
{% block page_content %}
<p>{{admission_no}}</p>
{% endblock %}
http://localhost:8009/studentinfo/suresh
Student Info Page Generated By Document Generator

Concluding thoughts

There are certain use cases when you want to give information to “Guest” or website user:

  1. Which has a role
  2. But does not have a access to the desk.

Kanssfer Consulting

Technology updates, tips and tricks for professionals, teams and organizations.

Parth Joshi

Written by

Consultant, Trainer, Entrepreneur : ERP | IoT | Blockchain

Kanssfer Consulting

Technology updates, tips and tricks for professionals, teams and organizations.