iro/controller/viewinterface.py
changeset 302 3f4bdea2abbf
parent 295 dc3cc61c7f6f
child 309 7fa1d4713a4f
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
       
     1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
       
     2 # 
       
     3 # This file is part of Iro.
       
     4 # 
       
     5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
       
     6 # this software and associated documentation files (the "Software"), to deal in
       
     7 # the Software without restriction, including without limitation the rights to use,
       
     8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
       
     9 # #Software, and to permit persons to whom the Software is furnished to do so,
       
    10 # subject to the following conditions:
       
    11 # 
       
    12 # The above copyright notice and this permission notice shall be included in
       
    13 # all copies or substantial portions of the Software.
       
    14 # 
       
    15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
       
    16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
       
    17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    21 
       
    22 # -*- coding: utf-8 -*-
       
    23 from ..model.decorators import vUser, vRoute, dbdefer, vTyp
       
    24 from ..model.message import SMS, Fax, Mail
       
    25 
       
    26 from ..validate import validate, vBool, vTel, vEmail, vInteger
       
    27 
       
    28 from .task import createJob
       
    29 
       
    30 class Interface(object):
       
    31     '''Interface for views.'''
       
    32     
       
    33     @validate(kwd="detailed", func=vBool, need=False)
       
    34     @validate(kwd="id", func=vInteger, minv=0, need=False, none_allowed=True)
       
    35     @vUser
       
    36     @dbdefer
       
    37     def status(self, session, user, id=None, detailed=False):
       
    38         '''Returns the status of one or more jobs.
       
    39 
       
    40         :param string user: apikey of a user
       
    41         :param integer id: one job id
       
    42         :param `boolean` detailed: return more details about the status
       
    43 
       
    44         :return dict: 
       
    45            - `key` -- is the job id
       
    46            - [`key`][**'status'**] -- status of the job
       
    47 
       
    48         .. warning:: detailed is not used yet.
       
    49         
       
    50         >>> status(APIKEY)
       
    51         {"1":  {"status":"sended"},
       
    52          "2":  {"status":"error"},
       
    53          "10": {"status":"sending"}}
       
    54 
       
    55         >>> status(APIKEY,10)
       
    56         {"10": {"status":"sending"}}
       
    57         '''
       
    58         user = session.merge(user)
       
    59         ret={}
       
    60         if not id:
       
    61             for job in user.jobs:
       
    62                 ret[str(job.id)]={"status":job.status}
       
    63         else:
       
    64             ret[str(id)]={"status":user.job(id).status}
       
    65         
       
    66         return ret
       
    67    
       
    68     @validate(kwd="recipients",func=vTel)
       
    69     @vUser
       
    70     @validate(kwd="route", func=vRoute, typ="sms")
       
    71     def sms(self, user, message, recipients, route="default", info=""):
       
    72         '''Send a sms.
       
    73 
       
    74         :param string user: apikey of a user
       
    75         :param string message: message
       
    76         :param list recipients:  a list of telefon numbers (use ITU-T E.123)
       
    77         :param route: route to use to send, or a list of routes as fallback
       
    78         :type route: string|list
       
    79         :param string info: a name, to combine different jobs to one billing group
       
    80 
       
    81         :return integer: the job id
       
    82         '''
       
    83         d = createJob(user, recipients, SMS(message), route, info)
       
    84         def ret(job):
       
    85             return job.dbjob
       
    86         d.addCallback(ret)
       
    87         return d
       
    88    
       
    89     @validate(kwd="recipients",func=vTel)
       
    90     @vUser
       
    91     @validate(kwd="route",func=vRoute, typ="fax")
       
    92     def fax(self, user, subject, fax, recipients, route="default", info=""):
       
    93         '''Send a fax.
       
    94 
       
    95         :param string user: apikey of a user
       
    96         :param string subject: subject
       
    97         :param string fax: content (base64 encoded)
       
    98         :param list recipients:  a list of telefon numbers (use ITU-T E.123)
       
    99         :param route: route to use to send, or a list of routes as fallback
       
   100         :type route: string|list
       
   101         :param string info: a name, to combine different jobs to one billing group
       
   102 
       
   103         :return integer: the job id
       
   104         '''
       
   105         d = createJob(user, recipients, Fax(subject, fax), route, info)
       
   106         def ret(job):
       
   107             return job.dbjob
       
   108         d.addCallback(ret)
       
   109         return d
       
   110 
       
   111     @validate(kwd="recipients",func=vEmail, allowString=False)
       
   112     @validate(kwd="frm",func=vEmail, need=False, allowList=False)
       
   113     @vUser
       
   114     @validate(kwd="route",func=vRoute, typ="mail")
       
   115     def mail(self, user, subject, body, recipients, frm=None, route="default", info=""):
       
   116         '''Send a mail.
       
   117 
       
   118         :param string user: apikey of a user
       
   119         :param string subject: subject
       
   120         :param string body: mail body
       
   121         :param list recipients:  a list of email addresses
       
   122         :param route: route to use to send, or a list of routes as fallback
       
   123         :type route: string|list
       
   124         :param string info: a name, to combine different jobs to one billing group
       
   125         :param string frm: sender mail address
       
   126         :return integer: the job id
       
   127         '''
       
   128         d = createJob(user, recipients, Mail(subject, body, frm), route, info)
       
   129         def ret(job):
       
   130             return job.dbjob
       
   131         d.addCallback(ret)
       
   132         return d
       
   133        
       
   134     @validate(kwd="typ", func=vTyp)
       
   135     @vUser
       
   136     @dbdefer
       
   137     def routes(self, session, user, typ):
       
   138         '''Returns a list of all possible offernames.
       
   139 
       
   140         :param string user: apikey of a user
       
   141         :param string typ: a typ of message -- one of in this list ["sms","fax","mail"]
       
   142 
       
   143         :return list: a list of all possible offer names for a typ
       
   144         '''
       
   145         user = session.merge(user)
       
   146         offers = user.routes(typ)
       
   147         return [u[0] for u in offers]
       
   148         
       
   149     @validate(kwd="typ", func=vTyp)
       
   150     @vUser
       
   151     @dbdefer
       
   152     def defaultRoute(self, session, user, typ):
       
   153         '''Returns all default offernames.
       
   154  
       
   155         :param string user: apikey of a user
       
   156         :param string typ: a typ of message -- one of in this list ["sms","fax","mail"]
       
   157 
       
   158         :return list: a list of all possible offer names for a typ
       
   159         '''
       
   160         user = session.merge(user)
       
   161         offers = user.routes(typ, default=True)
       
   162         return [u[0] for u in offers]
       
   163 
       
   164     @vUser
       
   165     @dbdefer
       
   166     def bill(self, session, user):
       
   167         '''Returns the bill, of not paid messages.
       
   168 
       
   169         :param string user: apikey of a user
       
   170 
       
   171         :return dict:
       
   172             - `route` -- one offer name ; **"total"** complete sum
       
   173             - [`route`][`info`][**anz**] -- Number of sended messages in one billing group
       
   174             - [`route`][`info`][**price**] -- Price for one billing group
       
   175             - [`route` | **total**][**anz**] -- Number of sended messages for one offer
       
   176             - [`route` | **total**][**price**] -- Price for one offer
       
   177 
       
   178         >>> bill(APIKEY)
       
   179         {"route1": {"info1":{"anz":1,"price":2.00},
       
   180                     "info2":{"anz":2,"price":5.00},
       
   181                     "anz":3,"price":7.00},
       
   182         "route2":  {"info1":{"anz":3, "price":1.00},
       
   183                     "info3":{"anz":4, "price":8.00},
       
   184                     "anz":7, "price":9.00},
       
   185         "total":   {"anz":10, "price":16.00}
       
   186         }
       
   187 
       
   188         '''
       
   189         ret={'total':{'price':0, 'anz':0}}
       
   190         user=session.merge(user)
       
   191         for route in user.rights:
       
   192             n=route.offer_name
       
   193             ret[n]={'price':0, 'anz':0, 'info':{}}
       
   194             for bill in route.bill:
       
   195                 ret[n]['info'][bill.info]={'price':float(bill.price),'anz':bill.anz}
       
   196                 ret[n]['price'] += bill.price
       
   197                 ret[n]['anz'] += bill.anz
       
   198             ret['total']['price'] += ret[n]['price'] 
       
   199             ret['total']['anz'] += ret[n]['anz'] 
       
   200             ret[n]['price'] = float(ret[n]['price'])
       
   201 
       
   202         ret['total']['price'] = float(ret['total']['price'])
       
   203         return ret
       
   204 
       
   205     @validate(kwd="recipients",func=vTel)
       
   206     def telnumber(self,recipients):
       
   207         '''Return True, if all telnumbers a vaild.
       
   208         
       
   209         :param list recipients:  a list of telnumbers (use ITU-T E.123)
       
   210 
       
   211         :return boolean: True -- all numbers are valid
       
   212         '''
       
   213         return True
       
   214 
       
   215     @validate(kwd="recipients",func=vEmail)
       
   216     def email(self,recipients):
       
   217         '''Return True, if all mailadresses a valid.
       
   218         
       
   219         :param list recipients:  a list of mailadresses
       
   220 
       
   221         :return boolean: True -- all addresses are valid
       
   222         '''
       
   223         return True