Implement wallet model and CRUD

This commit is contained in:
Mark Veidemanis 2023-03-17 18:37:38 +00:00
parent 6e6b23da63
commit 0723f14c53
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
9 changed files with 256 additions and 19 deletions

View File

@ -20,7 +20,16 @@ from django.contrib.auth.views import LogoutView
from django.urls import include, path
from two_factor.urls import urlpatterns as tf_urls
from core.views import ads, aggregators, banks, base, notifications, platforms, profit
from core.views import (
ads,
aggregators,
banks,
base,
notifications,
platforms,
profit,
wallets,
)
# from core.views.stripe_callbacks import Callback
@ -200,4 +209,25 @@ urlpatterns = [
profit.Profit.as_view(),
name="profit",
),
# Wallets
path(
"wallets/<str:type>/",
wallets.WalletList.as_view(),
name="wallets",
),
path(
"wallets/<str:type>/create/",
wallets.WalletCreate.as_view(),
name="wallet_create",
),
path(
"wallets/<str:type>/update/<str:pk>/",
wallets.WalletUpdate.as_view(),
name="wallet_update",
),
path(
"wallets/<str:type>/delete/<str:pk>/",
wallets.WalletDelete.as_view(),
name="wallet_delete",
),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -13,6 +13,7 @@ from .models import (
Provider,
Requisition,
User,
Wallet,
)
# flake8: noqa: E501
@ -90,10 +91,11 @@ class PlatformForm(RestrictedFormMixin, ModelForm):
upper = ["usd", "otp"]
for field in self.fields:
for up in upper:
if up in self.fields[field].label:
self.fields[field].label = self.fields[field].label.replace(
up, up.upper()
)
if self.fields[field].label:
if up in self.fields[field].label:
self.fields[field].label = self.fields[field].label.replace(
up, up.upper()
)
class Meta:
model = Platform
@ -117,6 +119,7 @@ class PlatformForm(RestrictedFormMixin, ModelForm):
"no_reference_amount_check_max_usd",
"base_usd",
"withdrawal_trigger",
"payees",
"enabled",
)
help_texts = {
@ -139,9 +142,17 @@ class PlatformForm(RestrictedFormMixin, ModelForm):
"no_reference_amount_check_max_usd": "When ticked, when no reference was found and a trade is higher than this amount, we will not accept payment even if it is the only one with this amount.",
"base_usd": "The amount in USD to keep in the platform.",
"withdrawal_trigger": "The amount above the base USD to trigger a withdrawal.",
"payees": "The wallet addresses to send profit concerning this platform to.",
"enabled": "Whether or not the platform connection is enabled.",
}
payees = forms.ModelMultipleChoiceField(
queryset=Wallet.objects.all(),
widget=forms.CheckboxSelectMultiple,
help_text=Meta.help_texts["payees"],
required=False,
)
class AdForm(RestrictedFormMixin, ModelForm):
def __init__(self, *args, **kwargs):
@ -214,9 +225,32 @@ class RequisitionForm(RestrictedFormMixin, ModelForm):
fields = (
"payment_details",
"transaction_source",
"payees",
)
help_texts = {
"payment_details": "Shown once a user opens a trade.",
"transaction_source": "Whether to check pending or booked transactions.",
"payees": "The wallet addresses to send profit concerning this requisition to.",
}
payees = forms.ModelMultipleChoiceField(
queryset=Wallet.objects.all(),
widget=forms.CheckboxSelectMultiple,
help_text=Meta.help_texts["payees"],
required=False,
)
class WalletForm(RestrictedFormMixin, ModelForm):
class Meta:
model = Wallet
fields = (
"name",
"address",
)
help_texts = {
"name": "The name of the wallet.",
"address": "The XMR address to send funds to.",
}

View File

@ -0,0 +1,39 @@
# Generated by Django 4.1.7 on 2023-03-17 18:15
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0027_alter_requisition_payment_details'),
]
operations = [
migrations.AddField(
model_name='requisition',
name='throughput',
field=models.FloatField(default=0),
),
migrations.CreateModel(
name='Wallet',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('address', models.CharField(max_length=255)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='platform',
name='payees',
field=models.ManyToManyField(blank=True, to='core.wallet'),
),
migrations.AddField(
model_name='requisition',
name='payees',
field=models.ManyToManyField(blank=True, to='core.wallet'),
),
]

View File

@ -0,0 +1,25 @@
# Generated by Django 4.1.7 on 2023-03-17 18:31
import uuid
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0028_requisition_throughput_wallet_platform_payees_and_more'),
]
operations = [
migrations.AlterField(
model_name='requisition',
name='id',
field=models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='wallet',
name='id',
field=models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False),
),
]

View File

@ -158,6 +158,17 @@ class Aggregator(models.Model):
return transaction
class Wallet(models.Model):
"""
A wallet for a user.
"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
address = models.CharField(max_length=255)
class Platform(models.Model):
"""
A connection to an arbitrage platform like AgoraDesk.
@ -192,7 +203,7 @@ class Platform(models.Model):
base_usd = models.FloatField(default=2800)
withdrawal_trigger = models.FloatField(default=200)
# payees = models.ManyToManyField(Wallet, blank=True)
payees = models.ManyToManyField(Wallet, blank=True)
enabled = models.BooleanField(default=True)
@ -488,21 +499,12 @@ class Trade(models.Model):
release_response = models.JSONField(default=dict)
# class BankAccount(models.Model):
# """
# Extra information for an account.
# """
# account_id = models.CharField(max_length=255)
# payment_details = models.TextField()
# enabled = models.BooleanField(default=True)
class Requisition(models.Model):
"""
A requisition for an Aggregator
"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
aggregator = models.ForeignKey(Aggregator, on_delete=models.CASCADE)
requisition_id = models.CharField(max_length=255)
@ -512,8 +514,8 @@ class Requisition(models.Model):
max_length=255, choices=TRANSACTION_SOURCE_CHOICES, default="booked"
)
# throughput = models.FloatField(default=0)
# payees = models.ManyToManyField(Wallet, blank=True)
throughput = models.FloatField(default=0)
payees = models.ManyToManyField(Wallet, blank=True)
assets = {

View File

@ -271,6 +271,9 @@
<a class="navbar-item" href="{% url 'platforms' type='page' %}">
Platform Connections
</a>
<a class="navbar-item" href="{% url 'wallets' type='page' %}">
Wallets
</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">

View File

@ -22,7 +22,7 @@
<td>
<a
class="has-text-grey"
onclick="window.prompt('Copy to clipboard: Ctrl+C, Enter', '{{ item.id }}/');">
onclick="window.prompt('Copy to clipboard: Ctrl+C, Enter', '{{ item.id }}');">
<span class="icon" data-tooltip="Copy to clipboard">
<i class="fa-solid fa-copy" aria-hidden="true"></i>
</span>

View File

@ -0,0 +1,69 @@
{% load cache %}
{% load cachalot cache %}
{% get_last_invalidation 'core.Ad' as last %}
{% include 'mixins/partials/notify.html' %}
{# cache 600 objects_ads request.user.id object_list type last #}
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>user</th>
<th>name</th>
<th>address</th>
<th>actions</th>
</thead>
{% for item in object_list %}
<tr>
<td>
<a
class="has-text-grey"
onclick="window.prompt('Copy to clipboard: Ctrl+C, Enter', '{{ item.id }}');">
<span class="icon" data-tooltip="Copy to clipboard">
<i class="fa-solid fa-copy" aria-hidden="true"></i>
</span>
</a>
</td>
<td>{{ item.user }}</td>
<td>{{ item.name }}</td>
<td>{{ item.address }}</td>
<td>
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'wallet_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-pencil"></i>
</span>
</span>
</button>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{% url 'wallet_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-xmark"></i>
</span>
</span>
</button>
</div>
</td>
</tr>
{% endfor %}
</table>
{# endcache #}

35
core/views/wallets.py Normal file
View File

@ -0,0 +1,35 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from mixins.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
from two_factor.views.mixins import OTPRequiredMixin
from core.forms import WalletForm
from core.models import Wallet
class WalletList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/wallet-list.html"
model = Wallet
page_title = "List of wallets"
list_url_name = "wallets"
list_url_args = ["type"]
submit_url_name = "wallet_create"
class WalletCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Wallet
form_class = WalletForm
submit_url_name = "wallet_create"
class WalletUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Wallet
form_class = WalletForm
submit_url_name = "wallet_update"
class WalletDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Wallet