Skip to content

Commit

Permalink
remove redundant get_rate helper; refactor details route response
Browse files Browse the repository at this point in the history
  • Loading branch information
chibie committed Nov 20, 2018
1 parent ddb5bb2 commit 6175c9d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 50 deletions.
7 changes: 7 additions & 0 deletions apps/base/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ class TransactionAdmin(admin.ModelAdmin):
search_fields = ('user__email',)


class FAQAdmin(admin.ModelAdmin):
list_display = ('number', 'question', 'answer', 'modified', 'created')
list_filter = ('number', 'created', 'modified')
search_fields = ('question', 'answer',)


admin.site.register(User, UserAdmin)
admin.site.register(Transaction, TransactionAdmin)
admin.site.register(FAQ, FAQAdmin)

#admin.site.login = login_required(admin.site.login)
#admin.autodiscover()
Expand Down
4 changes: 2 additions & 2 deletions apps/base/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

class ICOConf(AppConf):
TOKEN_NAME = os.getenv("TOKEN_NAME")
TOKEN_SYMBOL = os.getenv("TOKEN_SYMBOL")
TOKEN_SYMBOL = os.getenv("TOKEN_SYMBOL").upper()
TOKEN_TOTAL_SUPPLY = int(os.getenv("TOKEN_TOTAL_SUPPLY"))
TOKEN_DECIMALS = float(os.getenv("TOKEN_DECIMALS"))
STAGE = os.getenv("STAGE")
STAGE = os.getenv("STAGE").upper()
START = os.getenv("START")
END = os.getenv("END")
PRICE = float(os.getenv("PRICE")) # in dollars
Expand Down
10 changes: 0 additions & 10 deletions apps/base/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ def calculate_bought(amount):
return bought


def get_rate(currency):
import requests

dollar_rate = float(requests.get(
f'https://api.coinbase.com/v2/prices/{currency.upper()}-USD/spot'
).json()['data']['amount'])

return dollar_rate


def transfer_tokens(user, amount):
amount = float(amount)

Expand Down
2 changes: 1 addition & 1 deletion apps/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FAQ(models.Model):
created = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.number
return f'{self.number}'


class Referral(models.Model):
Expand Down
9 changes: 7 additions & 2 deletions apps/base/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ def validate(self, data):


class ICODetailSerializer(serializers.Serializer):
token_name = serializers.CharField()
token_symbol = serializers.CharField()
stage = serializers.CharField()
start = serializers.DateField()
end = serializers.DateField()
rates = serializers.DictField()
price = serializers.FloatField()
currencies = serializers.ListField(
child=serializers.CharField()
)
bonus = serializers.FloatField()
current_raised = serializers.FloatField()
total_raised = serializers.FloatField()
Expand Down Expand Up @@ -86,4 +91,4 @@ class FAQSerializer(serializers.ModelSerializer):

class Meta:
model = FAQ
fields = ('id', 'number', 'question', 'answer')
fields = ('id', 'number', 'question', 'answer')
50 changes: 17 additions & 33 deletions apps/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def ipn(request):
code=code,
amount=amount,
currency='USD',
description=f'Purchased {purchased} {settings.ICO_TOKEN_SYMBOL.upper()}',
description=f'[{settings.ICO_STAGE}] Purchased {purchased} {settings.ICO_TOKEN_SYMBOL.upper()}',
status=status
)
elif status == 'confirmed':
Expand All @@ -63,7 +63,7 @@ def ipn(request):
code=helpers.transfer_tokens(user, purchased),
amount=purchased,
currency=settings.ICO_TOKEN_SYMBOL.upper(),
description=f'Received {purchased} {settings.ICO_TOKEN_SYMBOL.upper()}',
description=f'[{settings.ICO_STAGE}] Received {purchased} {settings.ICO_TOKEN_SYMBOL.upper()}',
status=status
)

Expand Down Expand Up @@ -117,47 +117,31 @@ def details(request):
Return details of ICO
"""

def get_amount_raised(current=False):
raised = dict()
for currency in settings.ICO_CURRENCIES:
if current:
raised[currency[0]] = Transaction.objects.filter(
currency=currency[0].upper(),
status='paid',
description=settings.ICO_STAGE.lower() + ' order'
).aggregate(raised=Sum('amount'))['raised'] or 0.0
current_raised = Transaction.objects.filter(
currency='USD',
status='confirmed',
description__contains=settings.ICO_STAGE
).aggregate(raised=Sum('amount'))['raised'] or 0.0

raised[currency[0]] *= helpers.get_rate(currency[0])

else:
raised[currency[0]] = Transaction.objects.filter(
currency=currency[0].upper(),
status='paid',
description__contains='order'
).aggregate(raised=Sum('amount'))['raised'] or 0.0
total_raised = Transaction.objects.filter(
currency='USD',
status='confirmed'
).aggregate(raised=Sum('amount'))['raised'] or 0.0

raised[currency[0]] *= helpers.get_rate(currency[0])

total = float()

for i in raised:
total += raised[i]

return total

current_raised = get_amount_raised(current=True)
total_raised = get_amount_raised()
rates = {settings.ICO_TOKEN_SYMBOL: settings.ICO_PRICE}
currencies = list()

for currency in settings.ICO_CURRENCIES:
rates[currency[0].upper()] = helpers.get_rate(currency[0])
currencies = currencies + [currency[0].upper()]

if request.method == 'GET':
serializer = ICODetailSerializer(data={
'token_name': settings.ICO_TOKEN_NAME,
'token_symbol': settings.ICO_TOKEN_SYMBOL,
'stage': settings.ICO_STAGE,
'start': settings.ICO_START,
'end': settings.ICO_END,
'rates': rates,
'price': settings.ICO_PRICE,
'currencies': currencies,
'bonus': settings.ICO_BONUS,
'current_raised': current_raised,
'total_raised': total_raised,
Expand Down
4 changes: 2 additions & 2 deletions wallet/settings/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
CLIENT_AUTH_BASE = "http://localhost:3000"

DJOSER = {
'PASSWORD_RESET_CONFIRM_URL': CLIENT_AUTH_BASE + '/password/reset/confirm/{uid}/{token}',
'ACTIVATION_URL': CLIENT_AUTH_BASE + '/activate/{uid}/{token}',
'PASSWORD_RESET_CONFIRM_URL': CLIENT_AUTH_BASE + '/reset-password?uid={uid}&token={token}',
'ACTIVATION_URL': CLIENT_AUTH_BASE + '/activate?uid={uid}&token={token}',
'SEND_ACTIVATION_EMAIL': False,
'SERIALIZERS': {
'user': 'base.serializers.UserSerializer'
Expand Down

0 comments on commit 6175c9d

Please sign in to comment.