You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.5 KiB
Python

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
from .models import Aggregator, NotificationSettings, Platform, User
# 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.",
}
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",
"enabled",
)
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.",
"enabled": "Whether or not the aggregator connection is enabled.",
}
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:
if up in self.fields[field].label:
self.fields[field].label = self.fields[field].label.replace(
up, up.upper()
)
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",
"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.",
"enabled": "Whether or not the platform connection is enabled.",
}