iro/model/offer.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
child 312 42fd5075a5d1
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 from .dbdefer import dbdefer
       
    23 
       
    24 import schema
       
    25 from ..config import configParser
       
    26 from ..offer import getProvider, Offer
       
    27 
       
    28 @dbdefer
       
    29 def extendProvider(session, user, typ, ps):
       
    30     """extend and reduce the offer list to allowed routes for **user**.
       
    31 
       
    32     - extend the "default" to the default offerlist of the **user**.
       
    33     - extend a Provider name to all routes of that provider.
       
    34 
       
    35     :param session: a valid session ( created by decorator :func:`iro.model.dbdefer.dbdefer`)
       
    36     :param `iro.model.schema.User` user: a user object
       
    37     :param string typ: typ of the message
       
    38     :param ps: a list of strings or a string, each one offer name or provider name
       
    39     :return: a extended an reduced offer list
       
    40     """
       
    41     user = session.merge(user)
       
    42     ret = []
       
    43     if ps == "default" or ps == ["default"]:
       
    44         ps = (q[0] for q in  user.routes(typ,default=True))
       
    45     for p in ps:
       
    46         if p not in ret and user.has_right(typ, offer_name = p): 
       
    47                 ret.append(p)
       
    48         elif user.providers(typ).filter(schema.Offer.provider==p).first():
       
    49             for r in providers[p].typs[typ]:
       
    50                 n = user.has_right(typ, provider=p, route=r)
       
    51                 if n and n not in ret:
       
    52                     ret.append(n)
       
    53     return ret
       
    54 
       
    55 @dbdefer
       
    56 def loadOffers(session):
       
    57     """loading Offers from database and configuration file and store them in :attr:`~iro.model.offer.offers` and :attr:`~iro.model.offer.providers`.
       
    58 
       
    59     :param session: a valid session ( created by decorator :func:`iro.model.dbdefer.dbdefer`)
       
    60     """
       
    61     offers.clear()
       
    62     providers.clear()
       
    63     for provider in ( s for s in configParser.sections() if not s in ["main",]):
       
    64         p=getProvider(provider,configParser.get(provider,"typ"),configParser.items(provider))
       
    65         for t in p.typs:
       
    66             for r in p.typs[t]:
       
    67                 n = schema.Offer.get(session, provider, r, t).name
       
    68                 offers[n]=Offer(provider=p,route=r,typ=t,name=n)
       
    69         providers[provider]=p
       
    70 
       
    71 offers={}
       
    72 """A dict of all available offers -- key is the offer name"""
       
    73 
       
    74 providers={}
       
    75 """A dict of all available providers -- key is the provider name"""
       
    76 
       
    77 
       
    78 configParser.registerReload(loadOffers)
       
    79