From 820a1e01a6bf1c0d5ec4687aaeb2a32737c4826a Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 4 Jul 2016 00:23:23 +0530 Subject: [PATCH] [fix] added set-limits, changed clear-limit to clear-limits --- frappe/commands/site.py | 60 +++++++++++++++++++++++++++------------ frappe/limits.py | 6 ++-- frappe/utils/scheduler.py | 5 +++- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/frappe/commands/site.py b/frappe/commands/site.py index 9065fa11d3..4bbe622550 100755 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -332,47 +332,70 @@ def set_admin_password(context, admin_password): finally: frappe.destroy() - @click.command('set-limit') @click.option('--site', help='site name') -@click.argument('limit', type=click.Choice(['emails', 'space', 'users', 'expiry', - 'support_email', 'support_chat', 'upgrade_link'])) +@click.argument('limit') @click.argument('value') @pass_context def set_limit(context, site, limit, value): """Sets user / space / email limit for a site""" + _set_limits(context, site, ((limit, value),)) + +@click.command('set-limits') +@click.option('--site', help='site name') +@click.option('--limit', 'limits', type=(unicode, unicode), multiple=True) +@pass_context +def set_limits(context, site, limits): + _set_limits(context, site, limits) + +def _set_limits(context, site, limits): import datetime + + if not limits: + return + if not site: site = get_site(context) with frappe.init_site(site): - if limit=='expiry': - try: - datetime.datetime.strptime(value, '%Y-%m-%d') - except ValueError: - raise ValueError("Incorrect data format, should be YYYY-MM-DD") + new_limits = {} + for limit, value in limits: + if limit not in ('emails', 'space', 'users', 'expiry', + 'support_email', 'support_chat', 'upgrade_link'): + frappe.throw('Invalid limit {0}'.format(limit)) + + if limit=='expiry': + try: + datetime.datetime.strptime(value, '%Y-%m-%d') + except ValueError: + raise ValueError("Incorrect data format, should be YYYY-MM-DD") - elif limit=='space': - value = float(value) + elif limit=='space': + value = float(value) - elif limit in ('users', 'emails'): - value = int(value) + elif limit in ('users', 'emails'): + value = int(value) - update_limits({ limit : value }) + new_limits[limit] = value -@click.command('clear-limit') + update_limits(new_limits) + +@click.command('clear-limits') @click.option('--site', help='site name') -@click.argument('limit', type=click.Choice(['emails', 'space', 'users', 'expiry', +@click.argument('limits', nargs=-1, type=click.Choice(['emails', 'space', 'users', 'expiry', 'support_email', 'support_chat', 'upgrade_link'])) @pass_context -def clear_limit(context, site, limit): +def clear_limits(context, site, limits): """Clears given limit from the site config, and removes limit from site config if its empty""" from frappe.limits import clear_limit as _clear_limit + if not limits: + return + if not site: site = get_site(context) with frappe.init_site(site): - _clear_limit(limit) + _clear_limit(limits) # Remove limits from the site_config, if it's empty limits = get_limits() @@ -396,6 +419,7 @@ commands = [ set_admin_password, uninstall, set_limit, - clear_limit, + set_limits, + clear_limits, _use, ] diff --git a/frappe/limits.py b/frappe/limits.py index f13439eeb6..83a586462a 100755 --- a/frappe/limits.py +++ b/frappe/limits.py @@ -135,8 +135,10 @@ def update_limits(limits_dict): def clear_limit(key): '''Remove a limit option from site_config''' limits = get_limits() - if key in limits: - del limits[key] + to_clear = [key] if isinstance(key, basestring) else key + for key in to_clear: + if key in limits: + del limits[key] update_site_config("limits", limits, validate=False) frappe.conf.limits = limits diff --git a/frappe/utils/scheduler.py b/frappe/utils/scheduler.py index c3a87aab90..dbe89fabde 100755 --- a/frappe/utils/scheduler.py +++ b/frappe/utils/scheduler.py @@ -174,7 +174,10 @@ def log(method, message=None): def get_enabled_scheduler_events(): enabled_events = frappe.db.get_global("enabled_scheduler_events") if enabled_events: - return json.loads(enabled_events) + if isinstance(enabled_events, basestring): + enabled_events = json.loads(enabled_events) + + return enabled_events return ["all", "hourly", "hourly_long", "daily", "daily_long", "weekly", "weekly_long", "monthly", "monthly_long"]