iro/model/offer.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
child 312 42fd5075a5d1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/model/offer.py	Thu Sep 27 17:15:46 2012 +0200
@@ -0,0 +1,79 @@
+# 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.
+
+from .dbdefer import dbdefer
+
+import schema
+from ..config import configParser
+from ..offer import getProvider, Offer
+
+@dbdefer
+def extendProvider(session, user, typ, ps):
+    """extend and reduce the offer list to allowed routes for **user**.
+
+    - extend the "default" to the default offerlist of the **user**.
+    - extend a Provider name to all routes of that provider.
+
+    :param session: a valid session ( created by decorator :func:`iro.model.dbdefer.dbdefer`)
+    :param `iro.model.schema.User` user: a user object
+    :param string typ: typ of the message
+    :param ps: a list of strings or a string, each one offer name or provider name
+    :return: a extended an reduced offer list
+    """
+    user = session.merge(user)
+    ret = []
+    if ps == "default" or ps == ["default"]:
+        ps = (q[0] for q in  user.routes(typ,default=True))
+    for p in ps:
+        if p not in ret and user.has_right(typ, offer_name = p): 
+                ret.append(p)
+        elif user.providers(typ).filter(schema.Offer.provider==p).first():
+            for r in providers[p].typs[typ]:
+                n = user.has_right(typ, provider=p, route=r)
+                if n and n not in ret:
+                    ret.append(n)
+    return ret
+
+@dbdefer
+def loadOffers(session):
+    """loading Offers from database and configuration file and store them in :attr:`~iro.model.offer.offers` and :attr:`~iro.model.offer.providers`.
+
+    :param session: a valid session ( created by decorator :func:`iro.model.dbdefer.dbdefer`)
+    """
+    offers.clear()
+    providers.clear()
+    for provider in ( s for s in configParser.sections() if not s in ["main",]):
+        p=getProvider(provider,configParser.get(provider,"typ"),configParser.items(provider))
+        for t in p.typs:
+            for r in p.typs[t]:
+                n = schema.Offer.get(session, provider, r, t).name
+                offers[n]=Offer(provider=p,route=r,typ=t,name=n)
+        providers[provider]=p
+
+offers={}
+"""A dict of all available offers -- key is the offer name"""
+
+providers={}
+"""A dict of all available providers -- key is the provider name"""
+
+
+configParser.registerReload(loadOffers)
+