def forwards(self, orm): "Write your forwards methods here." for payment in orm.Payment.objects.all(): payment.offer_currency = payment.offer.currency payment.offer2payment_suggested_rate = Decimal( str(currency_service.get_rate(payment.offer_currency, payment.currency)) ) payment.usd2payment_rate = Decimal(str(currency_service.get_rate("USD", payment.currency))) payment.save()
def forwards(self, orm): "Write your forwards methods here." for payment in orm.Payment.objects.all(): payment.offer_currency = payment.offer.currency payment.offer2payment_suggested_rate = Decimal( str( currency_service.get_rate(payment.offer_currency, payment.currency))) payment.usd2payment_rate = Decimal( str(currency_service.get_rate('USD', payment.currency))) payment.save()
def newPayment(cls, offer, currency): payment = cls() payment.offer = offer payment.currency = currency payment.offer_currency = offer.currency payment.offer2payment_suggested_rate = Decimal(str(currency_service.get_rate(offer.currency, payment.currency))) payment.usd2payment_rate = Decimal(str(currency_service.get_rate('USD', payment.currency))) payment.creationDate = timezone.now() payment.lastChangeDate = payment.creationDate payment.status = Payment.CREATED payment.confirm_key = hashlib.md5(str(time.time()) + str(random.random())).hexdigest() return payment
def newPayment(cls, offer, currency): payment = cls() payment.offer = offer payment.currency = currency payment.offer_currency = offer.currency payment.offer2payment_suggested_rate = Decimal( str(currency_service.get_rate(offer.currency, payment.currency))) payment.usd2payment_rate = Decimal( str(currency_service.get_rate('USD', payment.currency))) payment.creationDate = timezone.now() payment.lastChangeDate = payment.creationDate payment.status = Payment.CREATED payment.confirm_key = hashlib.md5( str(time.time()) + str(random.random())).hexdigest() return payment
def _currency_options(offer): is_brazilian = offer.sponsor.getUserInfo().brazilianPaypal btc = {'currency': 'BTC', 'selectLabel': 'Bitcoin', 'rate': currency_service.get_rate(offer.currency, 'BTC')} if is_brazilian: brl = {'currency': 'BRL', 'selectLabel': 'R$, usando Paypal', 'rate': currency_service.get_rate(offer.currency, 'BRL')} return [brl, btc] else: usd = {'currency': 'USD', 'selectLabel': 'US$, using Paypal', 'rate': currency_service.get_rate(offer.currency, 'USD')} return [usd, btc]
def addAFewFrespoSettings(request): return { 'SITE_HOME': settings.SITE_HOME, 'FS_FEE': settings.FS_FEE, 'BITCOIN_ENABLED': settings.BITCOIN_ENABLED, 'ENABLE_PIWIK': settings.ENABLE_PIWIK, 'BTC2USD': currency_service.get_rate('BTC', 'USD', False) }
def _currency_options(offer): is_brazilian = offer.sponsor.getUserInfo().brazilianPaypal btc = {"currency": "BTC", "selectLabel": "Bitcoin", "rate": currency_service.get_rate(offer.currency, "BTC")} if is_brazilian: brl = { "currency": "BRL", "selectLabel": "R$, usando Paypal", "rate": currency_service.get_rate(offer.currency, "BRL"), } return [brl, btc] else: usd = { "currency": "USD", "selectLabel": "US$, using Paypal", "rate": currency_service.get_rate(offer.currency, "USD"), } return [usd, btc]
def getStats(self): stats = { 'sponsoredOpenCount': 0, 'sponsoredOpenPriceUSD': Decimal(0), 'sponsoredOpenPriceBTC': Decimal(0), 'sponsoredOpenPriceBTC_inUSD': Decimal(0), 'sponsoredRevokedCount': 0, 'sponsoredRevokedPriceUSD': Decimal(0), 'sponsoredRevokedPriceBTC': Decimal(0), 'sponsoredRevokedPriceBTC_inUSD': Decimal(0), 'sponsoredPaidCount': 0, 'sponsoredPaidPriceUSD': Decimal(0), 'sponsoredPaidPriceBTC': Decimal(0), 'sponsoredPaidPriceBTC_inUSD': Decimal(0), 'workingInProgressCount': 0, 'workingAbortedCount': 0, 'workingDoneCount': 0 } for offer in self.getOffers(): assert offer.currency in ('USD', 'BTC') if (offer.status == Offer.OPEN): stats['sponsoredOpenCount'] += 1 if offer.currency == 'USD': stats['sponsoredOpenPriceUSD'] += offer.price elif offer.currency == 'BTC': stats['sponsoredOpenPriceBTC'] += offer.price elif (offer.status == Offer.REVOKED): stats['sponsoredRevokedCount'] += 1 if offer.currency == 'USD': stats['sponsoredRevokedPriceUSD'] += offer.price elif offer.currency == 'BTC': stats['sponsoredRevokedPriceBTC'] += offer.price elif (offer.status == Offer.PAID): stats['sponsoredPaidCount'] += 1 if offer.currency == 'USD': stats['sponsoredPaidPriceUSD'] += offer.price elif offer.currency == 'BTC': stats['sponsoredPaidPriceBTC'] += offer.price btc2usd = currency_service.get_rate('BTC', 'USD', False) btc2usd_decimal = Decimal(str(btc2usd)) stats['sponsoredOpenPriceBTC_inUSD'] = stats[ 'sponsoredOpenPriceBTC'] * btc2usd_decimal stats['sponsoredRevokedPriceBTC_inUSD'] = stats[ 'sponsoredRevokedPriceBTC'] * btc2usd_decimal stats['sponsoredPaidPriceBTC_inUSD'] = stats[ 'sponsoredPaidPriceBTC'] * btc2usd_decimal for solution in self.getSolutions(): if (solution.status == Solution.IN_PROGRESS): stats['workingInProgressCount'] += 1 elif (solution.status == Solution.ABORTED): stats['workingAbortedCount'] += 1 elif (solution.status == Solution.DONE): stats['workingDoneCount'] += 1 return stats
def getStats(self): stats = {'sponsoredOpenCount' : 0, 'sponsoredOpenPriceUSD' : Decimal(0), 'sponsoredOpenPriceBTC' : Decimal(0), 'sponsoredOpenPriceBTC_inUSD' : Decimal(0), 'sponsoredRevokedCount' : 0, 'sponsoredRevokedPriceUSD' : Decimal(0), 'sponsoredRevokedPriceBTC' : Decimal(0), 'sponsoredRevokedPriceBTC_inUSD' : Decimal(0), 'sponsoredPaidCount' : 0, 'sponsoredPaidPriceUSD' : Decimal(0), 'sponsoredPaidPriceBTC' : Decimal(0), 'sponsoredPaidPriceBTC_inUSD' : Decimal(0), 'workingInProgressCount' : 0, 'workingAbortedCount' : 0, 'workingDoneCount' : 0} for offer in self.getOffers(): assert offer.currency in ('USD','BTC') if(offer.status == Offer.OPEN): stats['sponsoredOpenCount'] += 1 if offer.currency == 'USD': stats['sponsoredOpenPriceUSD'] += offer.price elif offer.currency == 'BTC': stats['sponsoredOpenPriceBTC'] += offer.price elif(offer.status == Offer.REVOKED): stats['sponsoredRevokedCount'] += 1 if offer.currency == 'USD': stats['sponsoredRevokedPriceUSD'] += offer.price elif offer.currency == 'BTC': stats['sponsoredRevokedPriceBTC'] += offer.price elif(offer.status == Offer.PAID): stats['sponsoredPaidCount'] += 1 if offer.currency == 'USD': stats['sponsoredPaidPriceUSD'] += offer.price elif offer.currency == 'BTC': stats['sponsoredPaidPriceBTC'] += offer.price btc2usd = currency_service.get_rate('BTC', 'USD', False) btc2usd_decimal = Decimal(str(btc2usd)) stats['sponsoredOpenPriceBTC_inUSD'] = stats['sponsoredOpenPriceBTC'] * btc2usd_decimal stats['sponsoredRevokedPriceBTC_inUSD'] = stats['sponsoredRevokedPriceBTC'] * btc2usd_decimal stats['sponsoredPaidPriceBTC_inUSD'] = stats['sponsoredPaidPriceBTC'] * btc2usd_decimal for solution in self.getSolutions(): if(solution.status == Solution.IN_PROGRESS): stats['workingInProgressCount'] += 1 elif(solution.status == Solution.ABORTED): stats['workingAbortedCount'] += 1 elif(solution.status == Solution.DONE): stats['workingDoneCount'] += 1 return stats
def get_stats(): btc2usd = currency_service.get_rate('BTC', 'USD', False) btc2usd_decimal = Decimal(str(btc2usd)) open_sum_btc = _sum(SUM_OPEN_BTC) open_sum_btc_in_usd = open_sum_btc * btc2usd_decimal paid_sum_btc = _sum(SUM_PAID_BTC) paid_sum_btc_in_usd = paid_sum_btc * btc2usd_decimal revoked_sum_btc = _sum(SUM_REVOKED_BTC) revoked_sum_btc_in_usd = revoked_sum_btc * btc2usd_decimal expired_sum_btc = _sum(SUM_EXPIRED_BTC) expired_sum_btc_in_usd = expired_sum_btc * btc2usd_decimal return { 'age': _age(), 'user_count': UserInfo.objects.count(), 'sponsor_count': _count(COUNT_SPONSORS), 'programmer_count': _count(COUNT_PROGRAMMERS), 'paid_programmer_count': _count(COUNT_PAID_PROGRAMMERS), 'offer_count': _count(COUNT_OFFERS), 'issue_count': _count(COUNT_ISSUES), 'issue_project_count': _count(COUNT_PROJECTS), 'issue_count_kickstarting': _count(COUNT_ISSUES_KICKSTARTING), 'issue_count_sponsoring': _count(COUNT_ISSUES_SPONSORING), 'paid_offer_count': _count(COUNT_OFFERS_PAID), 'open_offer_count': _count(COUNT_OFFERS_OPEN), 'revoked_offer_count': _count(COUNT_OFFERS_REVOKED), 'paid_sum_usd': _sum(SUM_PAID_USD), 'paid_sum_btc': paid_sum_btc, 'paid_sum_btc_in_usd': paid_sum_btc_in_usd, 'open_sum_usd': _sum(SUM_OPEN_USD), 'open_sum_btc': open_sum_btc, 'open_sum_btc_in_usd': open_sum_btc_in_usd, 'expired_sum_usd': _sum(SUM_EXPIRED_USD), 'expired_sum_btc': expired_sum_btc, 'expired_sum_btc_in_usd': expired_sum_btc_in_usd, 'revoked_sum_usd': _sum(SUM_REVOKED_USD), 'revoked_sum_btc': revoked_sum_btc, 'revoked_sum_btc_in_usd': revoked_sum_btc_in_usd, 'sponsors': _select(SELECT_SPONSORS), 'projects': _select(SELECT_SPONSORED_PROJECTS), }
def project_top_programmers(project_id): btc2usd = currency_service.get_rate('BTC', 'USD', False) SIZE = 10 parts_usd = PaymentPart.objects.select_related('programmer', 'payment').filter( payment__offer__issue__project__id=project_id, payment__status__in=[Payment.CONFIRMED_IPN, Payment.CONFIRMED_TRN], payment__currency='USD').order_by('-price') parts_btc = PaymentPart.objects.select_related('programmer', 'payment').filter( payment__offer__issue__project__id=project_id, payment__status__in=[Payment.CONFIRMED_IPN, Payment.CONFIRMED_TRN], payment__currency='BTC').order_by('-price') programmers = {} def map_part(part): programmer_id = part.programmer.id programmer = programmers.get(programmer_id) if not programmer: programmer = part.programmer programmers[programmer_id] = programmer if not hasattr(programmer, 'received_usd'): setattr(programmer, 'received_usd', Decimal(0)) received_usd = part.price if part.payment.currency == 'BTC': received_usd *= Decimal(str(btc2usd)) programmer.received_usd += received_usd for part_usd in parts_usd[0:10]: map_part(part_usd) for part_btc in parts_btc[0:10]: map_part(part_btc) # for part_usd, part_btc in zip(parts_usd, parts_btc): # map_part(part_usd) # map_part(part_btc) # if len(programmers) > SIZE: # break def add_solution(solution): programmer_id = solution.programmer.id programmer = programmers.get(programmer_id) if not programmer: programmer = solution.programmer programmers[programmer_id] = programmer if solution.status == Solution.DONE: if not hasattr(programmer, 'done'): setattr(programmer, 'done', 0) programmer.done += 1 if len(programmers) < SIZE: solutions_done = Solution.objects.select_related('programmer').filter( issue__project__id=project_id, status=Solution.DONE ).order_by('-lastChangeDate') for solution in solutions_done: add_solution(solution) if len(programmers) > SIZE: break if len(programmers) < SIZE: solutions_open = Solution.objects.select_related('programmer').filter( issue__project__id=project_id, status=Solution.IN_PROGRESS ).order_by('-lastChangeDate') for solution in solutions_open: add_solution(solution) if len(programmers) > SIZE: break for programmer in programmers.values(): if not hasattr(programmer, 'received_usd'): setattr(programmer, 'received_usd', Decimal(0)) if not hasattr(programmer, 'done'): setattr(programmer, 'done', 0) def compare_programmers(s1, s2): dif = s1.received_usd - s2.received_usd if dif != 0: return int(math.copysign(1, dif)) dif = s1.done - s2.done if dif != 0: return int(math.copysign(1, dif)) return 0 programmers = sorted(programmers.values(), compare_programmers, reverse=True) return programmers
def project_top_sponsors(project_id): btc2usd = currency_service.get_rate('BTC', 'USD', False) SIZE = 10 query = Payment.objects.select_related('offer__sponsor').filter( offer__issue__project__id=project_id, status__in=[Payment.CONFIRMED_IPN, Payment.CONFIRMED_TRN]).extra( {'total_usd': 'total / usd2payment_rate'}).order_by('-total_usd') sponsors = {} for payment in query: sponsor_id = payment.offer.sponsor.id sponsor = sponsors.get(sponsor_id) if not sponsor: sponsor = payment.offer.sponsor sponsors[sponsor_id] = payment.offer.sponsor if not hasattr(sponsor, 'paid_usd'): setattr(sponsor, 'paid_usd', Decimal(0)) sponsor.paid_usd += payment.total_usd if len(sponsors) >= SIZE: break if len(sponsors) < SIZE: usd_offers = Offer.objects.select_related('sponsor').filter( issue__project_id=project_id, status=Offer.OPEN, currency='USD' ).order_by('-price') btc_offers = Offer.objects.select_related('sponsor').filter( issue__project_id=project_id, status=Offer.OPEN, currency='BTC' ).order_by('-price') def map_offer_sponsor(offer): sponsor_id = offer.sponsor.id sponsor = sponsors.get(sponsor_id) if not sponsor: sponsor = offer.sponsor sponsors[sponsor_id] = sponsor if not hasattr(sponsor, 'offered_usd'): setattr(sponsor, 'offered_usd', Decimal(0)) offered_usd = offer.price if offer.currency == 'BTC': offered_usd *= Decimal(str(btc2usd)) sponsor.offered_usd += offered_usd for offer_usd in usd_offers[0:10]: map_offer_sponsor(offer_usd) for offer_btc in btc_offers[0:10]: map_offer_sponsor(offer_btc) # for offer_usd, offer_btc in zip(usd_offers, btc_offers): # map_offer_sponsor(offer_usd) # map_offer_sponsor(offer_btc) # if len(sponsors) >= SIZE: # break for sponsor in sponsors.values(): if not hasattr(sponsor, 'paid_usd'): setattr(sponsor, 'paid_usd', Decimal(0)) if not hasattr(sponsor, 'offered_usd'): setattr(sponsor, 'offered_usd', Decimal(0)) def compare_sponsors(s1, s2): dif = s1.paid_usd - s2.paid_usd if dif != 0: return int(math.copysign(1, dif)) dif = s1.offered_usd - s2.offered_usd if dif != 0: return int(math.copysign(1, dif)) return 0 sponsors = sorted(sponsors.values(), compare_sponsors, reverse=True) return sponsors
def test_currency_service(self): r = currency_service.get_rate('USD', 'BRL') r = currency_service.get_rate('USD', 'BTC') r = currency_service.get_rate('BTC', 'USD') r = currency_service.get_rate('BTC', 'BRL') pass