2023-03-05 20:09:31 +00:00
from django import forms
from django . contrib . auth . forms import UserCreationForm
from django . core . exceptions import FieldDoesNotExist
from django . forms import ModelForm
from mixins . restrictions import RestrictedFormMixin
2023-03-10 02:21:36 +00:00
from . models import (
Ad ,
Aggregator ,
Asset ,
2023-03-18 10:48:07 +00:00
LinkGroup ,
2023-03-10 02:21:36 +00:00
NotificationSettings ,
2023-03-20 11:06:37 +00:00
OperatorWallets ,
2023-05-06 10:12:54 +00:00
Payout ,
2023-03-10 02:21:36 +00:00
Platform ,
Provider ,
2023-03-16 20:20:36 +00:00
Requisition ,
2023-03-10 02:21:36 +00:00
User ,
2023-03-17 18:37:38 +00:00
Wallet ,
2023-03-10 02:21:36 +00:00
)
2023-03-05 20:09:31 +00:00
# flake8: noqa: E501
class NewUserForm ( UserCreationForm ) :
email = forms . EmailField ( required = True )
class Meta :
model = User
fields = (
" username " ,
" email " ,
" first_name " ,
" last_name " ,
" password1 " ,
" password2 " ,
)
def save ( self , commit = True ) :
user = super ( NewUserForm , self ) . save ( commit = False )
user . email = self . cleaned_data [ " email " ]
if commit :
user . save ( )
return user
class CustomUserCreationForm ( UserCreationForm ) :
class Meta :
model = User
fields = " __all__ "
class NotificationSettingsForm ( RestrictedFormMixin , ModelForm ) :
class Meta :
model = NotificationSettings
fields = (
" ntfy_topic " ,
" ntfy_url " ,
)
help_texts = {
" ntfy_topic " : " The topic to send notifications to. " ,
" ntfy_url " : " Custom NTFY server. Leave blank to use the default server. " ,
}
2023-03-07 16:59:39 +00:00
class AggregatorForm ( RestrictedFormMixin , ModelForm ) :
def __init__ ( self , * args , * * kwargs ) :
super ( AggregatorForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ " secret_id " ] . label = " Secret ID "
class Meta :
model = Aggregator
fields = (
" name " ,
" service " ,
" secret_id " ,
" secret_key " ,
" poll_interval " ,
2023-03-18 10:48:07 +00:00
" link_group " ,
2023-03-07 17:02:09 +00:00
" enabled " ,
2023-03-07 16:59:39 +00:00
)
help_texts = {
" name " : " The name of the aggregator connection. " ,
" service " : " The aggregator service to use. " ,
" secret_id " : " The secret ID for the aggregator service. " ,
" secret_key " : " The secret key for the aggregator service. " ,
" poll_interval " : " The interval in seconds to poll the aggregator service. " ,
2023-03-18 10:48:07 +00:00
" link_group " : " The link group to use for this aggregator connection. " ,
2023-03-07 17:02:09 +00:00
" enabled " : " Whether or not the aggregator connection is enabled. " ,
2023-03-07 16:59:39 +00:00
}
2023-03-09 23:27:16 +00:00
class PlatformForm ( RestrictedFormMixin , ModelForm ) :
def __init__ ( self , * args , * * kwargs ) :
super ( PlatformForm , self ) . __init__ ( * args , * * kwargs )
upper = [ " usd " , " otp " ]
for field in self . fields :
for up in upper :
2023-03-17 18:37:38 +00:00
if self . fields [ field ] . label :
if up in self . fields [ field ] . label :
self . fields [ field ] . label = self . fields [ field ] . label . replace (
up , up . upper ( )
)
2023-03-09 23:27:16 +00:00
class Meta :
model = Platform
fields = (
" name " ,
" service " ,
" token " ,
" password " ,
" otp_token " ,
" username " ,
" send " ,
" cheat " ,
" dummy " ,
" cheat_interval_seconds " ,
" margin " ,
" max_margin " ,
" min_margin " ,
" min_trade_size_usd " ,
" max_trade_size_usd " ,
" accept_within_usd " ,
" no_reference_amount_check_max_usd " ,
2023-03-10 15:34:46 +00:00
" base_usd " ,
" withdrawal_trigger " ,
2023-03-17 18:37:38 +00:00
" payees " ,
2023-03-18 10:48:07 +00:00
" link_group " ,
2023-03-09 23:27:16 +00:00
" enabled " ,
)
help_texts = {
" name " : " The name of the platform connection. " ,
" service " : " The platform service to use. " ,
" token " : " The JWT auth token. " ,
" password " : " Account password " ,
" otp_token " : " The OTP secret key. " ,
" username " : " Account username " ,
" send " : " Whether or not to send messages on new trades. " ,
" cheat " : " Whether or not to run the Autoprice cheat. " ,
" dummy " : " When enabled, the trade escrow feature will be disabled. " ,
" cheat_interval_seconds " : " The interval in seconds to run the Autoprice cheat. " ,
" margin " : " The current margin. Only valid for initial ads post. Autoprice will override this. " ,
" max_margin " : " The maximum margin to use. " ,
" min_margin " : " The minimum margin to use. " ,
" min_trade_size_usd " : " The minimum trade size in USD. " ,
" max_trade_size_usd " : " The maximum trade size in USD. " ,
" accept_within_usd " : " When a trade is wrong by less than this amount, it will be accepted. " ,
" 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. " ,
2023-03-10 15:34:46 +00:00
" base_usd " : " The amount in USD to keep in the platform. " ,
" withdrawal_trigger " : " The amount above the base USD to trigger a withdrawal. " ,
2023-03-17 18:37:38 +00:00
" payees " : " The wallet addresses to send profit concerning this platform to. " ,
2023-03-18 10:48:07 +00:00
" link_group " : " The link group to use for this platform. " ,
2023-03-09 23:27:16 +00:00
" enabled " : " Whether or not the platform connection is enabled. " ,
}
2023-03-10 02:21:36 +00:00
2023-03-17 18:37:38 +00:00
payees = forms . ModelMultipleChoiceField (
queryset = Wallet . objects . all ( ) ,
widget = forms . CheckboxSelectMultiple ,
help_text = Meta . help_texts [ " payees " ] ,
required = False ,
)
2023-03-10 02:21:36 +00:00
class AdForm ( RestrictedFormMixin , ModelForm ) :
def __init__ ( self , * args , * * kwargs ) :
super ( AdForm , self ) . __init__ ( * args , * * kwargs )
class Meta :
model = Ad
fields = (
" name " ,
" text " ,
" payment_details " ,
" payment_details_real " ,
" payment_method_details " ,
" dist_list " ,
" asset_list " ,
" provider_list " ,
2023-03-18 10:48:07 +00:00
# "platforms",
# "aggregators",
2023-04-18 07:31:36 +00:00
" require_feedback_score " ,
2023-03-10 15:04:39 +00:00
" account_whitelist " ,
2023-03-14 09:38:39 +00:00
" send_reference " ,
2023-03-10 02:21:36 +00:00
" visible " ,
2023-03-18 10:48:07 +00:00
" link_group " ,
2023-03-10 02:21:36 +00:00
" enabled " ,
)
help_texts = {
2023-03-10 20:18:17 +00:00
" name " : " The name of the ad. " ,
2023-03-10 02:21:36 +00:00
" text " : " The content of the ad. " ,
" payment_details " : " Shown before a user opens a trade. " ,
" payment_details_real " : " Shown after a user opens a trade. " ,
" payment_method_details " : " Shown in the list " ,
" dist_list " : " Currency and country, space separated, one pair per line. " ,
" asset_list " : " List of assets to distribute ads for. " ,
" provider_list " : " List of providers to distribute ads for. " ,
2023-03-18 10:48:07 +00:00
# "platforms": "Enabled platforms for this ad",
# "aggregators": "Enabled aggregators for this ad",
2023-04-18 07:31:36 +00:00
" require_feedback_score " : " Mminimum feedback score for users. Set to 0 to disable. " ,
2023-03-10 15:04:39 +00:00
" account_whitelist " : " List of account IDs to use, one per line. " ,
2023-03-14 09:38:39 +00:00
" send_reference " : " Whether or not to send the reference on new trades. " ,
2023-03-10 02:21:36 +00:00
" visible " : " Whether or not this ad is visible. " ,
2023-03-18 10:48:07 +00:00
" link_group " : " The link group to use for this ad. " ,
2023-03-10 02:21:36 +00:00
" enabled " : " Whether or not this ad is enabled. " ,
}
asset_list = forms . ModelMultipleChoiceField (
queryset = Asset . objects . all ( ) ,
widget = forms . CheckboxSelectMultiple ,
help_text = Meta . help_texts [ " asset_list " ] ,
required = True ,
)
provider_list = forms . ModelMultipleChoiceField (
queryset = Provider . objects . all ( ) ,
widget = forms . CheckboxSelectMultiple ,
help_text = Meta . help_texts [ " provider_list " ] ,
required = True ,
)
2023-03-18 10:48:07 +00:00
# platforms = forms.ModelMultipleChoiceField(
# queryset=Platform.objects.all(),
# widget=forms.CheckboxSelectMultiple,
# help_text=Meta.help_texts["platforms"],
# required=True,
# )
# aggregators = forms.ModelMultipleChoiceField(
# queryset=Aggregator.objects.all(),
# widget=forms.CheckboxSelectMultiple,
# help_text=Meta.help_texts["aggregators"],
# required=True,
# )
2023-03-16 20:20:36 +00:00
class RequisitionForm ( RestrictedFormMixin , ModelForm ) :
class Meta :
model = Requisition
fields = (
" payment_details " ,
2023-04-18 07:58:36 +00:00
" owner_name " ,
2023-03-16 20:20:36 +00:00
" transaction_source " ,
2023-03-17 18:37:38 +00:00
" payees " ,
2023-03-16 20:20:36 +00:00
)
help_texts = {
" payment_details " : " Shown once a user opens a trade. " ,
2023-04-18 07:58:36 +00:00
" owner_name " : " Owner name to send with payment details if not provided by aggregator. " ,
2023-03-16 20:20:36 +00:00
" transaction_source " : " Whether to check pending or booked transactions. " ,
2023-03-17 18:37:38 +00:00
" 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. " ,
2023-03-16 20:20:36 +00:00
}
2023-03-18 10:48:07 +00:00
class LinkGroupForm ( RestrictedFormMixin , ModelForm ) :
class Meta :
model = LinkGroup
fields = (
" name " ,
" platform_owner_cut_percentage " ,
" requisition_owner_cut_percentage " ,
" operator_cut_percentage " ,
" enabled " ,
)
help_texts = {
" name " : " The name of the link group. " ,
" platform_owner_cut_percentage " : " The percentage of the total profit of this group to give to the platform owners. " ,
" requisition_owner_cut_percentage " : " The percentage of the total profit of this group to give to the requisition owners. " ,
" operator_cut_percentage " : " The percentage of the total profit of this group to give to the operator. " ,
" enabled " : " Whether or not this link group is enabled. " ,
}
def clean ( self ) :
cleaned_data = super ( LinkGroupForm , self ) . clean ( )
platform_owner_cut_percentage = cleaned_data . get (
" platform_owner_cut_percentage "
)
requisition_owner_cut_percentage = cleaned_data . get (
" requisition_owner_cut_percentage "
)
operator_cut_percentage = cleaned_data . get ( " operator_cut_percentage " )
total_sum = (
platform_owner_cut_percentage
+ requisition_owner_cut_percentage
+ operator_cut_percentage
)
if total_sum != 100 :
self . add_error (
" platform_owner_cut_percentage " ,
f " The sum of the percentages must be 100, not { total_sum } . " ,
)
self . add_error (
" requisition_owner_cut_percentage " ,
f " The sum of the percentages must be 100, not { total_sum } . " ,
)
self . add_error (
" operator_cut_percentage " ,
f " The sum of the percentages must be 100, not { total_sum } . " ,
)
return
return cleaned_data
2023-03-20 11:06:37 +00:00
class OperatorWalletsForm ( RestrictedFormMixin , ModelForm ) :
class Meta :
model = OperatorWallets
fields = ( " payees " , )
help_texts = {
" payees " : " Wallets to designate as payees for this operator. " ,
}
payees = forms . ModelMultipleChoiceField (
queryset = Wallet . objects . all ( ) ,
widget = forms . CheckboxSelectMultiple ,
help_text = Meta . help_texts [ " payees " ] ,
required = False ,
)
2023-05-06 10:12:54 +00:00
class PayoutForm ( RestrictedFormMixin , ModelForm ) :
class Meta :
model = Payout
fields = (
" wallet " ,
" amount " ,
" description " ,
)
help_texts = {
" wallet " : " The wallet the payment was sent to. " ,
" amount " : " The amount of the payment. " ,
" description " : " The description of the payment. " ,
}