示例#1
0
class PaymentCreatForm(forms.ModelForm):

    total = MoneyField()
    captured_amount = MoneyField()

    class Meta:
        model = Payment
        fields = '__all__'
示例#2
0
class CurrencyTradeForm(forms.Form):
    source_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(children__isnull=True),
        to_field_name='uuid'
    )
    source_amount = MoneyField(decimal_places=2)
    trading_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(children__isnull=True, type=Account.TYPES.trading),
        to_field_name='uuid',
        help_text='The account in which to perform the trade. '
                  'This account must support both the source and destination currency. If none exist '
                  'perhaps create one.'
    )
    destination_account = forms.ModelChoiceField(queryset=Account.objects.filter(children__isnull=True), to_field_name='uuid')
    destination_amount = MoneyField(decimal_places=2)
    description = forms.CharField(widget=forms.Textarea, required=False)

    def clean(self):
        cleaned_data = super(CurrencyTradeForm, self).clean()
        if self.errors:
            return cleaned_data

        source_account = cleaned_data['source_account']
        source_amount = cleaned_data['source_amount']
        trading_account = cleaned_data['trading_account']
        destination_amount = cleaned_data['destination_amount']
        destination_account = cleaned_data['destination_account']

        if source_amount.currency.code not in source_account.currencies:
            raise ValidationError('Source account does not support {}'.format(source_amount.currency))
        if source_amount.currency.code not in trading_account.currencies:
            raise ValidationError('Trading account does not support {}'.format(source_amount.currency))
        if destination_amount.currency.code not in trading_account.currencies:
            raise ValidationError('Trading account does not support {}'.format(source_amount.currency))
        if destination_amount.currency.code not in destination_account.currencies:
            raise ValidationError('Destination account does not support {}'.format(source_amount.currency))

        return cleaned_data

    @transaction.atomic()
    def save(self):
        source_account = self.cleaned_data.get('source_account')
        trading_account = self.cleaned_data.get('trading_account')
        destination_account = self.cleaned_data.get('destination_account')
        source_amount = self.cleaned_data.get('source_amount')
        destination_amount = self.cleaned_data.get('destination_amount')

        transaction = Transaction.objects.create(
            description=self.cleaned_data.get('description')
        )
        Leg.objects.create(transaction=transaction, account=source_account, amount=source_amount)
        Leg.objects.create(transaction=transaction, account=trading_account, amount=-source_amount)
        Leg.objects.create(transaction=transaction, account=trading_account, amount=destination_amount)
        Leg.objects.create(transaction=transaction, account=destination_account, amount=-destination_amount)
        return transaction
示例#3
0
class DisabledFieldForm(forms.ModelForm):
    if VERSION[:2] != (1, 8):
        money = MoneyField(disabled=True)

    class Meta:
        fields = "__all__"
        model = SimpleModel
示例#4
0
class LegForm(forms.ModelForm):
    """A form for representing a single transaction leg

    Attributes:

        account (TreeNodeChoiceField): Choose an account the leg will interact with
        description (forms.CharField): Optional description/notes for this leg
        amount (MoneyField): The amount for this leg. Positive values indicate money coming into the transaction,
            negative values indicate money leaving the transaction.

    See Also:

        This is a `ModelForm` for the :class:`Leg model <hordak.models.Leg>`.
    """
    account = TreeNodeChoiceField(Account.objects.all(), to_field_name='uuid')
    description = forms.CharField(required=False)
    amount = MoneyField(required=True, decimal_places=2)

    class Meta:
        model = Leg
        fields = ('amount', 'account', 'description')

    def __init__(self, *args, **kwargs):
        self.statement_line = kwargs.pop('statement_line', None)
        super(LegForm, self).__init__(*args, **kwargs)

    def clean_amount(self):
        amount = self.cleaned_data['amount']
        if amount.amount <= 0:
            raise ValidationError('Amount must be greater than zero')

        if self.statement_line and self.statement_line.amount < 0:
            amount *= -1

        return amount
示例#5
0
class UpdateGoalForm(ModelForm):
    balance = MoneyField(disabled=True)
    target_date = forms.DateField(
        widget=forms.DateInput(attrs={'type': 'date'}),
        validators=[NotInPastValidator])

    class Meta:
        model = Goal
        fields = ['name', 'target_amount', 'target_date', 'balance']
class QuotesForm(UploadForm):
    cycle_value = MoneyField(min_value=0,
                             max_value=999999,
                             currency_choices=[('GBP', 'Pound Sterling')],
                             label='',
                             required=True)
    step_type = forms.CharField(widget=forms.HiddenInput(
        attrs={'value': 'quote'}))

    class Meta:
        model = Quotes
        fields = ['file']
示例#7
0
class SimpleTransactionForm(forms.ModelForm):
    """A simplified form for transferring an an amount from one account to another

    This only allows the creation of transactions with two legs. This also uses
    :meth:`Account.transfer_to()`.

    See Also:

        * :meth:`hordak.models.Account.transfer_to()`.
    """

    from_account = TreeNodeChoiceField(
        queryset=Account.objects.all(), to_field_name="uuid"
    )
    to_account = TreeNodeChoiceField(
        queryset=Account.objects.all(), to_field_name="uuid"
    )
    amount = MoneyField(
        max_digits=MAX_DIGITS,
        decimal_places=DECIMAL_PLACES,
    )

    class Meta:
        model = Transaction
        fields = ["amount", "from_account", "to_account", "date", "description"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Limit currency choices if setup
        default_currency = DEFAULT_CURRENCY
        amount_field, currency_field = self.fields["amount"].fields

        self.fields["amount"].widget.widgets[1].choices = currency_field.choices = [
            (code, name)
            for code, name in currency_field.choices
            if code == default_currency or code in CURRENCIES
        ]
        self.fields["amount"].initial[1] = default_currency

    def save(self, commit=True):
        from_account = self.cleaned_data.get("from_account")
        to_account = self.cleaned_data.get("to_account")
        amount = self.cleaned_data.get("amount")

        return from_account.transfer_to(
            to_account=to_account,
            amount=amount,
            description=self.cleaned_data.get("description"),
            date=self.cleaned_data.get("date"),
        )
示例#8
0
class FiltersForm(forms.Form):
    salary_from = MoneyField(widget=CustomMoneyWidget,
                             default_currency='USD',
                             default_amount=0.0)
    salary_to = forms.FloatField()
    without_salary = forms.BooleanField()

    def clean_salary_from(self):
        if not self.cleaned_data['salary_from']:
            self.cleaned_data['salary_from'] = Money(0.0, 'USD')
        return self.cleaned_data['salary_from']

    experience_from = forms.IntegerField(widget=forms.NumberInput(
        attrs={'min': '0'}))
    experience_to = forms.IntegerField(widget=forms.NumberInput(
        attrs={'min': '0'}))
    without_experience = forms.BooleanField()

    EXP_TYPE = [
        ('months', 'months'),
        ('years', 'years'),
    ]
    experience_type = forms.ChoiceField(choices=EXP_TYPE)

    ORDER_BY_TYPE = [
        ('pub_dtime', 'date(older first)'),
        ('-pub_dtime', 'date(newer first)'),
        ('salary', 'salary(lowest first)'),
        ('-salary', 'salary(highest first)'),
        ('experience', 'experience(smallest first)'),
        ('-experience', 'experience(biggest first)'),
    ]
    order_by = forms.ChoiceField(choices=ORDER_BY_TYPE)

    city = forms.CharField(max_length=20)

    def __init__(self, _salary_from=None, *args, **kwargs):
        super(FiltersForm, self).__init__(*args, **kwargs)
        self.fields['city'].required = False
        self.fields['salary_from'].required = False
        self.fields['salary_to'].required = False
        self.fields['experience_from'].required = False
        self.fields['experience_to'].required = False
        self.fields['without_experience'].required = False
        self.fields['without_salary'].required = False

        self.fields[
            'salary_from'].initial = _salary_from if _salary_from is not None else Money(
                0.0, 'USD')
示例#9
0
class WalletTransactionForm(ModelForm):
    date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
    note = forms.CharField(widget=forms.Textarea, required=False)
    amount = MoneyField()
    category = forms.CharField(required=True)

    class Meta:
        model = WalletTransaction
        fields = ['amount', 'date', 'note', 'is_expense']
        labels = {
            'is_expense': 'Type',
        }

    def clean_amount(self):
        amount = self.cleaned_data.get('amount')
        if amount.amount < 0:
            raise forms.ValidationError('Amount must be 0 or grater')
        return amount
示例#10
0
class SimpleTransactionForm(forms.ModelForm):
    """A simplified form for transferring an an amount from one account to another

    This only allows the creation of transactions with two legs. This also uses
    :meth:`Account.transfer_to()`.

    See Also:

        * :meth:`hordak.models.Account.transfer_to()`.
    """
    from_account = TreeNodeChoiceField(queryset=Account.objects.all(), to_field_name='uuid')
    to_account = TreeNodeChoiceField(queryset=Account.objects.all(), to_field_name='uuid')
    amount = MoneyField(decimal_places=2)

    class Meta:
        model = Transaction
        fields = ['amount', 'from_account', 'to_account', 'date', 'description']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Limit currency choices if setup
        default_currency = getattr(settings, 'DEFAULT_CURRENCY', 'EUR')
        amount_field, currency_field = self.fields['amount'].fields

        self.fields['amount'].widget.widgets[1].choices = currency_field.choices = [
            (code, name)
            for code, name
            in currency_field.choices
            if code == default_currency or code in getattr(settings, 'CURRENCIES', [])
        ]
        self.fields['amount'].initial[1] = default_currency

    def save(self, commit=True):
        from_account = self.cleaned_data.get('from_account')
        to_account = self.cleaned_data.get('to_account')
        amount = self.cleaned_data.get('amount')

        return from_account.transfer_to(
            to_account=to_account,
            amount=amount,
            description=self.cleaned_data.get('description'),
            date=self.cleaned_data.get('date'),
        )
示例#11
0
class SimpleTransactionForm(forms.ModelForm):
    """A simplified form for transferring an an amount from one account to another

    This only allows the creation of transactions with two legs. This also uses
    :meth:`Account.transfer_to()`.

    See Also:

        * :meth:`hordak.models.Account.transfer_to()`.
    """
    from_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(children__isnull=True),
        to_field_name='uuid')
    to_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(children__isnull=True),
        to_field_name='uuid')
    amount = MoneyField(decimal_places=2)

    class Meta:
        model = Transaction
        fields = [
            'amount',
            'from_account',
            'to_account',
            'description',
        ]

    def save(self, commit=True):
        from_account = self.cleaned_data.get('from_account')
        to_account = self.cleaned_data.get('to_account')
        amount = self.cleaned_data.get('amount')

        return from_account.transfer_to(
            to_account=to_account,
            amount=amount,
            description=self.cleaned_data.get('description'))
示例#12
0
class MoneyFormMultipleCurrencies(forms.Form):
    money = MoneyField(currency_choices=[('SEK', 'Swedish Krona'), ('EUR', 'Euro')], max_value=1000, min_value=2)
示例#13
0
class PreciseForm(forms.Form):
    money = MoneyField()

    class Meta:
        fields = "__all__"
示例#14
0
class PriceFormMixin(forms.ModelForm):
    price = MoneyField(required=False, widget=PriceMoneyWidget)
示例#15
0
class MoneyForm(forms.Form):

    money = MoneyField(currency_choices=[(u'SEK', u'Swedish Krona')],
                       max_value=1000,
                       min_value=2)
示例#16
0
class OptionalMoneyForm(forms.Form):
    money = MoneyField(required=False,
                       currency_choices=[("SEK", "Swedish Krona")])
示例#17
0
class RefundPaymentForm(forms.Form):
    amount = MoneyField(min_value=0)
示例#18
0
class MoneyForm(forms.Form):
    money = MoneyField(currency_choices=[("SEK", "Swedish Krona")],
                       max_value=1000,
                       min_value=2)
示例#19
0
class MoneyFormMultipleCurrencies(forms.Form):
    money = MoneyField(currency_choices=[("SEK", "Swedish Krona"),
                                         ("EUR", "Euro")],
                       max_value=1000,
                       min_value=2)
示例#20
0
class OptionalMoneyForm(forms.Form):
    money = MoneyField(required=False, currency_choices=[('SEK', 'Swedish Krona')])
示例#21
0
class WalletCreateForm(ModelForm):
    balance = MoneyField(label="Initial Balance")

    class Meta:
        model = Wallet
        fields = ['name', 'type', 'balance']
示例#22
0
class CurrencyTradeForm(forms.Form):
    source_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(children__isnull=True), to_field_name="uuid"
    )
    source_amount = MoneyField(max_digits=MAX_DIGITS, decimal_places=DECIMAL_PLACES)
    trading_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(
            children__isnull=True, type=Account.TYPES.trading
        ),
        to_field_name="uuid",
        help_text="The account in which to perform the trade. "
        "This account must support both the source and destination currency. If none exist "
        "perhaps create one.",
    )
    destination_account = forms.ModelChoiceField(
        queryset=Account.objects.filter(children__isnull=True), to_field_name="uuid"
    )
    destination_amount = MoneyField(
        max_digits=MAX_DIGITS, decimal_places=DECIMAL_PLACES
    )
    description = forms.CharField(widget=forms.Textarea, required=False)

    def clean(self):
        cleaned_data = super(CurrencyTradeForm, self).clean()
        if self.errors:
            return cleaned_data

        source_account = cleaned_data["source_account"]
        source_amount = cleaned_data["source_amount"]
        trading_account = cleaned_data["trading_account"]
        destination_amount = cleaned_data["destination_amount"]
        destination_account = cleaned_data["destination_account"]

        if source_amount.currency.code not in source_account.currencies:
            raise ValidationError(
                "Source account does not support {}".format(source_amount.currency)
            )
        if source_amount.currency.code not in trading_account.currencies:
            raise ValidationError(
                "Trading account does not support {}".format(source_amount.currency)
            )
        if destination_amount.currency.code not in trading_account.currencies:
            raise ValidationError(
                "Trading account does not support {}".format(source_amount.currency)
            )
        if destination_amount.currency.code not in destination_account.currencies:
            raise ValidationError(
                "Destination account does not support {}".format(source_amount.currency)
            )

        return cleaned_data

    @transaction.atomic()
    def save(self):
        source_account = self.cleaned_data.get("source_account")
        trading_account = self.cleaned_data.get("trading_account")
        destination_account = self.cleaned_data.get("destination_account")
        source_amount = self.cleaned_data.get("source_amount")
        destination_amount = self.cleaned_data.get("destination_amount")

        transaction = Transaction.objects.create(
            description=self.cleaned_data.get("description")
        )
        Leg.objects.create(
            transaction=transaction, account=source_account, amount=source_amount
        )
        Leg.objects.create(
            transaction=transaction, account=trading_account, amount=-source_amount
        )
        Leg.objects.create(
            transaction=transaction, account=trading_account, amount=destination_amount
        )
        Leg.objects.create(
            transaction=transaction,
            account=destination_account,
            amount=-destination_amount,
        )
        return transaction
示例#23
0
class CapturePaymentForm(forms.Form):
    amount = MoneyField(min_value=0)
示例#24
0
class MoneyForm(forms.Form):

    money = MoneyField(currency_widget=CurrencySelectWidget(choices=[("a",
                                                                      "a")]))