From 30a820f302fca17d34fdacdc6852c815eba3a9e5 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 3 May 2023 18:57:48 +0000 Subject: [PATCH] Add option to filter snacks from historical transactions --- apiserver/apiserver/api/views.py | 32 +++++++++++++++--------- webclient/src/AdminTransactions.js | 39 ++++++++++++++++++++++-------- webclient/src/light.css | 4 +++ 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index ef63249..5fbe3f9 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -499,19 +499,27 @@ class TransactionViewSet(Base, List, Create, Retrieve, Update): def get_queryset(self): queryset = models.Transaction.objects month = self.request.query_params.get('month', '') + exclude_paypal = self.request.query_params.get('exclude_paypal', '') == 'true' + exclude_snacks = self.request.query_params.get('exclude_snacks', '') == 'true' - if self.action == 'list' and month: - try: - dt = datetime.datetime.strptime(month, '%Y-%m') - except ValueError: - raise exceptions.ValidationError(dict(month='Should be YYYY-MM.')) - queryset = queryset.filter(date__year=dt.year) - queryset = queryset.filter(date__month=dt.month) - queryset = queryset.exclude(category='Memberships:Fake Months') - return queryset.order_by('-date', '-id') - elif self.action == 'list': - queryset = queryset.exclude(report_type__isnull=True) - queryset = queryset.exclude(report_type='') + if self.action == 'list': + if month: + try: + dt = datetime.datetime.strptime(month, '%Y-%m') + except ValueError: + raise exceptions.ValidationError(dict(month='Should be YYYY-MM.')) + queryset = queryset.filter(date__year=dt.year) + queryset = queryset.filter(date__month=dt.month) + queryset = queryset.exclude(category='Memberships:Fake Months') + else: + queryset = queryset.exclude(report_type__isnull=True) + queryset = queryset.exclude(report_type='') + + if exclude_paypal: + queryset = queryset.exclude(account_type='PayPal') + + if exclude_snacks: + queryset = queryset.exclude(category='Snacks') return queryset.order_by('-date', '-id') else: return queryset.all() diff --git a/webclient/src/AdminTransactions.js b/webclient/src/AdminTransactions.js index afc979c..c19c5b8 100644 --- a/webclient/src/AdminTransactions.js +++ b/webclient/src/AdminTransactions.js @@ -43,29 +43,24 @@ export function AdminReportedTransactions(props) { let transactionsCache = false; let summaryCache = false; -let excludePayPalCache = false; export function AdminHistoricalTransactions(props) { const { token } = props; const [input, setInput] = useState({ month: moment() }); const [transactions, setTransactions] = useState(transactionsCache); const [summary, setSummary] = useState(summaryCache); - const [excludePayPal, setExcludePayPal] = useState(excludePayPalCache); + const [excludePayPal, setExcludePayPal] = useState(false); + const [excludeSnacks, setExcludeSnacks] = useState(true); const [loading, setLoading] = useState(false); const [error, setError] = useState(false); const handleDatetime = (v) => setInput({ ...input, month: v }); - const handleExcludePayPal = (e, v) => { - setExcludePayPal(v.checked); - excludePayPalCache = v.checked; - }; - - const handleSubmit = (e) => { + const makeRequest = () => { if (loading) return; setLoading(true); const month = input.month.format('YYYY-MM'); - requester('/transactions/?month=' + month, 'GET', token) + requester('/transactions/?month=' + month + '&exclude_paypal=' + excludePayPal + '&exclude_snacks=' + excludeSnacks, 'GET', token) .then(res => { setLoading(false); setError(false); @@ -92,6 +87,22 @@ export function AdminHistoricalTransactions(props) { }); }; + const handleSubmit = (e) => { + makeRequest(); + }; + + const handleExcludePayPal = (e, v) => { + setExcludePayPal(v.checked); + }; + + const handleExcludeSnacks = (e, v) => { + setExcludeSnacks(v.checked); + }; + + useEffect(() => { + makeRequest(); + }, [excludePayPal, excludeSnacks]); + return (
@@ -150,12 +161,20 @@ export function AdminHistoricalTransactions(props) { } - !excludePayPal || x.account_type !== 'PayPal')} /> + + +
:

Error loading transactions.

diff --git a/webclient/src/light.css b/webclient/src/light.css index f7b9abe..323d0bd 100644 --- a/webclient/src/light.css +++ b/webclient/src/light.css @@ -236,6 +236,10 @@ body { text-overflow: ellipsis; } +.filter-option { + margin-right: 1rem; +} + .footer { margin-top: -20rem;