-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserializers.py
148 lines (121 loc) · 4.87 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from rest_framework import serializers
from banks.models import Banks, BanksExchangeRates, CurrencyMarkets
from crypto_exchanges.models import (CryptoExchanges, CryptoExchangesRates,
InterExchanges, InterExchangesUpdates,
IntraCryptoExchangesRates)
ROUND_TO = 10
class RoundingDecimalField(serializers.DecimalField):
"""
Custom redefinition of the display of decimal numbers.
"""
MIN_NON_EMPTY_DECIMAL: int = 5
def __init__(self, max_digits, decimal_places, percent=False, **kwargs):
super().__init__(max_digits, decimal_places, **kwargs)
self.percent = percent
def validate_precision(self, value):
return value
def quantize(self, value):
"""
Quantize the decimal value to the configured precision.
"""
if self.percent:
return round(value, self.decimal_places)
integers, real_decimal_places = map(
len, str(value).split('.'))
max_decimal_places = self.max_digits - integers
if real_decimal_places > max_decimal_places:
self.decimal_places = max_decimal_places
if integers >= self.max_digits:
self.decimal_places = 1
result = round(value, self.decimal_places)
if result == int(result):
return round(value, 1)
return result
class CryptoExchangesSerializer(serializers.ModelSerializer):
"""
A serializer for the CryptoExchanges model, which represents a
cryptocurrency exchange.
"""
class Meta:
model = CryptoExchanges
fields = ('name',)
class BanksSerializer(serializers.ModelSerializer):
"""
A serializer for the Banks model, which represents a bank.
"""
class Meta:
model = Banks
fields = ('name',)
class IntraCryptoExchangesRatesSerializer(serializers.ModelSerializer):
"""
A serializer for the IntraCryptoExchangesRates model, which represents
exchange rates between two cryptocurrencies on a single exchange.
"""
price = RoundingDecimalField(max_digits=ROUND_TO, decimal_places=None)
class Meta:
model = IntraCryptoExchangesRates
exclude = ('id', 'update', 'crypto_exchange')
class CryptoExchangesRatesSerializer(serializers.ModelSerializer):
"""
A serializer for the CryptoExchangesRates model, which represents exchange
rates between a cryptocurrency on a single exchange and a fiat currency on
banks.
"""
intra_crypto_exchange = IntraCryptoExchangesRatesSerializer(read_only=True)
price = RoundingDecimalField(max_digits=ROUND_TO, decimal_places=None)
class Meta:
model = CryptoExchangesRates
exclude = (
'id', 'update', 'trade_type', 'crypto_exchange', 'bank'
)
class CurrencyMarketsSerializer(serializers.ModelSerializer):
"""
A serializer for the CurrencyMarkets model, which represents a market for
exchanging currencies.
"""
class Meta:
model = CurrencyMarkets
fields = ('name',)
class BanksExchangeRatesSerializer(serializers.ModelSerializer):
"""
A serializer for the BanksExchangeRates model, which represents exchange
rates between two fiat currencies at a bank.
"""
bank = BanksSerializer(read_only=True)
currency_market = CurrencyMarketsSerializer(read_only=True)
price = RoundingDecimalField(max_digits=ROUND_TO, decimal_places=None)
class Meta:
model = BanksExchangeRates
exclude = ('id', 'update')
class UpdateSerializer(serializers.ModelSerializer):
"""
A serializer for the InterExchangesUpdates model, which represents
updates to the exchange rates between two fiat currencies on a bank or
between a cryptocurrency and a fiat currency on an exchange.
"""
class Meta:
model = InterExchangesUpdates
fields = ('updated',)
class InterExchangesSerializer(serializers.ModelSerializer):
"""
A serializer for the InterExchanges model, which represents an exchange of
currency or cryptocurrency between two banks and between a bank and an
exchange.
"""
crypto_exchange = CryptoExchangesSerializer(read_only=True)
input_bank = BanksSerializer(read_only=True)
output_bank = BanksSerializer(read_only=True)
input_crypto_exchange = CryptoExchangesRatesSerializer(read_only=True)
output_crypto_exchange = CryptoExchangesRatesSerializer(read_only=True)
interim_crypto_exchange = IntraCryptoExchangesRatesSerializer(
read_only=True)
second_interim_crypto_exchange = IntraCryptoExchangesRatesSerializer(
read_only=True)
bank_exchange = BanksExchangeRatesSerializer(read_only=True)
marginality_percentage = RoundingDecimalField(
max_digits=4, decimal_places=2, percent=True
)
update = UpdateSerializer(read_only=True)
class Meta:
model = InterExchanges
fields = '__all__'