def post(self, request, command):
        with self._handle_exception(request):
            if (command == 'activate-stable'):
                password = request.data.get('activation_code', None)
                if (password is None):
                    e_msg = ('Activation code is required for '
                             'Stable subscription')
                    handle_exception(Exception(e_msg), request)
                # remove any leading or trailing white spaces. happens enough
                # times due to copy-paste.
                password = password.strip()
                stableo = self._toggle_repos(password=password)
                return Response(UpdateSubscriptionSerializer(stableo).data)

            if (command == 'activate-testing'):
                testingo = self._toggle_repos(on='testing', off='stable')
                return Response(UpdateSubscriptionSerializer(testingo).data)

            if (command == 'check-status'):
                name = request.data.get('name')
                stableo = UpdateSubscription.objects.get(name=name)
                if (stableo.password is not None):
                    stableo.status, text = repo_status(stableo)
                    stableo.save()
                return Response(UpdateSubscriptionSerializer(stableo).data)
    def _toggle_repos(self, on='stable', off='testing', password=None):
        # toggle between testing and stable repos
        ncd = settings.UPDATE_CHANNELS[on]
        fcd = settings.UPDATE_CHANNELS[off]
        try:
            offo = UpdateSubscription.objects.get(name=fcd['name'])
            offo.status = 'inactive'
            offo.save()
            switch_repo(offo, on=False)
        except UpdateSubscription.DoesNotExist:
            pass

        try:
            ono = UpdateSubscription.objects.get(name=ncd['name'])
        except UpdateSubscription.DoesNotExist:
            appliance = Appliance.objects.get(current_appliance=True)
            ono = UpdateSubscription(name=ncd['name'],
                                     description=ncd['description'],
                                     url=ncd['url'], appliance=appliance,
                                     status='active')
        ono.password = password
        status, text = repo_status(ono)
        ono.status = status
        ono.save()
        if (status == 'inactive'):
            e_msg = (
                'Activation code ({}) could not be authorized for your '
                'appliance ({}). Verify the code and try again. If the '
                'problem persists, email [email protected] with this '
                'message.').format(ono.password, appliance.uuid)
            raise RockStorAPIException(status_code=400, detail=e_msg)
        if (status != 'active'):
            e_msg = ('Failed to activate subscription. Status code: '
                     '{} details: {}').format(status, text)
            raise Exception(e_msg)
        switch_repo(ono)
        return ono
    def _toggle_repos(self, on='stable', off='testing', password=None):
        #toggle between testing and stabel repos
        ncd = settings.UPDATE_CHANNELS[on]
        fcd = settings.UPDATE_CHANNELS[off]
        try:
            offo = UpdateSubscription.objects.get(name=fcd['name'])
            offo.status = 'inactive'
            offo.save()
            switch_repo(offo, on=False)
        except UpdateSubscription.DoesNotExist:
            pass

        try:
            ono = UpdateSubscription.objects.get(name=ncd['name'])
        except UpdateSubscription.DoesNotExist:
            appliance = Appliance.objects.get(current_appliance=True)
            ono = UpdateSubscription(name=ncd['name'],
                                     description=ncd['description'],
                                     url=ncd['url'], appliance=appliance,
                                     status='active')
        ono.password = password
        status, text = repo_status(ono)
        ono.status = status
        ono.save()
        if (status == 'inactive'):
            e_msg = ('Activation code(%s) could not be authorized. '
                     'Verify the code and try again. If '
                     'the problem persists, contact '
                     '[email protected]' % ono.password)
            raise Exception(e_msg)
        if (status != 'active'):
            e_msg = ('Failed to activate subscription. status code: '
                     '%s details: %s' % (status, text))
            raise Exception(e_msg)
        switch_repo(ono)
        return ono