From: CMDR furrycat Date: Mon, 25 Sep 2017 10:28:03 +0000 (+0100) Subject: Allow date range in faction history. X-Git-Url: http://git.furryclan.net/?a=commitdiff_plain;h=0f879a048568e1551dfd63cdf3bfbe238503adc1;p=furrycat%2Fcatbot.git Allow date range in faction history. --- diff --git a/plugin/faction/faction.py b/plugin/faction/faction.py index b99d4b1..7de21d3 100644 --- a/plugin/faction/faction.py +++ b/plugin/faction/faction.py @@ -1323,7 +1323,7 @@ class Faction(DBConnection): else: return None - async def graph_faction_history(self, destination, *, names = None, systems = None, width = 1024, height = 768): + async def graph_faction_history(self, destination, *, names = None, systems = None, start_date = None, end_date = None, width = 1024, height = 768): faction_ids = [] system_ids = [] @@ -1351,6 +1351,12 @@ class Faction(DBConnection): if len(faction_ids): sql += ' and i.faction_id in ({})'.format(', '.join(['?'] * len(faction_ids))) params += faction_ids + if start_date is not None: + sql += ' and timestamp>=?' + params.append(start_date) + if end_date is not None: + sql += ' and timestamp<=?' + params.append(end_date) sql += ' group by timestamp, system_name, faction_name order by timestamp desc, system_name, faction_name' if len(faction_ids) == 1: @@ -1427,13 +1433,34 @@ class Faction(DBConnection): return True async def show_history_report(self, message, args): - params = { 'names': [], 'systems': [] } + params = { 'names': [], 'systems': [], 'start_date': None, 'end_date': None } key = 'names' - for arg in args: + i = 0 + while i < len(args): + arg = args[i].lower() + if i > len(args) - 1: + break + try: + param = args[i + 1] + except IndexError: + param = None if arg == 'in': key = 'systems' + i += 1 + continue + elif arg in ['from', 'to']: + date = bot.parse_iso8601(param) + if date is None: + await bot.say(message.channel, '{}?'.format(arg)) + return False + if arg == 'from': + params['start_date'] = date + else: + params['end_date'] = date + i += 2 continue - params[key].append(arg) + params[key].append(args[i]) + i += 1 num_factions = len(params['names']) num_systems = len(params['systems'])