diff --git a/api/currencies.json b/api/currencies.json new file mode 100644 index 00000000..bf3a4382 --- /dev/null +++ b/api/currencies.json @@ -0,0 +1,6 @@ +{ + "1":"USD", + "2":"EUR", + "3":"ETH", + "4":"ABC" +} diff --git a/api/models.py b/api/models.py index 452e57e3..6fa81646 100644 --- a/api/models.py +++ b/api/models.py @@ -7,6 +7,7 @@ from django.dispatch import receiver from django.utils.html import mark_safe from pathlib import Path +import json ############################# # TODO @@ -65,11 +66,6 @@ class Order(models.Model): BUY = 0, 'BUY' SELL = 1, 'SELL' - class Currencies(models.IntegerChoices): - USD = 1, 'USD' - EUR = 2, 'EUR' - ETH = 3, 'ETH' - class Status(models.IntegerChoices): WFB = 0, 'Waiting for bond' PUB = 1, 'Published in order book' @@ -92,6 +88,9 @@ class Order(models.Model): TLD = 18, 'Taker lost dispute' EXP = 19, 'Expired' + currency_dict = json.load(open('./api/currencies.json')) + currency_choices = [(int(val), label) for val, label in list(currency_dict.items())] + print(currency_choices) # order info status = models.PositiveSmallIntegerField(choices=Status.choices, null=False, default=Status.WFB) created_at = models.DateTimeField(auto_now_add=True) @@ -99,7 +98,7 @@ class Order(models.Model): # order details type = models.PositiveSmallIntegerField(choices=Types.choices, null=False) - currency = models.PositiveSmallIntegerField(choices=Currencies.choices, null=False) + currency = models.PositiveSmallIntegerField(choices=currency_choices, null=False) amount = models.DecimalField(max_digits=9, decimal_places=4, validators=[MinValueValidator(0.00001)]) payment_method = models.CharField(max_length=30, null=False, default="not specified", blank=True) diff --git a/api/urls.py b/api/urls.py index eae708dd..a3b73309 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,9 +1,10 @@ from django.urls import path -from .views import OrderMakerView, OrderView, UserView, BookView +from .views import OrderMakerView, OrderView, UserView, BookView, get_currencies_json urlpatterns = [ path('make/', OrderMakerView.as_view()), path('order/', OrderView.as_view({'get':'get','post':'take_or_update'})), path('usergen/', UserView.as_view()), path('book/', BookView.as_view()), + path('currencies/', get_currencies_json), ] \ No newline at end of file diff --git a/api/views.py b/api/views.py index 04fc6d42..470d02e7 100644 --- a/api/views.py +++ b/api/views.py @@ -21,6 +21,9 @@ from pathlib import Path from datetime import timedelta from django.utils import timezone +import json +from django.http import HttpResponse + # .env expiration_time = 8 @@ -263,6 +266,7 @@ class BookView(ListAPIView): def get(self,request, format=None): currency = request.GET.get('currency') + print("currency:", currency) type = request.GET.get('type') queryset = Order.objects.filter(currency=currency, type=type, status=int(Order.Status.PUB)) if len(queryset)== 0: @@ -281,7 +285,10 @@ class BookView(ListAPIView): book_data.append(data) return Response(book_data, status=status.HTTP_200_OK) - + +def get_currencies_json(request): + currency_dict = json.load(open('./api/currencies.json')) + return HttpResponse(json.dumps(currency_dict),content_type="application/json") diff --git a/frontend/src/components/BookPage.js b/frontend/src/components/BookPage.js index 93cb63d8..1f43c68a 100644 --- a/frontend/src/components/BookPage.js +++ b/frontend/src/components/BookPage.js @@ -8,8 +8,10 @@ export default class BookPage extends Component { orders: new Array(), currency: 1, type: 1, + currencies_dict: {"1":"USD"} }; this.getOrderDetails() + this.getCurrencyDict() this.state.currencyCode = this.getCurrencyCode(this.state.currency) } @@ -24,6 +26,15 @@ export default class BookPage extends Component { not_found: data.not_found, })); } + getCurrencyDict() { + fetch('/api/currencies') + .then((response) => response.json()) + .then((data) => + this.setState({ + currencies_dict: data + })); + + } handleCardClick=(e)=>{ console.log(e) @@ -48,7 +59,7 @@ export default class BookPage extends Component { // Gets currency code (3 letters) from numeric (e.g., 1 -> USD) // Improve this function so currencies are read from json getCurrencyCode(val){ - return (val == 1 ) ? "USD": ((val == 2 ) ? "EUR":"ETH") + return this.state.currencies_dict[val.toString()] } // pretty numbers @@ -156,9 +167,10 @@ export default class BookPage extends Component { }} onChange={this.handleCurrencyChange} > - USD - EUR - ETH + { + Object.entries(this.state.currencies_dict) + .map( ([key, value]) => {value} ) + } diff --git a/frontend/urls.py b/frontend/urls.py index 74fb0594..d7b68630 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -8,5 +8,6 @@ urlpatterns = [ path('make/', index), path('book/', index), path('order/', index), - path('wait/', index), + path('wait/', index), + path('currencies/',index) ] \ No newline at end of file