Skip to content

Commit

Permalink
Merge pull request #65 from shariquerik/fixes-1
Browse files Browse the repository at this point in the history
feat: Default View and fixes
  • Loading branch information
shariquerik authored Feb 3, 2024
2 parents 9a73b0e + 79c12bd commit 270c2bb
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 196 deletions.
4 changes: 2 additions & 2 deletions crm/api/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ def get_list_data(
"page_length": page_length,
"page_length_count": page_length_count,
"is_default": is_default,
"views": get_views(doctype, is_view=True),
"total_count": frappe.client.get_count(doctype, filters=filters),
"views": get_views(doctype),
"total_count": len(frappe.get_all(doctype, filters=filters)),
"row_count": len(data),
}

Expand Down
4 changes: 1 addition & 3 deletions crm/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


@frappe.whitelist()
def get_views(doctype, is_view=False):
def get_views(doctype):
if frappe.session.user == "Guest":
frappe.throw("Authentication failed", exc=frappe.AuthenticationError)

Expand All @@ -13,8 +13,6 @@ def get_views(doctype, is_view=False):
.select("*")
.where(Criterion.any([View.user == '', View.user == frappe.session.user]))
)
if is_view:
query = query.where(View.is_view == True)
if doctype:
query = query.where(View.dt == doctype)
views = query.run(as_dict=True)
Expand Down
23 changes: 4 additions & 19 deletions crm/fcrm/doctype/crm_view_settings/crm_view_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
"field_order": [
"label",
"user",
"user_list",
"is_default",
"column_break_zacm",
"dt",
"route_name",
"pinned",
"public",
"default",
"is_view",
"columns_tab",
"load_default_columns",
"columns",
Expand All @@ -31,7 +29,6 @@
"label": "Columns"
},
{
"depends_on": "eval: doc.public == false",
"fieldname": "user",
"fieldtype": "Link",
"label": "User",
Expand Down Expand Up @@ -111,26 +108,14 @@
},
{
"default": "0",
"fieldname": "default",
"fieldname": "is_default",
"fieldtype": "Check",
"label": "Default"
},
{
"default": "0",
"fieldname": "is_view",
"fieldtype": "Check",
"label": "Is View"
},
{
"depends_on": "eval: doc.public == true",
"fieldname": "user_list",
"fieldtype": "Text",
"label": "User List"
"label": "Is Default"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-01-22 21:44:41.733977",
"modified": "2024-02-03 18:38:09.412745",
"modified_by": "Administrator",
"module": "FCRM",
"name": "CRM View Settings",
Expand Down
113 changes: 57 additions & 56 deletions crm/fcrm/doctype/crm_view_settings/crm_view_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def create(view):
view.rows = view.rows + default_rows if default_rows else view.rows
view.rows = remove_duplicates(view.rows)

if not view.columns:
view.columns = sync_default_list_columns(view.doctype)

doc = frappe.new_doc("CRM View Settings")
doc.name = view.label
doc.label = view.label
doc.is_view = True
doc.dt = view.doctype
doc.user = frappe.session.user
doc.route_name = view.route_name or ""
Expand All @@ -50,7 +52,6 @@ def update(view):

doc = frappe.get_doc("CRM View Settings", view.name)
doc.label = view.label
doc.is_view = True
doc.route_name = view.route_name or ""
doc.load_default_columns = view.load_default_columns or False
doc.filters = json.dumps(filters)
Expand Down Expand Up @@ -81,60 +82,6 @@ def pin(name, value):
doc.pinned = value
doc.save()

@frappe.whitelist()
def make_default(name, doctype, route_name):
existing_default_view = frappe.db.exists("CRM View Settings", {
"default": True,
"user": frappe.session.user
})
if existing_default_view:
frappe.db.set_value("CRM View Settings", existing_default_view, "default", False)
else:
public_default = frappe.db.exists("CRM View Settings", {
"default": True,
"public": True
})
if public_default:
public_default_view = frappe.get_doc("CRM View Settings", public_default)
user_list = json.loads(public_default_view.user_list)
if frappe.session.user in user_list:
updated_user_list = [user for user in user_list if user != frappe.session.user]
if not updated_user_list:
public_default_view.default = False
public_default_view.user_list = None
else:
public_default_view.user_list = json.dumps(updated_user_list)
public_default_view.save()

if not name and doctype:
exists = frappe.db.exists("CRM View Settings", {
"dt": doctype,
"is_view": False,
"user": frappe.session.user
})
if not exists:
doc = frappe.new_doc("CRM View Settings")
doc.label = "List View"
doc.default = True
doc.dt = doctype
doc.route_name = route_name
doc.user = frappe.session.user
doc.is_view = False
doc.insert()
else:
frappe.db.set_value("CRM View Settings", exists, "default", True)
elif frappe.db.get_value("CRM View Settings", name, "public"):
doc = frappe.get_doc("CRM View Settings", name)
doc.default = True
doc.user_list = (
json.dumps([frappe.session.user])
if not doc.user_list
else json.dumps(json.loads(doc.user_list) + [frappe.session.user])
)
doc.save()
else:
frappe.db.set_value("CRM View Settings", name, "default", True)

def remove_duplicates(l):
return list(dict.fromkeys(l))

Expand All @@ -146,3 +93,57 @@ def sync_default_list_rows(doctype):
rows = list.default_list_data().get("rows")

return rows

def sync_default_list_columns(doctype):
list = get_controller(doctype)
columns = []

if hasattr(list, "default_list_data"):
columns = list.default_list_data().get("columns")

return columns


@frappe.whitelist()
def create_or_update_default_view(view):
view = frappe._dict(view)

filters = parse_json(view.filters) or {}
columns = parse_json(view.columns or '[]')
rows = parse_json(view.rows or '[]')

default_rows = sync_default_list_rows(view.doctype)
rows = rows + default_rows if default_rows else rows
rows = remove_duplicates(rows)

if not columns:
columns = sync_default_list_columns(view.doctype)

doc = frappe.db.exists(
"CRM View Settings",
{"dt": view.doctype, "is_default": True, "user": frappe.session.user},
)
if doc:
doc = frappe.get_doc("CRM View Settings", doc)
doc.label = view.label
doc.route_name = view.route_name or ""
doc.load_default_columns = view.load_default_columns or False
doc.filters = json.dumps(filters)
doc.order_by = view.order_by
doc.columns = json.dumps(columns)
doc.rows = json.dumps(rows)
doc.save()
else:
doc = frappe.new_doc("CRM View Settings")
doc.name = view.label or 'List View'
doc.label = view.label or 'List View'
doc.dt = view.doctype
doc.user = frappe.session.user
doc.route_name = view.route_name or ""
doc.load_default_columns = view.load_default_columns or False
doc.filters = json.dumps(filters)
doc.order_by = view.order_by
doc.columns = json.dumps(columns)
doc.rows = json.dumps(rows)
doc.is_default = True
doc.insert()
71 changes: 32 additions & 39 deletions frontend/src/components/ViewControls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
</div>
<div class="flex items-center gap-2">
<div
v-if="viewUpdated && (!view.public || isManager())"
v-if="viewUpdated && route.query.view && (!view.public || isManager())"
class="flex items-center gap-2 border-r pr-2"
>
<Button label="Cancel" @click="cancelChanges" />
<Button
:label="view?.name ? 'Save Changes' : 'Create View'"
@click="saveView"
/>
<Button label="Save Changes" @click="saveView" />
</div>
<div class="flex items-center gap-2">
<Filter
Expand Down Expand Up @@ -83,7 +80,6 @@ import ViewModal from '@/components/Modals/ViewModal.vue'
import SortBy from '@/components/SortBy.vue'
import Filter from '@/components/Filter.vue'
import ColumnSettings from '@/components/ColumnSettings.vue'
import { createToast } from '@/utils'
import { globalStore } from '@/stores/global'
import { viewsStore } from '@/stores/views'
import { usersStore } from '@/stores/users'
Expand All @@ -104,7 +100,7 @@ const props = defineProps({
})
const { $dialog } = globalStore()
const { reload: reloadView, getView, getDefaultView } = viewsStore()
const { reload: reloadView, getView } = viewsStore()
const { isManager } = usersStore()
const list = defineModel()
Expand Down Expand Up @@ -155,7 +151,7 @@ watch(
)
function getParams() {
let _view = getView(route.query.view)
let _view = getView(route.query.view, props.doctype)
const filters = (_view?.filters && JSON.parse(_view.filters)) || {}
const order_by = _view?.order_by || 'modified desc'
const columns = _view?.columns || ''
Expand Down Expand Up @@ -267,7 +263,9 @@ const viewsDropdownOptions = computed(() => {
}
})
let publicViews = list.value.data.views.filter((v) => v.public)
let savedViews = list.value.data.views.filter((v) => !v.pinned && !v.public)
let savedViews = list.value.data.views.filter(
(v) => !v.pinned && !v.public && !v.is_default
)
let pinnedViews = list.value.data.views.filter((v) => v.pinned)
publicViews.length &&
Expand Down Expand Up @@ -300,6 +298,10 @@ function updateFilter(filters) {
list.value.params.filters = filters
view.value.filters = filters
list.value.reload()
if (!route.query.view) {
create_or_update_default_view()
}
}
function updateSort(order_by) {
Expand All @@ -311,6 +313,10 @@ function updateSort(order_by) {
list.value.params.order_by = order_by
view.value.order_by = order_by
list.value.reload()
if (!route.query.view) {
create_or_update_default_view()
}
}
function updateColumns(obj) {
Expand All @@ -330,6 +336,21 @@ function updateColumns(obj) {
list.value.reload()
}
viewUpdated.value = true
if (!route.query.view) {
create_or_update_default_view()
}
}
function create_or_update_default_view() {
if (route.query.view) return
view.value.doctype = props.doctype
call(
'crm.fcrm.doctype.crm_view_settings.crm_view_settings.create_or_update_default_view',
{
view: view.value,
}
).then(() => reloadView())
}
function updatePageLength(value, loadMore = false) {
Expand Down Expand Up @@ -362,20 +383,6 @@ const viewActions = computed(() => {
},
]
let defaultView = getDefaultView()
if (
!defaultView ||
(route.query.view && route.query.view != defaultView.name) ||
(!route.query.view &&
(defaultView.route_name != route.name || defaultView.is_view))
) {
actions[0].items.push({
label: 'Make Default',
icon: () => h(FeatherIcon, { name: 'star', class: 'h-4 w-4' }),
onClick: () => makeDefault(),
})
}
if (route.query.view && (!view.value.public || isManager())) {
actions[0].items.push(
{
Expand Down Expand Up @@ -431,24 +438,10 @@ const viewActions = computed(() => {
return actions
})
function makeDefault() {
call('crm.fcrm.doctype.crm_view_settings.crm_view_settings.make_default', {
name: route.query.view || '',
doctype: props.doctype,
route_name: route.name,
}).then(() => {
createToast({
title: 'Default View Set',
icon: 'check',
iconClasses: 'text-green-600',
})
reloadView()
})
}
function duplicateView() {
let label = getView(route.query.view)?.label || 'List View'
view.value.name = ''
view.value.label = getView(route.query.view).label + ' New'
view.value.label = label + ' (New)'
showViewModal.value = true
}
Expand Down
9 changes: 1 addition & 8 deletions frontend/src/pages/CallLog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,12 @@ import {
} from 'frappe-ui'
import { usersStore } from '@/stores/users'
import { contactsStore } from '@/stores/contacts'
import { viewsStore } from '@/stores/views'
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const { getUser } = usersStore()
const { contacts, getContact, getLeadContact } = contactsStore()
const { getDefaultView } = viewsStore()
const props = defineProps({
callLogId: {
Expand Down Expand Up @@ -241,12 +239,7 @@ function createLead() {
}
const breadcrumbs = computed(() => {
let defaultView = getDefaultView()
let route = { name: 'Call Logs' }
if (defaultView?.route_name == 'Call Logs' && defaultView?.is_view) {
route = { name: 'Call Logs', query: { view: defaultView.name } }
}
let items = [{ label: 'Call Logs', route: route }]
let items = [{ label: 'Call Logs', route: { name: 'Call Logs' } }]
items.push({
label: callLog.data?.caller.label,
route: { name: 'Call Log', params: { callLogId: props.callLogId } },
Expand Down
Loading

0 comments on commit 270c2bb

Please sign in to comment.