Show toast msg instead of silent error, when grid fetch fails

specifically, if a user clicks "Save defaults" for the grid filters,
but they aren't currently logged in, error will ensue.

this is a bit of an edge case which IIUC would require multiple tabs
etc. but still is worth avoiding an error email from it.
This commit is contained in:
Lance Edgar 2024-04-11 16:58:12 -05:00
parent aa500351ed
commit cbbd77c49c
2 changed files with 48 additions and 24 deletions

View file

@ -584,16 +584,28 @@
this.loading = true this.loading = true
this.$http.get(`${'$'}{this.ajaxDataUrl}?${'$'}{params}`).then(({ data }) => { this.$http.get(`${'$'}{this.ajaxDataUrl}?${'$'}{params}`).then(({ data }) => {
${grid.component_studly}CurrentData = data.data if (!data.error) {
this.data = ${grid.component_studly}CurrentData ${grid.component_studly}CurrentData = data.data
this.rowStatusMap = data.row_status_map this.data = ${grid.component_studly}CurrentData
this.total = data.total_items this.rowStatusMap = data.row_status_map
this.firstItem = data.first_item this.total = data.total_items
this.lastItem = data.last_item this.firstItem = data.first_item
this.loading = false this.lastItem = data.last_item
this.checkedRows = this.locateCheckedRows(data.checked_rows) this.loading = false
if (success) { this.checkedRows = this.locateCheckedRows(data.checked_rows)
success() if (success) {
success()
}
} else {
this.$buefy.toast.open({
message: data.error,
type: 'is-danger',
duration: 2000, // 4 seconds
})
this.loading = false
if (failure) {
failure()
}
} }
}) })
.catch((error) => { .catch((error) => {

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2023 Lance Edgar # Copyright © 2010-2024 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -42,8 +42,7 @@ from sqlalchemy_utils.functions import get_primary_keys, get_columns
from rattail.db import model, Session as RattailSession from rattail.db import model, Session as RattailSession
from rattail.db.continuum import model_transaction_query from rattail.db.continuum import model_transaction_query
from rattail.util import prettify, simple_error, get_class_hierarchy from rattail.util import simple_error, get_class_hierarchy
from rattail.time import localtime
from rattail.threads import Thread from rattail.threads import Thread
from rattail.csvutil import UnicodeDictWriter from rattail.csvutil import UnicodeDictWriter
from rattail.files import temp_path from rattail.files import temp_path
@ -324,6 +323,13 @@ class MasterView(View):
string, then the view will return the rendered grid only. Otherwise string, then the view will return the rendered grid only. Otherwise
returns the full page. returns the full page.
""" """
# nb. normally this "save defaults" flag is checked within make_grid()
# but it returns JSON data so we can't just do a redirect when there
# is no user; must return JSON error message instead
if (self.request.GET.get('save-current-filters-as-defaults') == 'true'
and not self.request.user):
return self.json_response({'error': "User is not currently logged in"})
self.listing = True self.listing = True
grid = self.make_grid() grid = self.make_grid()
@ -1465,6 +1471,7 @@ class MasterView(View):
""" """
View showing diff details of a particular object version. View showing diff details of a particular object version.
""" """
app = self.get_rattail_app()
instance = self.get_instance() instance = self.get_instance()
model_class = self.get_model_class() model_class = self.get_model_class()
route_prefix = self.get_route_prefix() route_prefix = self.get_route_prefix()
@ -1512,7 +1519,7 @@ class MasterView(View):
'instance_title_normal': instance_title, 'instance_title_normal': instance_title,
'instance_url': self.get_action_url('versions', instance), 'instance_url': self.get_action_url('versions', instance),
'transaction': transaction, 'transaction': transaction,
'changed': localtime(self.rattail_config, transaction.issued_at, from_utc=True), 'changed': app.localtime(transaction.issued_at, from_utc=True),
'version_diffs': version_diffs, 'version_diffs': version_diffs,
'show_prev_next': True, 'show_prev_next': True,
'prev_url': prev_url, 'prev_url': prev_url,
@ -3502,14 +3509,14 @@ class MasterView(View):
Normalize the given object into a data dict, for use when writing to Normalize the given object into a data dict, for use when writing to
the results file for download. the results file for download.
""" """
app = self.get_rattail_app()
data = {} data = {}
for field in fields: for field in fields:
value = getattr(obj, field, None) value = getattr(obj, field, None)
# make timestamps zone-aware # make timestamps zone-aware
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = localtime(self.rattail_config, value, value = app.localtime(value, from_utc=not self.has_local_times)
from_utc=not self.has_local_times)
data[field] = value data[field] = value
@ -3539,13 +3546,14 @@ class MasterView(View):
Coerce the given data dict record, to a "row" dict suitable for use Coerce the given data dict record, to a "row" dict suitable for use
when writing directly to XLSX file. when writing directly to XLSX file.
""" """
app = self.get_rattail_app()
data = dict(data) data = dict(data)
for key in data: for key in data:
value = data[key] value = data[key]
# make timestamps local, "zone-naive" # make timestamps local, "zone-naive"
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = localtime(self.rattail_config, value, tzinfo=False) value = app.localtime(value, tzinfo=False)
data[key] = value data[key] = value
@ -4001,14 +4009,14 @@ class MasterView(View):
Normalize the given row object into a data dict, for use when writing Normalize the given row object into a data dict, for use when writing
to the results file for download. to the results file for download.
""" """
app = self.get_rattail_app()
data = {} data = {}
for field in fields: for field in fields:
value = getattr(row, field, None) value = getattr(row, field, None)
# make timestamps zone-aware # make timestamps zone-aware
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = localtime(self.rattail_config, value, value = app.localtime(value, from_utc=not self.has_local_times)
from_utc=not self.has_local_times)
data[field] = value data[field] = value
@ -4038,6 +4046,7 @@ class MasterView(View):
Coerce the given data dict record, to a "row" dict suitable for use Coerce the given data dict record, to a "row" dict suitable for use
when writing directly to XLSX file. when writing directly to XLSX file.
""" """
app = self.get_rattail_app()
data = dict(data) data = dict(data)
for key in data: for key in data:
value = data[key] value = data[key]
@ -4048,7 +4057,7 @@ class MasterView(View):
# make timestamps local, "zone-naive" # make timestamps local, "zone-naive"
elif isinstance(value, datetime.datetime): elif isinstance(value, datetime.datetime):
value = localtime(self.rattail_config, value, tzinfo=False) value = app.localtime(value, tzinfo=False)
data[key] = value data[key] = value
@ -4099,6 +4108,7 @@ class MasterView(View):
""" """
Return a dict for use when writing the row's data to XLSX download. Return a dict for use when writing the row's data to XLSX download.
""" """
app = self.get_rattail_app()
xlrow = {} xlrow = {}
for field in fields: for field in fields:
value = getattr(row, field, None) value = getattr(row, field, None)
@ -4111,9 +4121,9 @@ class MasterView(View):
# but we should make sure they're in "local" time zone effectively. # but we should make sure they're in "local" time zone effectively.
# note however, this assumes a "naive" time value is in UTC zone! # note however, this assumes a "naive" time value is in UTC zone!
if value.tzinfo: if value.tzinfo:
value = localtime(self.rattail_config, value, tzinfo=False) value = app.localtime(value, tzinfo=False)
else: else:
value = localtime(self.rattail_config, value, from_utc=True, tzinfo=False) value = app.localtime(value, from_utc=True, tzinfo=False)
xlrow[field] = value xlrow[field] = value
return xlrow return xlrow
@ -4177,12 +4187,13 @@ class MasterView(View):
""" """
Return a dict for use when writing the row's data to CSV download. Return a dict for use when writing the row's data to CSV download.
""" """
app = self.get_rattail_app()
csvrow = {} csvrow = {}
for field in fields: for field in fields:
value = getattr(obj, field, None) value = getattr(obj, field, None)
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
# TODO: this assumes value is *always* naive UTC # TODO: this assumes value is *always* naive UTC
value = localtime(self.rattail_config, value, from_utc=True) value = app.localtime(value, from_utc=True)
csvrow[field] = '' if value is None else str(value) csvrow[field] = '' if value is None else str(value)
return csvrow return csvrow
@ -4190,12 +4201,13 @@ class MasterView(View):
""" """
Return a dict for use when writing the row's data to CSV download. Return a dict for use when writing the row's data to CSV download.
""" """
app = self.get_rattail_app()
csvrow = {} csvrow = {}
for field in fields: for field in fields:
value = getattr(row, field, None) value = getattr(row, field, None)
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
# TODO: this assumes value is *always* naive UTC # TODO: this assumes value is *always* naive UTC
value = localtime(self.rattail_config, value, from_utc=True) value = app.localtime(value, from_utc=True)
csvrow[field] = '' if value is None else str(value) csvrow[field] = '' if value is None else str(value)
return csvrow return csvrow