def save(self, **kwargs): domain = self.validated_data['domain'] account_id = self.validated_data['account_id'] registrar = self.validated_data['registrar'] dns = self.validated_data.get('dns', '') logger.info('Begin registrar session: %s, %s, %s (%s)' % (domain, account_id, registrar, dns)) native_contract = RegistrarContract.find_native_by_domain(domain, registrar) if native_contract: contract, order = self.prolong_domain(domain, registrar, native_contract, account_id) else: contract, order = self.register_domain(domain, registrar, account_id, dns.split(',')) # request balance registrar_config = DomainRegistrarConfig(registrar) reg_connector = registrar_config.get_connector() registrar_balance = reg_connector.get_balance() return { 'id': order.fields['order_id'], 'domain': domain, 'balance': registrar_balance, 'contract': contract.number }
def register_domain(self, domain, registrar, account_id, dns_list): """ Register the new domain to account. :param dns_list: :param domain: Domain to register. :param registrar: Name of the registrar. :param account_id: ID of the domain owner. :return: Registration order. """ assert domain assert registrar assert account_id > 0 personal_data = RegistrarContract.get_linked_personal_data(account_id, registrar) registrar_contract, created = RegistrarContract.get_or_create_contract(personal_data, registrar) order = registrar_contract.get_native().domain_register(domain, nserver='\n'.join(dns_list)) logger.info("Order created: %s. Domain %s registration for %s." % (order, domain, registrar_contract.number)) RegistrarOrder.objects.create(id=order.fields['order_id'], contract=registrar_contract, domain=domain, is_prolong=False) return registrar_contract, order
def prolong_domain(self, domain, registrar, native_contract, account_id): """ Prolongate domain using known registrar contract. :param domain: Domain to prolong. :param registrar: Name of the registrar. :param native_contract: Used native registrar contract. :return: Prolong order. """ assert domain assert registrar assert native_contract assert account_id > 0 prolong_years = 1 personal_data = RegistrarContract.get_matched_personal_data(native_contract, account_id) registrar_contract = RegistrarContract.link_native_contract(native_contract, personal_data) order = native_contract.domain_prolong(prolong_years, domain) logger.info("Order created: %s. Domain %s prolongation for %s." % (order, domain, native_contract.number)) RegistrarOrder.objects.create(id=order.fields['order_id'], contract=registrar_contract, domain=domain, is_prolong=True, prolong_years=prolong_years) return native_contract, order
def handle_rf(self, order_item): assert order_item original_domain = order_item['domain'] order_item['domain'] = idna.encode(order_item['domain']) logger.info("IDNA convert: %s -> %s" % (original_domain, order_item['domain'])) return order_item
def _handle_domain(self, *args, **options): """ Handle operations with domains. :param args: :param options: :return: """ registrar_config = DomainRegistrarConfig(self.registrar_name) reg_connector = registrar_config.get_connector() if options['prolong']: assert self.contract, _("Specify profile or existing linked contract.") assert options['prolong'], _("Specify the prolongation period.") prolong_years = int(options['prolong']) contracts = list(reg_connector.find_contracts({'contract-num': self.contract})) if len(contracts) > 0: contract = contracts[0] for domain_name in options['domain']: order = contract.domain_prolong(prolong_years, domain_name) logger.info("Order created: %s" % order) elif options['register']: assert self.contract, _("Specify profile or existing linked contract.") contracts = list(reg_connector.find_contracts({'contract-num': self.contract})) if len(contracts) > 0: contract = contracts[0] name_servers = options['nameserver'].split(',') for domain_name in options['domain']: order = contract.domain_register(domain_name, nserver='\n'.join(name_servers)) logger.info( "Order created: %s. Domain %s registration on %s." % (order, domain_name, self.contract)) else: logger.info("There is no such contract %s in %s" % (self.contract, self.registrar_name)) elif options['update']: assert self.contract, _("Specify profile or existing linked contract.") contracts = list(reg_connector.find_contracts({'contract-num': self.contract})) if len(contracts) > 0: contract = contracts[0] name_servers = options['nameserver'].split(',') for domain_name in options['domain']: order = contract.domain_update(domain_name, nserver='\n'.join(name_servers)) logger.info("Order created: %s. Domain %s update in %s." % (order, domain_name, self.contract)) else: logger.info("There is no such contract %s in %s" % (self.contract, self.registrar_name))
def _handle_service(self, *args, **options): """ Handle operations with orders. :param args: :param options: :return: """ registrar_config = DomainRegistrarConfig(self.registrar_name) reg_connector = registrar_config.get_connector() if options['list']: assert self.contract, _("Specify profile or existing linked contract.") contracts = list(reg_connector.find_contracts({'contract-num': self.contract})) if len(contracts) > 0: contract = contracts[0] for service in contract.find_services({}): logger.info(service.service_data['service-id'], ' - ', service.service_data['domain'])
def get_or_create_contract(personal_data, registrar): """ Get existing or create new contract in the registrar system. If there is no known contracts, then it will be created from the personal data. :param personal_data: Personal data. :param registrar: Name of the registrar. :return: RegistrarContract object. """ assert personal_data assert registrar logger.info("Getting contract for user %s from registrar %s." % (personal_data.account.id, registrar)) registrar_config = DomainRegistrarConfig(registrar) reg_connector = registrar_config.get_connector() serializer_factory = registrar_config.get_serializer_factory() created = False registrar_contracts = RegistrarContract.objects.filter(personal_data=personal_data) if len(registrar_contracts) <= 0: logger.info("Linked contracts not found. Trying to find at registrar.") native_contracts = list(reg_connector.find_contracts({"e-mail": personal_data.email})) if len(native_contracts) <= 0: logger.info("Exporting contract...") personal_data_serializer = serializer_factory.get_serializer_by_data_type(personal_data.type) native_contract = reg_connector.create_contract(personal_data_serializer.serialize(personal_data)) logger.info("Exported as %s. Trying to link local." % native_contract) created = True else: native_contract = native_contracts[0] logger.info("Used existing contract %s. Trying to link local." % native_contract) registrar_contract = RegistrarContract.link_native_contract(native_contract, personal_data) return registrar_contract, created return registrar_contracts[0], created
def _handle_order(self, *args, **options): """ Handle operations with orders. :param args: :param options: :return: """ registrar_config = DomainRegistrarConfig(self.registrar_name) reg_connector = registrar_config.get_connector() if options['list']: assert self.contract, _("Specify profile or existing linked contract.") contracts = list(reg_connector.find_contracts({'contract-num': self.contract})) if len(contracts) > 0: contract = contracts[0] query = {} if 'state' in options: query['state'] = options['state'] for order in contract.find_orders(query): logger.info(order.order_data['order_id'], ' - ', order.order_data['order_items'])
def link_native_contract(native_contract, personal_data): """ Link remote registrar contract to the local personal data. :param native_contract: Contract at the registrar :param personal_data: User personal data. :return: RegistrarContract object. """ assert native_contract assert personal_data assert isinstance(native_contract, Contract) if not RegistrarContract.objects.filter( number=native_contract.number, registrar=native_contract.registrar.NAME ).exists(): logger.info("Linking contract %s to personal data %s." % (native_contract, personal_data.id)) RegistrarContract.objects.update_or_create( registrar=native_contract.registrar.NAME, number=native_contract.number, defaults=dict(personal_data=personal_data), ) return RegistrarContract.objects.filter( number=native_contract.number, registrar=native_contract.registrar.NAME )[0]
def _parse_globals(self, **options): self.registrar_name = options['registrar'] self.user_id = int(options['user_id']) if options['user_id'] else None self.contract = options['contract'] self.profile_id = int(options['profile_id']) if options['profile_id'] else None if self.profile_id and not self.contract: logger.info("Searching for the profile %s" % self.profile_id) local_contracts = RegistrarContract.objects.filter(personal_data=self.profile_id) if len(local_contracts) > 0: self.contract = local_contracts[0].number else: logger.error(_("There is no linked contracts to profile %s" % self.profile_id)) elif not self.profile_id and self.contract: local_contracts = RegistrarContract.objects.filter(registrar=self.registrar_name, number=self.contract) if len(local_contracts) > 0: self.profile_id = local_contracts[0].personal_data.id logger.info("Registered globals:") logger.info("registrar_name = %s" % self.registrar_name) logger.info("user_id = %s" % self.user_id) logger.info("contract = %s" % self.contract) logger.info("profile_id = %s" % self.profile_id)
def _handle_contract(self, *args, **options): """ Handle operations with contracts. :param args: :param options: :return: """ registrar_config = DomainRegistrarConfig(self.registrar_name) reg_connector = registrar_config.get_connector() serializer_factory = registrar_config.get_serializer_factory() if options['search_by_domain']: domain_name = options['search_by_domain'] logger.info("Searching contract by domain %s" % domain_name) contracts = list(reg_connector.find_contracts({'domain': domain_name})) if len(contracts) <= 0: logger.warning(_("Contract is not found by %s in %s." % (domain_name, self.registrar_name))) else: for contract in contracts: logger.info(" %s" % contract.number) elif options['search_by_email']: email = options['search_by_email'] logger.info("Searching contract by email %s" % email) contracts = list(reg_connector.find_contracts({'e-mail': email})) if len(contracts) <= 0: logger.warning(_("Contract is not found by %s in %s." % (email, self.registrar_name))) else: for contract in contracts: logger.info(" %s" % contract.number) elif options['unlink']: assert self.contract, _("Specify existing contract.") linked_contracts = RegistrarContract.objects.filter(registrar=self.registrar_name, number=self.contract) for linked_contract in linked_contracts: linked_contract.delete() logger.info(" unlinked %s" % linked_contract) elif options['link']: assert self.contract, _("Specify existing contract.") linked_contracts = RegistrarContract.objects.filter(registrar=self.registrar_name, number=self.contract) if len(linked_contracts) > 0: logger.info("Contract %s already linked to profile %s." % (self.contract, linked_contracts[0].id)) else: personal_data = PersonalData.objects.get(pk=self.profile_id) RegistrarContract.objects.update_or_create( registrar=self.registrar_name, number=self.contract, defaults=dict( personal_data=personal_data ) ) logger.info("Contract %s linked to profile id %s of account %s" % (self.contract, personal_data.id, personal_data.account.id)) elif options['list']: assert self.user_id, _("Specify user id.") for personal_data in PersonalData.objects.filter(account=self.user_id): logger.info("%s - %s - %s - %s" % ( personal_data.id, personal_data.type, 'default' if personal_data.default else '', 'verified' if personal_data.verified else 'not verified')) logger.info(unicode(personal_data.extended)) print_none = True for local_contract in RegistrarContract.objects.filter(personal_data=personal_data): logger.info(" %s (%s)" % (local_contract.number, local_contract.registrar)) print_none = False if print_none: logger.info(_(" there is no linked registrar contracts.")) elif options['export']: assert self.profile_id, _("Specify profile or contract.") personal_data = PersonalData.objects.get(pk=self.profile_id) known_contracts = RegistrarContract.objects.filter(personal_data=personal_data) if len(known_contracts) <= 0: personal_data_serializer = serializer_factory.get_serializer_by_data_type(personal_data.type) contract = reg_connector.create_contract(personal_data_serializer.serialize(personal_data)) # track created contracts contract, created = RegistrarContract.objects.update_or_create( registrar=self.registrar_name, number=contract.number, defaults=dict( personal_data=personal_data ) ) if created: logger.info("Created contract: %s" % contract.number) else: logger.info("Updated contract: %s" % contract.number) else: for known_contract in known_contracts: logger.info("Existing contract: %s" % known_contract.number) elif options['delete']: assert self.contract, _("Specify profile or existing linked contract.") try: for contract in reg_connector.find_contracts({'contract-num': self.contract}): logger.info("Remove contract %s from %s" % (self.contract, self.registrar_name)) contract.delete() local_contracts_count = RegistrarContract.objects.filter(number=self.contract).count() if local_contracts_count > 0: RegistrarContract.objects.filter(number=self.contract).delete() logger.info("Local contracts removed: %s" % local_contracts_count) except Exception, ex: raise ex