# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
#
# This file is part of Iro.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# #Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -*- coding: utf-8 -*-
from ..model.decorators import vUser, vRoute, dbdefer, vTyp
from ..model.message import SMS, Fax, Mail
from ..validate import validate, vBool, vTel, vEmail, vInteger
from .task import createJob
[docs]class Interface(object):
'''Interface for views.'''
@validate(kwd="detailed", func=vBool, need=False)
@validate(kwd="id", func=vInteger, minv=0, need=False, none_allowed=True)
@vUser
@dbdefer
[docs] def status(self, session, user, id=None, detailed=False):
'''Returns the status of one or more jobs.
:param string user: apikey of a user
:param integer id: one job id
:param `boolean` detailed: return more details about the status
:return dict:
- `key` -- is the job id
- [`key`][**'status'**] -- status of the job
.. warning:: detailed is not used yet.
>>> status(APIKEY)
{"1": {"status":"sended"},
"2": {"status":"error"},
"10": {"status":"sending"}}
>>> status(APIKEY,10)
{"10": {"status":"sending"}}
'''
user = session.merge(user)
ret={}
if not id:
for job in user.jobs:
ret[str(job.id)]={"status":job.status}
else:
ret[str(id)]={"status":user.job(id).status}
return ret
@validate(kwd="recipients",func=vTel)
@vUser
@validate(kwd="route", func=vRoute, typ="sms")
[docs] def sms(self, user, message, recipients, route="default", info=""):
'''Send a sms.
:param string user: apikey of a user
:param string message: message
:param list recipients: a list of telefon numbers (use ITU-T E.123)
:param route: route to use to send, or a list of routes as fallback
:type route: string|list
:param string info: a name, to combine different jobs to one billing group
:return integer: the job id
'''
d = createJob(user, recipients, SMS(message), route, info)
def ret(job):
return job.dbjob
d.addCallback(ret)
return d
@validate(kwd="recipients",func=vTel)
@vUser
@validate(kwd="route",func=vRoute, typ="fax")
[docs] def fax(self, user, subject, fax, recipients, route="default", info=""):
'''Send a fax.
:param string user: apikey of a user
:param string subject: subject
:param string fax: content (base64 encoded)
:param list recipients: a list of telefon numbers (use ITU-T E.123)
:param route: route to use to send, or a list of routes as fallback
:type route: string|list
:param string info: a name, to combine different jobs to one billing group
:return integer: the job id
'''
d = createJob(user, recipients, Fax(subject, fax), route, info)
def ret(job):
return job.dbjob
d.addCallback(ret)
return d
@validate(kwd="recipients",func=vEmail, allowString=False)
@validate(kwd="frm",func=vEmail, need=False, allowList=False)
@vUser
@validate(kwd="route",func=vRoute, typ="mail")
[docs] def mail(self, user, subject, body, recipients, frm=None, route="default", info=""):
'''Send a mail.
:param string user: apikey of a user
:param string subject: subject
:param string body: mail body
:param list recipients: a list of email addresses
:param route: route to use to send, or a list of routes as fallback
:type route: string|list
:param string info: a name, to combine different jobs to one billing group
:param string frm: sender mail address
:return integer: the job id
'''
d = createJob(user, recipients, Mail(subject, body, frm), route, info)
def ret(job):
return job.dbjob
d.addCallback(ret)
return d
@validate(kwd="typ", func=vTyp)
@vUser
@dbdefer
[docs] def routes(self, session, user, typ):
'''Returns a list of all possible offernames.
:param string user: apikey of a user
:param string typ: a typ of message -- one of in this list ["sms","fax","mail"]
:return list: a list of all possible offer names for a typ
'''
user = session.merge(user)
offers = user.routes(typ)
return [u[0] for u in offers]
@validate(kwd="typ", func=vTyp)
@vUser
@dbdefer
[docs] def defaultRoute(self, session, user, typ):
'''Returns all default offernames.
:param string user: apikey of a user
:param string typ: a typ of message -- one of in this list ["sms","fax","mail"]
:return list: a list of all possible offer names for a typ
'''
user = session.merge(user)
offers = user.routes(typ, default=True)
return [u[0] for u in offers]
@vUser
@dbdefer
[docs] def bill(self, session, user):
'''Returns the bill, of not paid messages.
:param string user: apikey of a user
:return dict:
- `route` -- one offer name ; **"total"** complete sum
- [`route`][`info`][**anz**] -- Number of sended messages in one billing group
- [`route`][`info`][**price**] -- Price for one billing group
- [`route` | **total**][**anz**] -- Number of sended messages for one offer
- [`route` | **total**][**price**] -- Price for one offer
>>> bill(APIKEY)
{"route1": {"info1":{"anz":1,"price":2.00},
"info2":{"anz":2,"price":5.00},
"anz":3,"price":7.00},
"route2": {"info1":{"anz":3, "price":1.00},
"info3":{"anz":4, "price":8.00},
"anz":7, "price":9.00},
"total": {"anz":10, "price":16.00}
}
'''
ret={'total':{'price':0, 'anz':0}}
user=session.merge(user)
for route in user.rights:
n=route.offer_name
ret[n]={'price':0, 'anz':0, 'info':{}}
for bill in route.bill:
ret[n]['info'][bill.info]={'price':float(bill.price),'anz':bill.anz}
ret[n]['price'] += bill.price
ret[n]['anz'] += bill.anz
ret['total']['price'] += ret[n]['price']
ret['total']['anz'] += ret[n]['anz']
ret[n]['price'] = float(ret[n]['price'])
ret['total']['price'] = float(ret['total']['price'])
return ret
@validate(kwd="recipients",func=vTel)
[docs] def telnumber(self,recipients):
'''Return True, if all telnumbers a vaild.
:param list recipients: a list of telnumbers (use ITU-T E.123)
:return boolean: True -- all numbers are valid
'''
return True
@validate(kwd="recipients",func=vEmail)
[docs] def email(self,recipients):
'''Return True, if all mailadresses a valid.
:param list recipients: a list of mailadresses
:return boolean: True -- all addresses are valid
'''
return True