iro/user.py
author Sandro Knauß <knauss@netzguerilla.net>
Sun, 07 Feb 2010 06:13:37 +0100
changeset 30 5253631a8dd3
parent 29 49ffb1f6cdbf
child 57 97ef6ca145e6
permissions -rw-r--r--
sms via sipgate working

# -*- coding: utf-8 -*-
#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>

#This program is free software; you can redistribute it and/or modify it under the terms
#of the GNU General Public License as published by the Free Software Foundation;
#either version 3 of the License, or any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#See the GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, see <http://www.gnu.org/licenses/>.
import logging
logger=logging.getLogger("iro.user")
class NotSupportedFeature (Exception):
    def __init__(self,name):
        self.name=name
    
    def __str__(self):
        return ("This is not a supported feature:", self.name)

class NoID(Exception):
    def __init__(self,i):
        self.i=i
    
    def __str__(self):
        return ("No Job with id:", self.i)


class User: 
    '''
    class for a xmlrpc user
    '''
    def __init__(self,jobqueue):
        self.jobqueue=jobqueue
        self.jobs={}
        self.features=["mail", "sms", "fax", ]

    def status(self,id=None,detailed=False):
        '''
        gets the status for a job
        if the id is None all Jobs of an user are given back
        '''
        try:
            jobs={}
            if id==None:
                jobs=self.jobs
            else:
                try:
                    jobs={id:self.jobs[id]}
                except:
                    logger.error("No Job ID %s",id)
                    #raise NoID(id)
            ret={}
            if not jobs:
                return {}

            for key in jobs:
                job=jobs[key]
                ret[key]={"name":job.getName(),"status":job.getStatus(detailed)}
            
            return ret
        except:
            logger.exception("Fehler in iro.user.status")
            return {}

    def stop(self,id):
        '''
        stops an job with id
        '''
        try:
            job=self.jobs[id]
            job.stop()
        except:
            raise NoID(id)
        job.stop()

    
    def startSMS(self,message,recipients,provider="default"):
        '''
        starts the SMS with message to recipients
        '''
        if not "sms" in self.features:
            raise NotSupportedFeature("sms")
        id = self.jobqueue.newSMS(message,recipients,provider)
        self.jobs[id]=self.jobqueue[id]
        return id
    
    
    def startFAX(self,subject, fax,provider="default"):
        '''
        starts the FAX  with the pdf file fax and the subject
        '''
        if not "fax" in self.features:
            raise NotSupportedFeature("fax")
        id = self.jobqueue.newFAX(subject, fax,recipients,provider)
        self.jobs[id]=self.jobqueue[id]
        return id

    
        
    def startMail(self,subject,  body , recipients,provider="default"):
        if not "mail" in self.features:
            raise NotSupportedFeature("mail")
        id = self.jobqueue.newMail(subject,  body ,recipients,provider)
        self.jobs[id]=self.jobqueue[id]
        return id    
       
    def getProvider(self, name):
        if not name in self.features:
            raise NotSupportedFeature(name)
       
        return self.jobqueue.providerlist.getProviderlist(name)
        
    def getDefaultProvider(self, name):
        if not name in self.features:
            raise NotSupportedFeature(name)
       
        return self.jobqueue.providerlist.getDefault(name)["name"]
        
class Admin(User):
    def __init__(self,jobqueue):
        User.__init__(self, jobqueue)
        self.jobs=jobqueue.jobs