def get_foreign_key_parameter(field_name, store): parameter = get_parameter_by_field(field_name, store) if not (parameter and parameter.foreign_key): msg = _('There is no defined %s parameter data' 'in the database.') % field_name raise DatabaseInconsistency(msg) return parameter
def get_interface(self): """ Based on the column values instantiate the stoqdrivers interface for the device itself. """ if self.device_name == '/dev/null': interface = 'serial' port = VirtualPort() product_id = vendor_id = None elif self.device_name.startswith('usb:'): # USB device interface, vendor_id, product_id = self.device_name.split(':') vendor_id = int(vendor_id, 16) product_id = int(product_id, 16) port = None else: # Serial device interface = 'serial' port = self._get_serial_port() product_id = vendor_id = None if self.type == DeviceSettings.CHEQUE_PRINTER_DEVICE: return ChequePrinter(brand=self.brand, model=self.model, port=port) elif self.type == DeviceSettings.NON_FISCAL_PRINTER_DEVICE: return NonFiscalPrinter(brand=self.brand, model=self.model, port=port, interface=interface, product_id=product_id, vendor_id=vendor_id) elif self.type == DeviceSettings.SCALE_DEVICE: return Scale(brand=self.brand, model=self.model, device=self.device_name, port=port) raise DatabaseInconsistency("The device type referred by this " "record (%r) is invalid, given %r." % (self, self.type))
def get_parameter_by_field(field_name, store): data = store.find(ParameterData, field_name=field_name).one() if data is None: raise DatabaseInconsistency( "Can't find a ParameterData object for the key %s" % field_name) return data
def _setup_widgets(self): individual = self.model.individual company = self.model.company if not (individual or company): raise DatabaseInconsistency('A person must have at least a ' 'company or an individual set.') tab_child = self.person_data_tab if individual and company: tab_text = _('Individual/Company Data') self.company_frame.set_label(_('Company Data')) self.company_frame.show() self.individual_frame.set_label(_('Individual Data')) self.individual_frame.show() elif individual: tab_text = _('Individual Data') self.company_frame.hide() self.individual_frame.set_label('') self.individual_frame.show() else: tab_text = _('Company Data') self.individual_frame.hide() self.company_frame.set_label('') self.company_frame.show() self.person_notebook.set_tab_label_text(tab_child, tab_text) addresses = self.model.get_total_addresses() if addresses == 2: self.address_button.set_label(_("1 More Address...")) elif addresses > 2: self.address_button.set_label( _("%i More Addresses...") % (addresses - 1)) if not self.model.client: self.credit_check_history_button.hide()
def _get_branches_for_filter(self): items = [(b.get_description(), b.id) for b in self._get_branches()] if not items: raise DatabaseInconsistency('You should have at least one ' 'branch on your database.' 'Found zero') items.insert(0, [_('All branches'), None]) return items
def _get_branches(self): items = [(b.person.name, b) for b in self.store.find(Branch)] if not items: raise DatabaseInconsistency('You should have at least one ' 'branch on your database.' 'Found zero') items.insert(0, [_('All branches'), None]) return items
def _set_admin_password(self, store): logger.info('_set_admin_password') adminuser = store.find(LoginUser, username=USER_ADMIN_DEFAULT_NAME).one() if adminuser is None: raise DatabaseInconsistency( ("You should have a user with username: %s" % USER_ADMIN_DEFAULT_NAME)) adminuser.set_password(self.login_password)
def get_header_data(): default_store = get_default_store() branch = get_current_branch(default_store) person = branch.person company = person.company main_address = person.get_main_address() if not person.name: # pragma nocover raise DatabaseInconsistency("The person by ID %r should have a " "name at this point" % (person.id, )) data = { 'title': branch.get_description(), 'lines': [], } # Address if main_address: address_parts = [] address_parts.append(main_address.get_address_string()) if main_address.postal_code: address_parts.append(main_address.postal_code) if main_address.get_city(): address_parts.append(main_address.get_city()) if main_address.get_state(): address_parts.append(main_address.get_state()) if address_parts: data['lines'].append(' - '.join(address_parts)) # Contact contact_parts = [] if person.phone_number: contact_parts.append(format_phone_number(person.phone_number)) if person.mobile_number: contact_parts.append(format_phone_number(person.mobile_number)) if person.fax_number: contact_parts.append( _("Fax: %s") % format_phone_number(person.fax_number)) if person.email: contact_parts.append(person.email) if contact_parts: data['lines'].append(' - '.join(contact_parts)) # Company details if company: company_parts = [] if company.get_cnpj_number(): company_parts.append(_("CNPJ: %s") % company.cnpj) if company.get_state_registry_number(): company_parts.append( _("State Registry: %s") % company.state_registry) if company_parts: data['lines'].append(' - '.join(company_parts)) return data
def _set_admin_password(self, store): logger.info('_set_admin_password') adminuser = store.find(LoginUser, username=USER_ADMIN_DEFAULT_NAME).one() if adminuser is None: raise DatabaseInconsistency( ("You should have a user with username: %s" % USER_ADMIN_DEFAULT_NAME)) # Lets create a user without password and set a cookie so that it # auto login adminuser.set_password(u'') get_utility(ICookieFile).store('admin', '')
def get_interface(self): """ Based on the column values instantiate the stoqdrivers interface for the device itself. """ port = SerialPort(device=self.device_name) if self.type == DeviceSettings.CHEQUE_PRINTER_DEVICE: return ChequePrinter(brand=self.brand, model=self.model, port=port) elif self.type == DeviceSettings.SCALE_DEVICE: return Scale(brand=self.brand, model=self.model, device=self.device_name) raise DatabaseInconsistency("The device type referred by this " "record (%r) is invalid, given %r." % (self, self.type))
def check_uptodate(self): """ Verify if the schema is up to date. :returns: True or False. """ # Fetch the latest, eg the last in the list patches = self._get_patches() latest_available = patches[-1].get_version() current_version = self.get_current_version() if current_version == latest_available: return True elif current_version > latest_available: current = "(%d.%d)" % current_version latest = "(%d.%d)" % latest_available raise DatabaseInconsistency( _('The current version of database %s is greater than the ' 'latest available version %s. Try upgrading your ' 'installation.') % (current, latest)) return False
def translate_status(cls, status): if not status in cls.statuses: raise DatabaseInconsistency(_(u'Got an unexpected status value: ' u'%s') % status) return cls.statuses[status]
def freight_type_name(self): if not self.freight_type in self.freight_types.keys(): raise DatabaseInconsistency(_(u'Invalid freight_type, got %d') % self.freight_type) return self.freight_types[self.freight_type]
def create_payment(self, branch, station: BranchStation, payment_type, payment_group, value, due_date=None, description=None, base_value=None, payment_number=None, identifier=None, ignore_max_installments=False): """Creates a new payment according to a payment method interface :param payment_type: the kind of payment, in or out :param payment_group: a :class:`PaymentGroup` subclass :param branch: the :class:`branch <stoqlib.domain.person.Branch>` associated with the payment, for incoming payments this is the branch receiving the payment and for outgoing payments this is the branch sending the payment. :param value: value of payment :param due_date: optional, due date of payment :param details: optional :param description: optional, description of the payment :param base_value: optional :param payment_number: optional :param ignore_max_installments: optional, defines whether max_installments should be ignored. :returns: a :class:`payment <stoqlib.domain.payment.Payment>` """ store = self.store if due_date is None: due_date = TransactionTimestamp() if not ignore_max_installments and payment_type == Payment.TYPE_IN: query = And(Payment.group_id == payment_group.id, Payment.method_id == self.id, Payment.payment_type == Payment.TYPE_IN, Payment.status != Payment.STATUS_CANCELLED) payment_count = store.find(Payment, query).count() if payment_count == self.max_installments: raise PaymentMethodError( _('You can not create more inpayments for this payment ' 'group since the maximum allowed for this payment ' 'method is %d') % self.max_installments) elif payment_count > self.max_installments: raise DatabaseInconsistency( _('You have more inpayments in database than the maximum ' 'allowed for this payment method')) if not description: description = self.describe_payment(payment_group) payment = Payment(store=store, branch=branch, station=station, identifier=identifier, payment_type=payment_type, due_date=due_date, value=value, base_value=base_value, group=payment_group, method=self, category=None, description=description, payment_number=payment_number) self.operation.payment_create(payment) return payment
def create_payment(self, payment_type, payment_group, branch, value, due_date=None, description=None, base_value=None, till=ValueUnset, payment_number=None): """Creates a new payment according to a payment method interface :param payment_type: the kind of payment, in or out :param payment_group: a :class:`PaymentGroup` subclass :param branch: the :class:`branch <stoqlib.domain.person.Branch>` associated with the payment, for incoming payments this is the branch receiving the payment and for outgoing payments this is the branch sending the payment. :param value: value of payment :param due_date: optional, due date of payment :param details: optional :param description: optional, description of the payment :param base_value: optional :param till: optional :param payment_number: optional :returns: a :class:`payment <stoqlib.domain.payment.Payment>` """ store = self.store if due_date is None: due_date = TransactionTimestamp() if payment_type == Payment.TYPE_IN: query = And(Payment.group_id == payment_group.id, Payment.method_id == self.id, Payment.payment_type == Payment.TYPE_IN, Payment.status != Payment.STATUS_CANCELLED) payment_count = store.find(Payment, query).count() if payment_count == self.max_installments: raise PaymentMethodError( _('You can not create more inpayments for this payment ' 'group since the maximum allowed for this payment ' 'method is %d') % self.max_installments) elif payment_count > self.max_installments: raise DatabaseInconsistency( _('You have more inpayments in database than the maximum ' 'allowed for this payment method')) if not description: description = self.describe_payment(payment_group) # If till is unset, do some clever guessing if till is ValueUnset: # We only need a till for inpayments if payment_type == Payment.TYPE_IN: till = Till.get_current(store) elif payment_type == Payment.TYPE_OUT: till = None else: raise AssertionError(payment_type) payment = Payment(store=store, branch=branch, payment_type=payment_type, due_date=due_date, value=value, base_value=base_value, group=payment_group, method=self, category=None, till=till, description=description, payment_number=payment_number) self.operation.payment_create(payment) return payment
def get_status_name(cls, status): if not status in cls.statuses: raise DatabaseInconsistency(_(u"Invalid status %d") % status) return cls.statuses[status]
def status_str(self): """The :obj:`Payment.status` as a translated string""" if not self.status in self.statuses: raise DatabaseInconsistency('Invalid status for Payment ' 'instance, got %d' % self.status) return self.statuses[self.status]
def _get_status_string(self): if not self.status in self.statuses: raise DatabaseInconsistency( _('Invalid status for product got %d') % self.status) return self.statuses[self.status]