def GetDispersals(id): record = Dispersals.get_by_id(id) list = record.people_to_pay if list.startswith("STARTED"): raise db.Rollback else: list = "STARTED\n" + list record.people_to_pay = list record.put() return record
def get(self): if not db.WRITE_CAPABILITY.is_enabled(): self.response.set_status(503) self.response.headders['Retry-After'] = 120 logging.info("Told that the db was down for maintenance") self.response.out.write('Currently down for maintenance') else: t=int(time.time()) - 7200; logging.info('CRON AccountNewRecords: totaling funds for after %d' % t) dispersal = Dispersals() commissions_total = 0 designer_total = 0 maintainers_total = 0 total = 0 maintainers = AppSettings.get_or_insert("maintainers", value="00000000-0000-0000-0000-000000000000|0").value.split("|") people_to_pay = [] query = Purchases.gql("WHERE accounted = :1", "0") for record in query: logging.info('CRON: %s|%s' % (record.key().id(), record.seller)) total += record.amount_paid token = 'Distributor_%s' % record.seller cacheditem = memcache.get(token) if cacheditem is None: dist = Distributor.gql("WHERE avkey = :1", record.seller).get() dist_info = {"max_discount":dist.max_discount, "commission":dist.commission} memcache.set(token, yaml.safe_dump(dist_info)) else: #pull the item's details out of the yaml'd dict dist_info = yaml.safe_load(cacheditem) if dist_info['commission'] > 0: commission = record.paid_amount*dist_info['commission']/100 commissions_total += commission stoken = '%s_seller' % (record.seller) commission_total = commission + getattr(dispersal, stoken, 0) setattr(dispersal, stoken, commission_total) people_to_pay.append(stoken) name = record.item item = tools.get_item(name, True) if item is None: logging.error('Error, Paid item %s not found. Requested by %s using %s.' % (name, self.request.headers['X-SecondLife-Owner-Name'], self.request.headers['X-SecondLife-Object-Name'])) if item['designer_cut'] > 0: cut = record.paid_amount*item['designer_cut']/100 designer_total += cut dtoken = '%s_designer' % (item['designer']) cut_total = cut + getattr(dispersal, dtoken, 0) setattr(dispersal, dtoken, cut_total) people_to_pay.append(dtoken) for maintainer, amount in zip(maintainers[::2], maintainers[1::2]): cut = total*int(amount)/100 maintainers_total += cut mtoken = '%s_maintainer' % (maintainer) setattr(dispersal, mtoken, cut) people_to_pay.append(mtoken) if query.count(1) > 0: if total >= (maintainers_total + designer_total + commissions_total): setattr(dispersal, 'commissions_total', commissions_total) setattr(dispersal, 'designers_total', designer_total) setattr(dispersal, 'maintainers_total', maintainers_total) setattr(dispersal, 'dispersal_total', (maintainers_total + designer_total + commissions_total)) setattr(dispersal, 'total', total) setattr(dispersal, 'people_to_pay', "\n".join(people_to_pay)) dispersal.put() logging.info('CRON AccountNewRecords: saved') #add right url taskqueue.add(url='/paiddist/disperse?id=%s' % (dispersal.key().id()), headers={}, queue_name='Disperse', method='PUT') for record in query: record.accounted = "1" record.put() else: logging.error("CRON AccountNewRecords: total dispersal %s is greater than total paid %s" % (maintainers_total + designer_total + commissions_total, total)) redirecturl = "not needed?" alarm.SendAlarm('Dispersal', t, True, "total dispersal %s is greater than total paid %s" % (maintainers_total + designer_total + commissions_total, total), redirecturl) self.error(500) else: logging.info('CRON AccountNewRecords: No records') logging.info('CRON AccountNewRecords: Finished')
def get(self): if not db.WRITE_CAPABILITY.is_enabled(): self.response.set_status(503) self.response.headders['Retry-After'] = 120 logging.info("Told that the db was down for maintenance") self.response.out.write('Currently down for maintenance') else: t = int(time.time()) - 7200 logging.info( 'CRON AccountNewRecords: totaling funds for after %d' % t) dispersal = Dispersals() commissions_total = 0 designer_total = 0 maintainers_total = 0 total = 0 maintainers = AppSettings.get_or_insert( "maintainers", value="00000000-0000-0000-0000-000000000000|0").value.split( "|") people_to_pay = [] query = Purchases.gql("WHERE accounted = :1", "0") for record in query: logging.info('CRON: %s|%s' % (record.key().id(), record.seller)) total += record.amount_paid token = 'Distributor_%s' % record.seller cacheditem = memcache.get(token) if cacheditem is None: dist = Distributor.gql("WHERE avkey = :1", record.seller).get() dist_info = { "max_discount": dist.max_discount, "commission": dist.commission } memcache.set(token, yaml.safe_dump(dist_info)) else: #pull the item's details out of the yaml'd dict dist_info = yaml.safe_load(cacheditem) if dist_info['commission'] > 0: commission = record.paid_amount * dist_info[ 'commission'] / 100 commissions_total += commission stoken = '%s_seller' % (record.seller) commission_total = commission + getattr( dispersal, stoken, 0) setattr(dispersal, stoken, commission_total) people_to_pay.append(stoken) name = record.item item = tools.get_item(name, True) if item is None: logging.error( 'Error, Paid item %s not found. Requested by %s using %s.' % (name, self.request.headers['X-SecondLife-Owner-Name'], self.request.headers['X-SecondLife-Object-Name'])) if item['designer_cut'] > 0: cut = record.paid_amount * item['designer_cut'] / 100 designer_total += cut dtoken = '%s_designer' % (item['designer']) cut_total = cut + getattr(dispersal, dtoken, 0) setattr(dispersal, dtoken, cut_total) people_to_pay.append(dtoken) for maintainer, amount in zip(maintainers[::2], maintainers[1::2]): cut = total * int(amount) / 100 maintainers_total += cut mtoken = '%s_maintainer' % (maintainer) setattr(dispersal, mtoken, cut) people_to_pay.append(mtoken) if query.count(1) > 0: if total >= (maintainers_total + designer_total + commissions_total): setattr(dispersal, 'commissions_total', commissions_total) setattr(dispersal, 'designers_total', designer_total) setattr(dispersal, 'maintainers_total', maintainers_total) setattr(dispersal, 'dispersal_total', (maintainers_total + designer_total + commissions_total)) setattr(dispersal, 'total', total) setattr(dispersal, 'people_to_pay', "\n".join(people_to_pay)) dispersal.put() logging.info('CRON AccountNewRecords: saved') #add right url taskqueue.add(url='/paiddist/disperse?id=%s' % (dispersal.key().id()), headers={}, queue_name='Disperse', method='PUT') for record in query: record.accounted = "1" record.put() else: logging.error( "CRON AccountNewRecords: total dispersal %s is greater than total paid %s" % (maintainers_total + designer_total + commissions_total, total)) redirecturl = "not needed?" alarm.SendAlarm( 'Dispersal', t, True, "total dispersal %s is greater than total paid %s" % (maintainers_total + designer_total + commissions_total, total), redirecturl) self.error(500) else: logging.info('CRON AccountNewRecords: No records') logging.info('CRON AccountNewRecords: Finished')