def validate(self, attrs): validated_data = super().validate(attrs=attrs) domain_settings = DomainsSettings.for_client( client=self.context['request'].user.clients.first()) # validate domain name if attrs['operation'] == 'register': available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_registration( domain_name=attrs['name'], domains_settings=domain_settings, skip_whois_check=True, ) if not available: raise ValidationError({'non_field_errors': error}) else: validated_data['name'] = adjusted_name if attrs['operation'] == 'transfer': available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_transfer( domain_name=attrs['non_field_errors'], domains_settings=domain_settings, skip_whois_check=True, ) if not available: raise ValidationError({'name': error}) else: validated_data['name'] = adjusted_name return validated_data
def transfer_domain(self, request: Request) -> Response: serializer = TransferDomainSerializer(data=request.data['domain']) if serializer.is_valid(raise_exception=True): client = Client.objects.get(id=serializer.validated_data['client']) domains_settings = DomainsSettings.for_client(client=client) domain_name = serializer.validated_data['name'] available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_transfer( domain_name=domain_name, domains_settings=domains_settings, skip_whois_check=True, ) if not available: Response(data={'detail': error}, status=400) else: domain_name = adjusted_name tld = DomainUtils.get_tld(domain_name=domain_name) with transaction.atomic(): user = client.users.first() if not user: user = request.user order = Order.objects.create(user=user, client=client, currency=client.currency, client_notes='', status=OrderStatus.pending) cycle = tld.register_product.cycles.filter( currency=client.currency, cycle_multiplier=serializer.validated_data['years'] + 1, ).first() plugin_data = dict(serializer.validated_data) plugin_data['operation'] = 'transfer' plugin_data['epp'] = serializer.validated_data['epp'] order_item = order.items.create( item_type=OrderItemTypes.service, product=tld.transfer_product, cycle=cycle, fixed_price=cycle.fixed_price, cycle_display=cycle.display_name, plugin_data=plugin_data, name=domain_name, description=_('Domain transfer'), ) if not validate_services_limit(): raise APIException( _('License service limit reached. Please check your license' ), ) create_service_for_item(item=order_item) return Response(data={ 'order_id': order.id, })
def is_available_for_transfer(self, request: Request) -> Response: client_id = request.query_params.get('client_id', None) client = Client.objects.get(id=client_id) domains_settings = DomainsSettings.for_client(client=client) domain_name = request.query_params.get('domain_name', None) available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_transfer( domain_name=domain_name, domains_settings=domains_settings, skip_whois_check=True, ) if not available: return Response(data={'error': error, 'available': False}) else: domain_name = adjusted_name tld = DomainUtils.get_tld(domain_name) if not tld: return Response( data={ 'error': _('Domains {} are not available for transfer'.format( DomainUtils.get_tld_name(domain_name))), 'available': False }) if not tld.transfer_product: return Response( data={ 'error': _('No price defined for {} domains'.format( tld.name)), 'available': False }) price_cycles = tld.get_prices_for_type_and_currency( price_type=PriceType.transfer, currency=client.currency) return Response( data={ 'available': True, 'adjusted_name': adjusted_name, 'prices': PriceCyclesSerializer().to_representation( instance=price_cycles), 'config': DomainsSettingsSerializer().to_representation( instance=domains_settings), 'tld': TLDSerializer().to_representation(instance=tld), })
def validate(self, attrs): validated_data = super().validate(attrs=attrs) client = self.context['request'].user.clients.first() domain_settings = DomainsSettings.for_client(client=client) # validate domain name if attrs['operation'] == 'register': available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_registration( domain_name=attrs['name'], domains_settings=domain_settings, ) if not available: raise ValidationError({'non_field_errors': error}) else: validated_data['name'] = adjusted_name contact_id = attrs.get('contact_id', 0) if contact_id is None: contact_id = 0 if contact_id == 0: missing_fields, missing_fields_labels = CustomFieldsValidator.client_has_missing_fields_for_domain( client_id=client.id, domain_name=attrs['name'], ) else: missing_fields, missing_fields_labels = CustomFieldsValidator.contact_has_missing_fields_for_domain( contact_id=contact_id, domain_name=attrs['name'], ) if missing_fields: raise ValidationError({ 'non_field_errors': _('Missing custom fields: {}. Edit client/contact.').format( ','.join(missing_fields_labels)) }) if attrs['operation'] == 'transfer': available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_transfer( domain_name=attrs['non_field_errors'], domains_settings=domain_settings, ) if not available: raise ValidationError({'name': error}) else: validated_data['name'] = adjusted_name return validated_data
def transfer_domain(self, request: Request) -> Response: serializer = TransferDomainSerializer(data=request.data['domain']) if serializer.is_valid(raise_exception=True): user = request.user client = user.clients.first() domains_settings = DomainsSettings.for_client(client=client) domain_name = serializer.validated_data['name'] available, error, adjusted_name = DomainUtils.check_if_domains_is_available_for_transfer( domain_name=domain_name, domains_settings=domains_settings) if not available: Response(data={'detail': error}, status=400) else: domain_name = adjusted_name tld = DomainUtils.get_tld(domain_name=domain_name) cart = cart_from_request(request=self.request, create=True) cycle = tld.register_product.cycles.filter( currency=client.currency, cycle_multiplier=serializer.validated_data['years'] + 1, ).first() plugin_data = dict(serializer.validated_data) plugin_data['operation'] = 'transfer' plugin_data['epp'] = serializer.validated_data['epp'] cart.items.create( item_type=OrderItemTypes.service, product=tld.transfer_product, cycle=cycle, fixed_price=cycle.fixed_price, cycle_display=cycle.display_name, plugin_data=plugin_data, name=domain_name, description=_('Domain transfer'), ) return Response()