def user_loses_policyProxy(user, policyProxy): """call explicitly (better to user setter and getter on the model) *before* deleting/removing a user from a policyProxy """ access_policies = user.access_policies if not user.active: access_policies = user.disabled_policies if isinstance(policyProxy, Group) and user in policyProxy.users: #user should have already been removed print "lost " + policyProxy.group_name for pol in policyProxy.access_policies: access_policies.remove(int(pol.id)) if isinstance(policyProxy, PolicyGroup) and user in policyProxy.users: print "lost " + policyProxy.name for pol in policyProxy.access_policies: if int(pol.id) in access_policies: access_policies.remove(int(pol.id)) if isinstance(policyProxy, Resource) \ and policyProxy.type=='tariff' \ and get_tariff(policyProxy.place.id, user.id, now(policyProxy.place)) != policyProxy: print "lost " + policyProxy.name for pol in policyProxy.access_policies: if int(pol.id) in access_policies: access_policies.remove(int(pol.id)) if user.active: user.access_policies = access_policies else: user.disabled_policies = access_policies
def user_acquires_policyProxy(user, policyProxy): """called when adding a user to a group/role, policyGroup, tariff (save_tariffHistoryEdit) """ #get the policies associated with this policyProxy access_policies = user.access_policies if not user.active: access_policies = user.disabled_policies if isinstance(policyProxy, Group) and user in policyProxy.users: print "acquired " + policyProxy.group_name for pol in policyProxy.access_policies: if int(pol.id) not in access_policies: access_policies.append(int(pol.id)) elif isinstance(policyProxy, PolicyGroup) and user in policyProxy.users: print "acquired " + policyProxy.name for pol in policyProxy.access_policies: if int(pol.id) not in access_policies: access_policies.append(int(pol.id)) elif isinstance(policyProxy, Resource) \ and policyProxy.type=='tariff' \ and get_tariff(policyProxy.place.id, user.id, now(policyProxy.place)) == policyProxy: print "acquired " + policyProxy.name for pol in policyProxy.access_policies: if int(pol.id) not in access_policies: access_policies.append(int(pol.id)) if user.active: user.access_policies = access_policies else: user.disabled_policies = access_policies
def recalculate_tariff_accessPolicies(location=None): """call explicitly when the month changes. Assumes monthly tariffs. Executed every hour on turn of the month so that updates are done per location at midnight. """ if not location: raise ValueError("There should be a location") users = filter_members(location, "", "member_search", active_only=False, start=None, end=None, override_user=True) time_here = now(location) #time_here = dt(2009, 2, 1) # for testing new_month = time_here + timedelta(days=1) old_month = time_here - timedelta(days=27) model.hub.commit() model.hub.begin() for user in users: old_tariff = get_tariff(location.id, user.id, old_month) new_tariff = get_tariff(location.id, user.id, new_month) if old_tariff != new_tariff: user_acquires_policyProxy(user, new_tariff) user_loses_policyProxy(user, old_tariff) model.hub.commit() model.hub.begin() model.hub.commit()