Skip to content

Commit

Permalink
feat: split list query logic methods
Browse files Browse the repository at this point in the history
- `get_list_filters` &
- `get_list_select`
  • Loading branch information
ssiyad committed Jun 28, 2023
1 parent 21c1769 commit d2b2321
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
27 changes: 22 additions & 5 deletions helpdesk/extends/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ def get_list(
group_by=group_by,
)

query = apply_custom_filters(doctype, query, fields=fields)
query = apply_custom_filters(doctype, query)
query = apply_hook(doctype, query)
query = apply_sort(doctype, order_by, query)

if not filters:
query = apply_custom_select(doctype, query)

return query.run(as_dict=True, debug=debug)


Expand Down Expand Up @@ -104,14 +107,28 @@ def check_permissions(doctype, parent):
frappe.throw(f"Insufficient Permission for {doctype}", frappe.PermissionError)


def apply_custom_filters(doctype: str, query, fields: list = []):
def apply_custom_filters(doctype: str, query):
"""
Apply custom filters to query
"""
controller = get_controller(doctype)

if hasattr(controller, "get_list_query"):
return_value = controller.get_list_query(query, fields)
if hasattr(controller, "get_list_filters"):
return_value = controller.get_list_filters(query)
if return_value is not None:
query = return_value

return query


def apply_custom_select(doctype: str, query):
"""
Apply custom select logic to query
"""
controller = get_controller(doctype)

if hasattr(controller, "get_list_select"):
return_value = controller.get_list_select(query)
if return_value is not None:
query = return_value

Expand All @@ -126,7 +143,7 @@ def apply_hook(doctype: str, query):
_module_path = "helpdesk.helpdesk.hooks." + doctype.lower()
_module = importlib.import_module(_module_path)
_class = getattr(_module, doctype)
_function = getattr(_class, "get_list_query")
_function = getattr(_class, "get_list_filters")
return _function(query)
except:
return query
10 changes: 7 additions & 3 deletions helpdesk/helpdesk/doctype/hd_article/hd_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class HDArticle(Document):
@staticmethod
def get_list_query(query):
def get_list_filters(query):
QBArticle = DocType("HD Article")
QBCategory = DocType("Category")

Expand Down Expand Up @@ -84,10 +84,14 @@ def add_feedback(hd_article, helpful):
field = "helpful" if helpful else "not_helpful"

value = cint(frappe.db.get_value("HD Article", hd_article, field))
frappe.db.set_value("HD Article", hd_article, field, value + 1, update_modified=False)
frappe.db.set_value(
"HD Article", hd_article, field, value + 1, update_modified=False
)


@frappe.whitelist(allow_guest=True)
def increment_view(hd_article):
value = cint(frappe.db.get_value("HD Article", hd_article, "views"))
frappe.db.set_value("HD Article", hd_article, "views", value + 1, update_modified=False)
frappe.db.set_value(
"HD Article", hd_article, "views", value + 1, update_modified=False
)
18 changes: 8 additions & 10 deletions helpdesk/helpdesk/doctype/hd_ticket/hd_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import json
from datetime import timedelta
from typing import List
from functools import lru_cache
from typing import List

import frappe
from frappe import _
from frappe.core.utils import get_parent_doc
from frappe.database.database import Criterion, Query
from frappe.desk.form.assign_to import add as assign
from frappe.desk.form.assign_to import clear as clear_all_assignments
from frappe.email.inbox import link_communication_to_document
Expand All @@ -18,6 +17,8 @@
from frappe.query_builder.functions import Count
from frappe.utils import date_diff, get_datetime, now_datetime, time_diff_in_seconds
from frappe.utils.user import is_website_user
from pypika.queries import Query
from pypika.terms import Criterion

from helpdesk.helpdesk.doctype.hd_ticket_activity.hd_ticket_activity import (
log_ticket_activity,
Expand All @@ -26,21 +27,16 @@
default_outgoing_email_account,
default_ticket_outgoing_email_account,
)
from helpdesk.utils import publish_event, capture_event
from helpdesk.utils import capture_event, publish_event


class HDTicket(Document):
@staticmethod
def get_list_query(query: Query, fields):
def get_list_select(query: Query):
QBTicket = frappe.qb.DocType("HD Ticket")
QBComment = frappe.qb.DocType("HD Ticket Comment")
QBCommunication = frappe.qb.DocType("Communication")

if not fields:
query = query.select(QBTicket.star)

query = HDTicket.filter_by_team(query)
query = query.groupby(QBTicket.name)
query = (
query.left_join(QBComment)
.on(QBComment.reference_ticket == QBTicket.name)
Expand All @@ -51,12 +47,14 @@ def get_list_query(query: Query, fields):
& (QBCommunication.reference_name == QBTicket.name)
)
.select(Count(QBCommunication.name).as_("count_communication"))
.select(QBTicket.star)
.groupby(QBTicket.name)
)

return query

@staticmethod
def filter_by_team(query: Query):
def get_list_filters(query: Query):
user = frappe.session.user

if HDTicket.can_ignore_restrictions(user):
Expand Down
40 changes: 20 additions & 20 deletions helpdesk/helpdesk/hooks/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@


class Contact:
@staticmethod
def get_list_query(query):
QBContact = frappe.qb.DocType("Contact")
QBEmail = frappe.qb.DocType("Contact Email")
QBPhone = frappe.qb.DocType("Contact Phone")
QBLink = frappe.qb.DocType("Dynamic Link")
@staticmethod
def get_list_filters(query):
QBContact = frappe.qb.DocType("Contact")
QBEmail = frappe.qb.DocType("Contact Email")
QBPhone = frappe.qb.DocType("Contact Phone")
QBLink = frappe.qb.DocType("Dynamic Link")

query = (
query.select(QBContact.first_name)
.select(QBContact.last_name)
.left_join(QBEmail)
.on(QBEmail.parent == QBContact.name)
.select(QBEmail.email_id)
.left_join(QBPhone)
.on(QBPhone.parent == QBContact.name)
.select(QBPhone.phone)
.left_join(QBLink)
.on(QBLink.parent == QBContact.name)
.select(QBLink.link_name)
)
query = (
query.select(QBContact.first_name)
.select(QBContact.last_name)
.left_join(QBEmail)
.on(QBEmail.parent == QBContact.name)
.select(QBEmail.email_id)
.left_join(QBPhone)
.on(QBPhone.parent == QBContact.name)
.select(QBPhone.phone)
.left_join(QBLink)
.on(QBLink.parent == QBContact.name)
.select(QBLink.link_name)
)

return query
return query

0 comments on commit d2b2321

Please sign in to comment.