def save_whois_data(self, request: Request, pk) -> Response: del pk # unused domain = self.get_object() whois_fields = [] connector = registrar_connector_manager.get_connector_instance( connector_name=domain.last_registrar.connector.name, ) if domain.last_registrar: whois_fields = connector.get_whois_data(domain=domain) fields_dict = {field.name: field for field in whois_fields} whois_data = request.data.get('whois_data', None) if whois_data and 'custom_fields' in whois_data: for field in whois_data['custom_fields']: name = field['name'] value = field['value'] if name in fields_dict: fields_dict[name].value = value success, message = connector.set_whois_data(domain=domain, whois_data=whois_fields) if success: return Response(status=HTTP_200_OK) else: return Response( data={ 'details': message, 'action_status': False, }, status=HTTP_422_UNPROCESSABLE_ENTITY, )
def get_info(self, request: Request, pk) -> Response: del pk # unused domain = self.get_object() registrar_id = request.query_params.get('registrar_id', None) registrar = Registrar.objects.filter( id=registrar_id).first() if registrar_id else None if registrar: registrar_connector = registrar_connector_manager.get_connector_instance( registrar.connector.name) actions = registrar_connector.get_domain_actions(domain=domain) if not actions: actions = [] return Response(data={ 'actions': [{ 'name': action_name, 'display_name': DomainActions.domain_actions_map[action_name], } for action_name in actions], 'custom_fields': { 'cnp': { 'label': 'CNP', 'required': True, 'type': 'text' } } }, ) else: return Response( data={'details': _('Registrar not found')}, status=HTTP_422_UNPROCESSABLE_ENTITY, )
def execute_action(self, request: Request, pk) -> Response: del pk # unused domain_action = request.data.get('domain_action', None) domain = self.get_object() registrar_id = request.data.get('registrar_id', None) registrar = Registrar.objects.filter( id=registrar_id).first() if registrar_id else None if not registrar: registrar = domain.last_registrar if domain_action and registrar: registrar_connector = registrar_connector_manager.get_connector_instance( registrar.connector.name) actions = registrar_connector.get_domain_actions(domain=domain) if domain_action in actions: action_succeeded, message = registrar_connector.execute_domain_action( domain=domain, action=domain_action, ) if action_succeeded: domain.last_registrar = registrar domain.save() return Response(data={ 'details': _('Action completed successfully'), 'action_status': action_succeeded, 'action_status_message': message, }, ) else: return Response( data={ 'details': _('Failed to execute action for domain'), 'action_status': action_succeeded, 'action_status_message': message, }, status=HTTP_422_UNPROCESSABLE_ENTITY, ) else: return Response( data={ 'details': _('Invalid action for domain'), 'action_status': False, }, status=HTTP_422_UNPROCESSABLE_ENTITY, ) else: return Response( data={ 'details': _('Invalid action or registrar not found'), 'action_status': False, }, status=HTTP_422_UNPROCESSABLE_ENTITY, )
def update_prices(self, request): connector_name = request.data.get('connector') tld_name = request.data.get('tld_name', None) if connector_name: connector_info = registrar_connector_manager.connectors_info.get( 'connector_name', None) if not connector_info: raise ValidationError( detail=_('Connector {} not found').format(connector_name)) connector = registrar_connector_manager.get_connector_instance( connector_name=connector_name) connector.update_prices(tld_name=tld_name) else: for connector_name in registrar_connector_manager.connectors_info: connector = registrar_connector_manager.get_connector_instance( connector_name=connector_name) connector.update_prices(tld_name=tld_name) return Response({'detail': _('Prices updated')})
def get_whois_fields(self, request: Request, pk) -> Response: del request, pk # unused domain = self.get_object() whois_fields = [] if domain.status != DomainStatus.active: raise ApiDomainExpectationFailed( detail=_('Domain status should be {}').format( DomainStatus.domain_status_map[DomainStatus.active])) if domain.last_registrar: whois_fields = registrar_connector_manager.get_connector_instance( connector_name=domain.last_registrar.connector.name, ).get_whois_data(domain=domain) if len(whois_fields) == 0: raise ApiDomainExpectationFailed( detail=_('Failed to retrieve whois data for domain')) whois_fields_definitions = OrderedDict() whois_data = {} whois_fields_values = [] for field in whois_fields: whois_fields_definitions[field.name] = { 'required': field.required, 'optional': not field.required, 'label': field.label if field.label else field.name, 'value': field.value, 'type': 'text', 'category': '', } whois_fields_values.append({ 'name': field.name, 'label': field.label, 'value': field.value, }) whois_data[field.name] = field.value return Response(data={ 'whois_fields': whois_fields_definitions, 'whois_data': { 'custom_fields': whois_fields_values }, }, status=HTTP_200_OK)
def save_nameservers(self, request: Request, pk) -> Response: del pk # unused domain = self.get_object() nameservers_data = SaveNameserversSerializer(data=request.data) if nameservers_data.is_valid(raise_exception=True): with transaction.atomic(): domain.nameservers.clear() for nameserver in [ nameservers_data.data.get('nameserver1', None), nameservers_data.data.get('nameserver2', None), nameservers_data.data.get('nameserver3', None), nameservers_data.data.get('nameserver4', None), ]: if nameserver: defaults = {'host_name': nameserver} db_nameserver, created = Nameserver.objects.get_or_create( **defaults, defaults=defaults) domain.nameservers.add(db_nameserver) domain.save() if domain.last_registrar: action_succeeded, message = registrar_connector_manager.get_connector_instance( connector_name=domain.last_registrar.connector.name, ).execute_domain_action( domain=domain, action=DomainActions.update_nameservers) if action_succeeded: return Response(data={'details': message}, status=HTTP_200_OK) else: raise APIException(detail=message) else: raise APIException(detail=_( 'This domain has no registrar associated. Please contact support.' )) return Response( data={'details': _('Nameservers updated successfully')}, status=HTTP_200_OK)