iro/joblist.py
changeset 302 3f4bdea2abbf
parent 90 eb04ac3a8327
parent 301 d5ebbcccc41b
child 303 9708742ff89c
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
     1 # -*- coding: utf-8 -*-
       
     2 #Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>
       
     3 
       
     4 #This program is free software; you can redistribute it and/or modify it under the terms
       
     5 #of the GNU General Public License as published by the Free Software Foundation;
       
     6 #either version 3 of the License, or any later version.
       
     7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
       
     8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
     9 #See the GNU General Public License for more details.
       
    10 
       
    11 #You should have received a copy of the GNU General Public License
       
    12 #along with this program; if not, see <http://www.gnu.org/licenses/>.
       
    13 
       
    14 from anbieter import content
       
    15 import hashlib, os,  time
       
    16 import logging
       
    17 logger=logging.getLogger("iro.joblist");
       
    18 
       
    19 class Joblist:
       
    20     '''
       
    21     Providing an list of jobs; each new job gets a hash id
       
    22     '''
       
    23     def __init__(self,manager, queue,providerlist,dbconn=None):
       
    24         self.jobs={}
       
    25         self.manager=manager
       
    26         self.queue=queue
       
    27         self.providerlist=providerlist
       
    28         self.dbconn=dbconn
       
    29         
       
    30 
       
    31     def __getitem__(self,key):
       
    32         return self.jobs[key]
       
    33 
       
    34     def __registerJob__(self, job, user):
       
    35         id = self._createID()
       
    36         if self.dbconn:
       
    37             job.setAcounting(self.manager.Acounting(id,self.dbconn))
       
    38         job.setId(id, user)
       
    39         self.jobs[id]=job
       
    40         self.queue.put(job)
       
    41         return id
       
    42 
       
    43     def newSMS(self, message, recipients, provider="default", user=None):
       
    44         '''
       
    45         creates a new SMS
       
    46         '''
       
    47         job=self.manager.SMSJob(self.providerlist, provider,message, content.SMS(message),recipients)
       
    48         return self.__registerJob__(job,user)
       
    49 
       
    50     def newFAX(self,subject, fax,recipients,provider="default",user=None):
       
    51         '''
       
    52         creates a new Fax
       
    53         '''
       
    54         job=self.manager.FaxJob(self.providerlist, provider,subject, content.FAX(subject,'' ,fax),recipients)
       
    55         return self.__registerJob__(job,user)
       
    56 
       
    57     def newMail(self, subject, body, recipients, frm, provider="default",user=None):
       
    58         '''
       
    59         creates a new Mail
       
    60         '''
       
    61         job=self.manager.MailJob(self.providerlist, provider,subject, content.Mail(subject, body, frm),recipients)
       
    62         return self.__registerJob__(job,user) 
       
    63 
       
    64     def _createID(self):
       
    65         '''
       
    66         creats a random hash id
       
    67         '''
       
    68         while True:
       
    69             m = hashlib.sha1()
       
    70             m.update(str(time.time()))
       
    71             m.update(os.urandom(10))
       
    72             if not self.jobs.has_key(m.hexdigest):
       
    73                 if not self.dbconn:                    
       
    74                     self.jobs[m.hexdigest()]=None
       
    75                     break
       
    76                 if not self.manager.Acounting(m.hexdigest(),self.dbconn).getStatus():
       
    77                     self.jobs[m.hexdigest()]=None
       
    78                     break
       
    79         return m.hexdigest()