def create_dummy_currency(index, currency_manager = None): if currency_manager == None: currency_manager = CurrencyManager() return currency_manager.create_currency( name = 'US%i Dollar' % index, code = 'USD%i' % index, symbol = '$%i' % index )
def create(self, client_id, invoice_no, invoice_date, sale_date, currency_id = None, language_id = None): ''' Create a new invoice ''' self._invoice_items = [] client = ClientManager.find_by_id(client_id) if currency_id is None: currency = client.default_currency else: currency = CurrencyManager.find_by_id(currency_id) if language_id is None: language = client.default_language else: language = LanguageManager.find_by_id(language_id) self._invoice = InvoiceEntity.create( user = self._user, client = client, currency = currency, language = language, invoice_no = invoice_no, invoice_date = invoice_date, sale_date = sale_date ) return self._invoice
def get(self, invoice_form=None, invoice_item_forms=[InvoiceItemForm(index=1)]): if invoice_form == None: # Initialize the Invoice form clients = self._client_manager.listify_clients() currencies = CurrencyManager.listify_currencies() languages = LanguageManager.listify_languages() invoice_form = InvoiceForm(clients, currencies, languages) return render_template( self, "invoice_create.html", {"invoice_form": invoice_form, "invoice_item_forms": invoice_item_forms} )
def __init__(self, *args, **kwargs): super(ClientForm, self).__init__(*args, **kwargs) # Initialize the currencies drop down currencies = CurrencyManager.list_currencies() currencies.insert(0, ("", "")) self.fields["default_currency"].choices = currencies # Initialize the languages drop down languages = LanguageManager.list_languages() languages.insert(0, ("", "")) self.fields["default_language"].choices = languages self.prefix = "client" self.auto_id = "%s"
def post(self): commit = False clients = self._client_manager.listify_clients() currencies = CurrencyManager.listify_currencies() languages = LanguageManager.listify_languages() invoice_form = InvoiceForm(data=self.request.POST, clients=clients, currencies=currencies, languages=languages) items = invoice_form.fields["items"].to_python(self.request.POST["invoice-items"]) or 0 max_item_index = int(self.request.POST["h-last-invoice-item-index"]) or 0 invoice_item_forms = [ InvoiceItemForm(index + 1, self.request.POST) for index in range(0, max_item_index) if self.request.POST.has_key("%i_invoice_item-description" % (index + 1)) ] if items > max_item_index: commit = False elif items == 0: commit = False elif invoice_form.is_valid(): # Validates the invoice items commit = True for form in invoice_item_forms: if not form.is_valid(): commit = False break if commit: self._invoice_manager.create( client_id=invoice_form.cleaned_data["client"], currency_id=invoice_form.cleaned_data["currency"], invoice_no=invoice_form.cleaned_data["invoice_no"], invoice_date=invoice_form.cleaned_data["invoice_date"], sale_date=invoice_form.cleaned_data["sale_date"], ) for invoice_item in invoice_item_forms: self._invoice_manager.add_invoice_item( description=invoice_item.cleaned_data["description"], quantity=invoice_item.cleaned_data["quantity"], unit_price=invoice_item.cleaned_data["unit_price"], ) self._invoice_manager.save() else: self.get(invoice_form, invoice_item_forms)
def setUp(self): super(Test_Currency, self).setUp() self.entity_manager = CurrencyManager()
class Test_Currency(BaseAppengineDatastoreTester): """ Currency management tests """ def setUp(self): super(Test_Currency, self).setUp() self.entity_manager = CurrencyManager() def test_create_currency(self): """ Test adding a currency """ currency = helpers.create_dummy_currency(1) self.verify_entity_instance(currency, CurrencyEntity) def test_create_existing_currency(self): """ Verify that adding a currency with an already existing symbol generates an exception """ currency = helpers.create_dummy_currency(1) try: currency = helpers.create_dummy_currency(1) self.fail('Added a duplicated currency symbol') except(CurrencyException): pass def test_add_exchange_rate_and_verify_conversion(self): """ Add an exchange rate from currency 1 to currency 2 and verify conversion occurs """ currency1 = helpers.create_dummy_currency(1) currency2 = helpers.create_dummy_currency(2) self.entity_manager.add_exchange_rate(from_currency = currency1, to_currency = currency2, date = date.today(), rate = 1.2) value1 = 10.0 value2 = self.entity_manager.convert_currency(from_currency = currency1, to_currency = currency2, date = date.today(), amount = value1) self.assertEqual(value2, 12.0) def test_exchange_rate_on_missing_day(self): """ Verify that a conversion using the rate of a day not inserted in the database generates an exception """ currency1 = helpers.create_dummy_currency(1) currency2 = helpers.create_dummy_currency(2) value1 = 10.0 try: self.entity_manager.convert_currency(from_currency = currency1, to_currency = currency2, date = date.today(), amount = value1) self.fail('Conversion occurred!!') except(CurrencyException): pass def test_exchange_rate_for_one_currency(self): """ Verify that an exchange rate cannot be created if the from and to currency are the same """ currency1 = helpers.create_dummy_currency(1) try: self.entity_manager.add_exchange_rate(from_currency = currency1, to_currency = currency1, date = date.today(), rate = 1.2) self.fail('Added a conversion rate from and to the same currency') except(CurrencyException): pass def test_duplicated_exchange_rate(self): """ Verify that a conversion rate cannot be added twice for the same day """ currency1 = helpers.create_dummy_currency(1) currency2 = helpers.create_dummy_currency(2) self.entity_manager.add_exchange_rate(from_currency = currency1, to_currency = currency2, date = date.today(), rate = 1.2) try: self.entity_manager.add_exchange_rate(from_currency = currency1, to_currency = currency2, date = date.today(), rate = 1.2) self.fail('Added an already existing conversion rate ') except(CurrencyException): pass def test_convert_as_list(self): """ Retrieve all currencies and convert to format usable in a dropdown as a tuple of pairs (id, name) """ for index in range (0, 5): helpers.create_dummy_currency(index) currencies = self.entity_manager.list_currencies() self.assertIsNotNone(currencies) self.assertEqual(len(currencies), 5) for currency in currencies: self.assertIsNotNone(currency) self.assertEqual(len(currency), 2)