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 ( Ad, Aggregator, Asset, NotificationSettings, Platform, Provider, 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", "base_usd", "withdrawal_trigger", "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.", "base_usd": "The amount in USD to keep in the platform.", "withdrawal_trigger": "The amount above the base USD to trigger a withdrawal.", "enabled": "Whether or not the platform connection is enabled.", } 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", "platforms", "aggregators", "account_whitelist", "visible", "enabled", ) help_texts = { "name": "The name of the aggregator connection.", "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.", "platforms": "Enabled platforms for this ad", "aggregators": "Enabled aggregators for this ad", "account_whitelist": "List of account IDs to use, one per line.", "visible": "Whether or not this ad is visible.", "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, ) 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, )