Browse Source

move enabling and disabling of subscriptions to program

Helmut Pozimski 6 years ago
parent
commit
511400f280
2 changed files with 47 additions and 35 deletions
  1. 43 0
      lib_stov/program.py
  2. 4 35
      stov

+ 43 - 0
lib_stov/program.py

@@ -394,3 +394,46 @@ def clean_database(database, conf):
     except stov_exceptions.DBWriteAccessFailedException as error:
         LOGGER.error(error)
         sys.exit(1)
+
+
+def change_subscription_state(database, sub_id, enable=False):
+    """
+    Enables or disables a subscription.
+
+    :param database: database object
+    :type database: lib_stov.database.Db
+    :param sub_id: ID of the subscription
+    :type sub_id: int
+    :param enable: whether to enable or disable the subscription
+    :type enable: bool
+    """
+    subscription_state = database.get_subscription(sub_id)
+    try:
+        if enable:
+            if int(subscription_state[0][6]) == 0:
+                LOGGER.error(_("The subscription ID %s is already enabled."),
+                             sub_id)
+            elif int(subscription_state[0][6]) == 1:
+                try:
+                    database.change_subscription_state(sub_id, 0)
+                except stov_exceptions.DBWriteAccessFailedException as error:
+                    LOGGER.error(error)
+                    sys.exit(1)
+                else:
+                    LOGGER.info(_("Enabled subscription ID %s."), sub_id)
+        else:
+            if int(subscription_state[0][6]) == 1:
+                LOGGER.error(_("Subscription ID %s is already disabled."),
+                             sub_id)
+            elif int(subscription_state[0][6]) == 0:
+                try:
+                    database.change_subscription_state(sub_id, 1)
+                except stov_exceptions.DBWriteAccessFailedException as error:
+                    LOGGER.error(error)
+                    sys.exit(1)
+                else:
+                    LOGGER.info(_("Disabled subscription ID %s."),
+                                sub_id)
+    except IndexError:
+        LOGGER.error(_("Could not find the subscription with ID %s, "
+                       "please check and try again."), sub_id)

+ 4 - 35
stov

@@ -85,41 +85,10 @@ elif ARGUMENTS.catchup:
     program.catchup(DB, ARGUMENTS.catchup)
 elif ARGUMENTS.cleanup:
     program.clean_database(DB, CONF)
-elif ARGUMENTS.enableid is not None:
-    SUBSCRIPTION_STATE = DB.get_subscription(ARGUMENTS.enableid)
-    try:
-        if int(SUBSCRIPTION_STATE[0][6]) == 0:
-            LOGGER.error(_("The subscription ID %s is already enabled."),
-                         ARGUMENTS.enableid)
-        elif int(SUBSCRIPTION_STATE[0][6]) == 1:
-            try:
-                DB.change_subscription_state(ARGUMENTS.enableid, 0)
-            except stov_exceptions.DBWriteAccessFailedException as error:
-                LOGGER.error(error)
-                sys.exit(1)
-            else:
-                LOGGER.info(_("Enabled subscription ID %s."), ARGUMENTS.enableid)
-    except IndexError:
-        LOGGER.error(_("Could not find the subscription with ID %s, "
-                       "please check and try again."), ARGUMENTS.enableid)
-elif ARGUMENTS.disableid is not None:
-    SUBSCRIPTION_STATE = DB.get_subscription(ARGUMENTS.disableid)
-    try:
-        if int(SUBSCRIPTION_STATE[0][6]) == 1:
-            LOGGER.error(_("Subscription ID %s is already disabled."),
-                         ARGUMENTS.disableid)
-        elif int(SUBSCRIPTION_STATE[0][6]) == 0:
-            try:
-                DB.change_subscription_state(ARGUMENTS.disableid, 1)
-            except stov_exceptions.DBWriteAccessFailedException as error:
-                LOGGER.error(error)
-                sys.exit(1)
-            else:
-                LOGGER.info(_("Disabled subscription ID %s."),
-                            ARGUMENTS.disableid)
-    except IndexError:
-        LOGGER.error(_("Could not find the subscription with ID %s, please"
-                       " check and try again."), ARGUMENTS.disableid)
+elif ARGUMENTS.enableid:
+    program.change_subscription_state(DB, ARGUMENTS.enableid, True)
+elif ARGUMENTS.disableid:
+    program.change_subscription_state(DB, ARGUMENTS.disableid, False)
 elif ARGUMENTS.license is True:
     LOGGER.info("""
     stov is free software: you can redistribute it and/or modify