Add Currency Support To Account Model

by ADMIN 38 views

Overview

In today's globalized economy, it's essential to track investments in different currencies. To cater to this requirement, we'll be adding currency support to the Account model. This feature will enable users to create and manage accounts in various currencies, including CNY, USD, EUR, JPY, HKD, SGD, and AUD.

Implementation Details

Database Changes

To add currency support, we'll need to make changes to the database schema. We'll add a new column to the Account model in models.py to store the currency.

currency = db.Column(db.String(3), nullable=False, default='CNY')

Code Changes Required

We'll need to update the Account model in both models.py and app.py to include the currency field. We'll also update the to_dict() method to include the currency and set the default currency as CNY.

class Account(db.Model):
    # existing fields...
    currency = db.Column(db.String(3), nullable=False, default='CNY')

    def to_dict(self):
        # existing fields...
        return {
            'id': self.id,
            'name': self.name,
            'balance': self.balance,
            'currency': self.currency,
        }

Database Migration

To add the currency column to the existing accounts, we'll create a new migration file. We'll set the default currency as 'CNY' for existing accounts.

flask db migrate -m "Add currency column"
flask db upgrade

Frontend Updates

We'll need to update the frontend to include a currency selection dropdown in the account creation/edit form. We'll also update the form handling in static/scripts.js to add the currency field to account creation/update and display the currency alongside the market value in the account list.

// static/scripts.js
const currencyOptions = [
    { value: 'CNY', label: 'Chinese Yuan' },
    { value: 'USD', label: 'US Dollar' },
    { value: 'EUR', label: 'Euro' },
    { value: 'JPY', label: 'Japanese Yen' },
    { value: 'HKD', label: 'Hong Kong Dollar' },
    { value: 'SGD', label: 'Singapore Dollar' },
    { value: 'AUD', label: 'Australian Dollar' },
];

// ...

const accountForm = document.getElementById('account-form');
accountForm.addEventListener('submit', (e) => {
    e.preventDefault();
    const formData = new FormData(accountForm);
    const currency = formData.get('currency');
    // ...
});

API Updates

We'll need to update the /api/accounts POST endpoint to accept the currency and update the /api/accounts/<int:id> PUT endpoint to handle currency updates. We'll also ensure that the currency is returned in account GET responses.

# app.py
from flask import jsonify

@app.route('/api/accounts', methods=['POST'])
def create_account():
    # ...
    data = request.get_json()
    currency = data.get('currency')
    # ...
    return jsonify({'account': account.to_dict()})

@app.route('/api/accounts/<int:id>', methods=['PUT'])
def update_account(id    # ...
    data = request.get_json()
    currency = data.get('currency')
    # ...
    return jsonify({'account': account.to_dict()})

@app.route('/api/accounts/<int:id>', methods=['GET'])
def get_account(id):
    account = Account.query.get(id)
    return jsonify({'account': account.to_dict()})

Sample Currency Options

Here's a sample list of supported currencies:

SUPPORTED_CURRENCIES = [
    'CNY',  # Chinese Yuan
    'USD',  # US Dollar
    'EUR',  # Euro
    'JPY',  # Japanese Yen
    'HKD',  # Hong Kong Dollar
    'SGD',  # Singapore Dollar
    'AUD',  # Australian Dollar
]

Testing Requirements


To ensure that the currency support feature works as expected, we'll need to write tests to cover the following scenarios:

  1. Test creation of new accounts with different currencies
  2. Test editing existing accounts to change currency
  3. Verify currency displays correctly in UI
  4. Verify default currency (CNY) works as expected
  5. Test data persistence of currency field
  6. Verify currency field is included in all relevant API responses

Future Considerations


While adding currency support is a significant step forward, there are a few additional features that we could consider implementing in the future:

  • Currency conversion functionality: This would allow users to convert their account balances between different currencies.
  • Currency symbol display: This would display the currency symbol alongside the account balance in the UI.
  • Formatting numbers based on currency locale: This would ensure that numbers are formatted correctly based on the user's locale.

Q: Why do we need to add currency support to the Account model?

A: In today's globalized economy, it's essential to track investments in different currencies. By adding currency support to the Account model, we can enable users to create and manage accounts in various currencies, making it easier for them to manage their finances across different regions.

Q: What are the benefits of adding currency support to the Account model?

A: The benefits of adding currency support to the Account model include:

  • Improved user experience: Users can now create and manage accounts in their preferred currency, making it easier for them to track their finances.
  • Increased flexibility: The ability to support multiple currencies allows users to invest in different markets and regions, increasing their investment options.
  • Enhanced data accuracy: By storing currency information in the Account model, we can ensure that data is accurate and up-to-date, reducing errors and inconsistencies.

Q: How do we add currency support to the Account model?

A: To add currency support to the Account model, we need to make changes to the database schema, update the Account model in both models.py and app.py, and update the frontend to include a currency selection dropdown in the account creation/edit form.

Q: What are the database changes required to add currency support?

A: To add currency support, we need to add a new column to the Account model in models.py to store the currency. We'll also need to create a new migration file to add the currency column to the existing accounts.

Q: How do we update the frontend to include a currency selection dropdown?

A: To update the frontend, we need to add a currency selection dropdown in the account creation/edit form and update the form handling in static/scripts.js to add the currency field to account creation/update and display the currency alongside the market value in the account list.

Q: What are the API updates required to add currency support?

A: To add currency support, we need to update the /api/accounts POST endpoint to accept the currency and update the /api/accounts/<int:id> PUT endpoint to handle currency updates. We'll also need to ensure that the currency is returned in account GET responses.

Q: How do we test the currency support feature?

A: To test the currency support feature, we need to write tests to cover the following scenarios:

  • Test creation of new accounts with different currencies
  • Test editing existing accounts to change currency
  • Verify currency displays correctly in UI
  • Verify default currency (CNY) works as expected
  • Test data persistence of currency field
  • Verify currency field is included in all relevant API responses

Q: What are the future considerations for adding currency support?

A: While adding currency support is a significant step forward, there are a few additional features that we could consider implementing in the future, including:

  • Currency conversion functionality: This would allow users to convert their account balances between different currencies.
  • Currency symbol display: This would display the currency symbol alongside the account balance in the UI.
  • Formatting numbers based on currency locale: This would ensure that numbers are formatted correctly based on the user's locale.

By implementing these features, we can provide a more comprehensive and user-friendly experience for our users.