Python django.core.validators.URLValidator() Examples
The following are 30
code examples of django.core.validators.URLValidator().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.core.validators
, or try the search function
.
Example #1
Source File: forms.py From eventoL with GNU General Public License v3.0 | 7 votes |
def clean(self): cleaned_data = super().clean() data_type = cleaned_data.get("type") value = cleaned_data.get("url") if data_type: if data_type.validate == '1': try: validator = URLValidator() validator(value) except ValidationError: self.add_error('url', 'Enter valid URL') elif data_type.validate == '2': try: validate_email(value) except ValidationError: self.add_error('url', 'Enter valid Email') else: # data_type none self.add_error('type', _('This field is required')) return cleaned_data
Example #2
Source File: goals_populator.py From goals.zone with GNU General Public License v3.0 | 6 votes |
def _extract_urls_from_comment(author, body, videogoal): for line in body.splitlines(): urls = re.findall( r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line) if len(urls) > 0: for url in urls: val = URLValidator() try: val(url) text = line.replace(url, '') if ':' in text: text = text.split(':', 1)[0] _insert_or_update_mirror(videogoal, text, url, author) except ValidationError: pass
Example #3
Source File: rest.py From memex-explorer with BSD 2-Clause "Simplified" License | 6 votes |
def validate_seeds(self, value): try: seeds = json.loads(value) except ValueError: raise serializers.ValidationError("Seeds must be a JSON encoded string.") if type(seeds) != list: raise serializers.ValidationError("Seeds must be an array of URLs.") validator = URLValidator() errors = [] for index, x in enumerate(seeds): try: validator(x) except ValidationError: # Add index to make it easier for CodeMirror to select the right # line. errors.append({index: x}) if errors: errors.insert(0, "The seeds list contains invalid urls.") errors.append({"list": "\n".join(seeds)}) raise serializers.ValidationError(errors) return value
Example #4
Source File: base.py From django-ca with GNU General Public License v3.0 | 5 votes |
def __call__(self, parser, namespace, value, option_string=None): validator = URLValidator() try: validator(value) except Exception: parser.error('%s: Not a valid URL.' % value) if getattr(namespace, self.dest) is None: setattr(namespace, self.dest, []) getattr(namespace, self.dest).append(value)
Example #5
Source File: utils.py From personfinder with Apache License 2.0 | 5 votes |
def sanitize_urls(record): """Clean up URLs to protect against XSS. We check URLs submitted through Person Finder, but bad data might come in through the API. """ url_validator = URLValidator(schemes=['http', 'https']) # Single-line URLs. for field in ['photo_url', 'source_url']: url = getattr(record, field, None) if not url: continue try: url_validator(url) except ValidationError: setattr(record, field, None) # Multi-line URLs. for field in ['profile_urls']: urls = (getattr(record, field, None) or '').splitlines() sanitized_urls = [] for url in urls: if url: try: url_validator(url) sanitized_urls.append(url) except ValidationError: logging.warning( 'Unsanitary URL in database on %s' % record.record_id) if len(urls) != len(sanitized_urls): setattr(record, field, '\n'.join(sanitized_urls))
Example #6
Source File: add_institution.py From django-uniauth with GNU Lesser General Public License v3.0 | 5 votes |
def handle(self, *args, **options): slug = slugify(options['name']) cas_server_url = options['cas_server_url'] if (not options['update_existing'] and Institution.objects.filter(slug=slug).exists()): raise CommandError("An institution with slug '" + slug + "' already exists.") try: validator = URLValidator() validator(options['cas_server_url']) except ValidationError: raise CommandError("Provided CAS server URL '" + cas_server_url + "' is malformed.") institution, created = Institution.objects.get_or_create( name=options['name'], slug=slug, defaults={'cas_server_url': cas_server_url} ) if created: self.stdout.write("Created institution '%s'.\n" % str(institution)) elif institution.cas_server_url != cas_server_url: # If institution already exists but with a different URL, # update it. institution.cas_server_url = cas_server_url institution.save() self.stdout.write("Updated institution '%s'.\n" % str(institution))
Example #7
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def __init__(self, schemes=('http', 'https'), **kwargs): if 'validators' not in kwargs: kwargs['validators'] = (validators.URLValidator(schemes=schemes),) super(URLField, self).__init__(**kwargs)
Example #8
Source File: application.py From omniport-backend with GNU General Public License v3.0 | 5 votes |
def validate_redirect_uris(self, value): """ Validate the value submitted for the field 'redirect_uris' :param value: the value submitted by the user :return: the value if validation passes :raise: ValidationError, if validation fails """ validator = URLValidator() redirect_urls = value.split(' ') for url in redirect_urls: validator(url) return value
Example #9
Source File: views.py From TWLight with MIT License | 5 votes |
def get(request, url=None, token=None): username = request.user.editor.wp_username groups = [] if not username: # Translators: When a user is being authenticated to access proxied resources, and the request's missing the editor's username, this error text is displayed. raise SuspiciousOperation(_("Missing Editor username.")) if request.user.editor.wp_bundle_authorized: groups.append("BUNDLE") for authorization in Authorization.objects.filter(user=request.user): if ( authorization.is_valid and authorization.get_authorization_method() == Partner.PROXY ): group = "P" + repr(authorization.partners.get().pk) if authorization.stream: group += "S" + repr(authorization.stream_id) groups.append(group) logger.info("{group}.".format(group=group)) if url: try: validate = URLValidator(schemes=("http", "https")) validate(url) except ValidationError: # Translators: Error text displayed to users when the target URL to access proxied publisher resources is invalid. raise SuspiciousOperation(_("Invalid EZProxy target URL.")) elif token: url = token else: # Translators: Error text displayed to users when the target URL to access proxied publisher resources is missing. raise SuspiciousOperation(_("Missing EZProxy target URL.")) return HttpResponseRedirect(EZProxyTicket(username, groups).url(url))
Example #10
Source File: models.py From hypha with BSD 3-Clause "New" or "Revised" License | 5 votes |
def clean(self): if self.service == 'twitter' and self.value.startswith('@'): self.username = self.username[1:] if self.service == 'website': validate = URLValidator() try: validate(self.value) except ValidationError as e: raise ValidationError({'value': e})
Example #11
Source File: core.py From doork with MIT License | 5 votes |
def is_valid_url(url): validate = URLValidator() try: validate(url) return True except ValidationError: return False
Example #12
Source File: answer_validation.py From govready-q with GNU General Public License v3.0 | 5 votes |
def validate_url(question, value): # Run the same checks as text (data type is str, stripped, and is not empty). value = validator.validate_text(question, value) # Then validate and normalize the URL. from django.core.validators import URLValidator urlvalidator = URLValidator() try: urlvalidator(value) except: raise ValueError("That is not a valid web address.") return value
Example #13
Source File: goals_populator.py From goals.zone with GNU General Public License v3.0 | 5 votes |
def _extract_links_from_comment(author, links, videogoal): for link in links: val = URLValidator() try: val(link.get('href')) text = link.text if 'http' in text and link.tail is not None and len(link.tail) > 0: text = link.tail _insert_or_update_mirror(videogoal, text, link.get('href'), author) except ValidationError: pass
Example #14
Source File: validators.py From Try-Django-1.10 with MIT License | 5 votes |
def validate_url(value): url_validator = URLValidator() reg_val = value if "http" in reg_val: new_value = reg_val else: new_value = 'http://' + value try: url_validator(new_value) except: raise ValidationError("Invalid URL for this field") return new_value
Example #15
Source File: forms.py From Bitpoll with GNU General Public License v3.0 | 5 votes |
def __init__(self, **kwargs): max_lengh = kwargs.pop('max_length') # TODO use this in validation of the whole field required = kwargs.pop('required') # TODO use this in validation of the whole field fields = ( URLField(validators=[URLValidator(schemes=['http', 'https'])], help_text="URL", required=True), CharField(required=False, help_text="User"), CharField(required=False, help_text="Password", widget=PasswordInput), ) super().__init__( # error_messages=error_messages, fields=fields, require_all_fields=False, **kwargs )
Example #16
Source File: url_validator.py From coursys with GNU General Public License v3.0 | 5 votes |
def __call__(self, value): try: super(QuickURLValidator, self).__call__(value) except ValidationError as e: # Trivial case failed. Try for possible IDN domain if value: value = smart_text(value) scheme, netloc, path, query, fragment = urllib.parse.urlsplit(value) try: netloc = netloc.encode('idna') # IDN -> ACE except UnicodeError: # invalid domain part raise e url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) super(URLValidator, self).__call__(url) else: raise else: url = value if True: # self.verify_exists no longer exists, but we're doing it anyway. import urllib.request, urllib.error, urllib.parse headers = { "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Language": "en-us,en;q=0.5", "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Connection": "close", "User-Agent": 'CourSys', } try: req = urllib.request.Request(url, None, headers) u = urllib.request.urlopen(req, timeout=2) except ValueError: raise ValidationError('Enter a valid URL.', code='invalid') except: # urllib2.URLError, httplib.InvalidURL, etc. raise ValidationError('This URL appears to be a broken link.', code='invalid_link')
Example #17
Source File: utils.py From django-ca with GNU General Public License v3.0 | 5 votes |
def multiline_url_validator(value): """Validate that a TextField contains one valid URL per line. .. seealso:: https://docs.djangoproject.com/en/1.9/ref/validators/ """ validator = URLValidator() for line in value.splitlines(): validator(line)
Example #18
Source File: views.py From coursys with GNU General Public License v3.0 | 5 votes |
def _delete_pagefile(request, course_slug, page_label, kind): """ Delete page/file """ with django.db.transaction.atomic(): offering = get_object_or_404(CourseOffering, slug=course_slug) page = get_object_or_404(Page, offering=offering, label=page_label) version = page.current_version() member = _check_allowed(request, offering, page.can_write, page.editdate()) if not member: return ForbiddenResponse(request, 'Not allowed to edit this '+kind+'.') can_create = member.role in MEMBER_ROLES[offering.page_creators()] if not can_create: return ForbiddenResponse(request, 'Not allowed to delete pages in for this offering (must have page-creator permission).') from django.core.validators import URLValidator from django.core.exceptions import ValidationError val = URLValidator() redirect = request.POST.get('redirect', 'Index') # Technically, the empty string is a valid value for the redirect because the field allows blanks. # We want to avoid that when deleting a page so we need an extra check here. if not redirect: redirect = 'Index' url = request.build_absolute_uri(urljoin(page.get_absolute_url(), redirect)) try: val(url) except ValidationError: messages.error(request, "Bad redirect URL entered. Not deleted.") return HttpResponseRedirect(reverse('offering:pages:edit_page', kwargs={'course_slug': course_slug, 'page_label': page.label})) redir_version = PageVersion(page=page, title=version.title, redirect=redirect, editor=member, comment='automatically generated on deletion') redir_version.set_redirect_reason('delete') redir_version.save() messages.success(request, "Page deleted and will redirect to this location.") return HttpResponseRedirect(urljoin(page.get_absolute_url(), redirect))
Example #19
Source File: forms.py From lmgtdfy with MIT License | 5 votes |
def clean(self): cleaned_data = super(MainForm, self).clean() domain_string = cleaned_data.get('domain', '') if not (domain_string.startswith('http://') or domain_string.startswith('https://')): domain_string = 'http://%s' % domain_string validator = validators.URLValidator() try: validator(domain_string) except: raise forms.ValidationError('Please enter a valid URL.') cleaned_data['domain_base'] = urlparse(domain_string).netloc return cleaned_data
Example #20
Source File: test_form_enter_data.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def test_enketo_remote_server(self): if not self._running_enketo(): raise SkipTest with HTTMock(enketo_mock): server_url = 'https://testserver.com/bob' form_id = "test_%s" % re.sub(re.compile("\."), "_", str(time())) url = enketo_url(server_url, form_id) self.assertIsInstance(url, basestring) self.assertIsNone(URLValidator()(url))
Example #21
Source File: version.py From DCRM with GNU Affero General Public License v3.0 | 5 votes |
def validate_bugs(value): """ Inherits from a Built-in URLValidator """ return bugs_validator(value)
Example #22
Source File: views.py From server with MIT License | 5 votes |
def add_city_image(request): """ Add image URL to a city :param request: :return: 400 Invalid City ID or Incorrect Image URL :return: 200 successful """ city_id = request.POST.get('city_id', None) image_url = request.POST.get('image_url', None) url_validator = URLValidator() if not city_id or not image_url: # incorrect request received error_message = "Missing parameters in request. Send city_id, image_url" return Response(error_message, status=status.HTTP_400_BAD_REQUEST) try: url_validator(image_url) city = City.objects.get(pk=city_id) city_image = CityImage(city=city, image_url=image_url) city_image.save() except ValidationError as e: # e is a list of error messages error_message = "\n".join(e) return Response(error_message, status=status.HTTP_400_BAD_REQUEST) except Exception as e: error_message = str(e) return Response(error_message, status=status.HTTP_400_BAD_REQUEST) success_message = "Successfully added new city image." return Response(success_message, status=status.HTTP_201_CREATED)
Example #23
Source File: validator.py From zulip with Apache License 2.0 | 5 votes |
def check_url(var_name: str, val: object) -> str: # First, ensure val is a string s = check_string(var_name, val) # Now, validate as URL validate = URLValidator() try: validate(s) return s except ValidationError: raise ValidationError(_('{var_name} is not a URL').format(var_name=var_name))
Example #24
Source File: views.py From zulip with Apache License 2.0 | 5 votes |
def register_remote_server( request: HttpRequest, zulip_org_id: str=REQ(str_validator=check_string_fixed_length(RemoteZulipServer.UUID_LENGTH)), zulip_org_key: str=REQ(str_validator=check_string_fixed_length(RemoteZulipServer.API_KEY_LENGTH)), hostname: str=REQ(str_validator=check_capped_string(RemoteZulipServer.HOSTNAME_MAX_LENGTH)), contact_email: str=REQ(str_validator=check_string), new_org_key: Optional[str]=REQ(str_validator=check_string_fixed_length( RemoteZulipServer.API_KEY_LENGTH), default=None), ) -> HttpResponse: # REQ validated the the field lengths, but we still need to # validate the format of these fields. try: # TODO: Ideally we'd not abuse the URL validator this way url_validator = URLValidator() url_validator('http://' + hostname) except ValidationError: raise JsonableError(_('{} is not a valid hostname').format(hostname)) try: validate_email(contact_email) except ValidationError as e: raise JsonableError(e.message) remote_server, created = RemoteZulipServer.objects.get_or_create( uuid=zulip_org_id, defaults={'hostname': hostname, 'contact_email': contact_email, 'api_key': zulip_org_key}) if not created: if remote_server.api_key != zulip_org_key: raise InvalidZulipServerKeyError(zulip_org_id) else: remote_server.hostname = hostname remote_server.contact_email = contact_email if new_org_key is not None: remote_server.api_key = new_org_key remote_server.save() return json_success({'created': created})
Example #25
Source File: staff_views.py From palanaeum with GNU Affero General Public License v3.0 | 5 votes |
def _save_entry_url_sources(request, entry_version: EntryVersion): """ Save updated url sources. """ source_re = r'^url-source-(\d+)-(name|url)$' urls_data = defaultdict(dict) # {id -> {url: "", name: ""} validator = URLValidator(['http', 'https']) for key in filter(lambda k: re.match(source_re, k), request.POST): match = re.match(source_re, key) urls_data[match.group(1)][match.group(2)] = request.POST[key] entry_version.url_sources.clear() for url_data in urls_data.values(): name = url_data['name'] url = url_data['url'] try: validator(url) except ValidationError: continue if not url: continue try: url_obj = URLSource.objects.get(url=url) except URLSource.DoesNotExist: url_obj = URLSource.objects.create(url=url, text=name) else: url_obj.text = name url_obj.save() url_obj.entry_versions.add(entry_version) url_obj.save() URLSource.remove_unused() return
Example #26
Source File: validator.py From rssant with BSD 3-Clause "New" or "Revised" License | 5 votes |
def url_validator(compiler, scheme='http https', default_schema=None, relaxed=False): """ Args: default_schema: 接受没有scheme的url并尝试修正 relaxed: accept not strict url """ if relaxed: return Compiler().compile(T.url.scheme(scheme)) schemes = set(scheme.replace(',', ' ').split(' ')) if default_schema and default_schema not in schemes: raise SchemaError('invalid default_schema {}'.format(default_schema)) _django_validate_url = URLValidator(schemes=schemes) def validate(value): if default_schema: value = coerce_url(value, default_schema=default_schema) try: _django_validate_url(value) except ValidationError: # TODO: access ValidationError.messages will cause error when # django/i18n not setup, maybe use validators package instead # raise Invalid(','.join(ex.messages).rstrip('.')) raise Invalid('invalid or incorrect url format') return value return validate
Example #27
Source File: fields.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def __init__(self, max_length=None, min_length=None, *args, **kwargs): super(URLField, self).__init__(max_length, min_length, *args, **kwargs) self.validators.append(validators.URLValidator())
Example #28
Source File: __init__.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def __init__(self, verbose_name=None, name=None, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 200) CharField.__init__(self, verbose_name, name, **kwargs) self.validators.append(validators.URLValidator())
Example #29
Source File: base.py From django-ca with GNU General Public License v3.0 | 5 votes |
def __call__(self, parser, namespace, value, option_string=None): validator = URLValidator() try: validator(value) except Exception: parser.error('%s: Not a valid URL.' % value) setattr(namespace, self.dest, value)
Example #30
Source File: admin.py From ChRIS_ultron_backEnd with MIT License | 4 votes |
def register_plugins_from_file(self, f): """ Custom method to register plugins from a text file f. The first string of each line of the file is the plugin's name or url, the second string is the version (can be empty) and the third string is the associated compute environment. Any plugin in the file must already be previously uploaded to the ChRIS store. """ summary = {'success': [], 'error': []} val = URLValidator() pl_manager = PluginManager() for line in f: try: # check file must be a text file strings = line.decode().strip().split() except UnicodeDecodeError: summary = {'success': [], 'error': []} break if len(strings) == 1: summary['error'].append({'plugin_name': strings[0], 'code': 'Missing compute resource identifier.'}) elif len(strings) > 1: try: val(strings[0]) # check whether the first string is a url except ValidationError: # register by name plg_name = strings[0] if len(strings) == 2: plg_version = None compute_resource = strings[1] else: plg_version = strings[1] compute_resource = strings[2] try: pl_manager.register_plugin(plg_name, plg_version, compute_resource) except Exception as e: summary['error'].append({'plugin_name': plg_name, 'code': str(e)}) else: summary['success'].append({'plugin_name': plg_name}) else: # register by url plg_url = strings[0] compute_resource = strings[1] try: pl_manager.register_plugin_by_url(plg_url, compute_resource) except Exception as e: summary['error'].append({'plugin_name': plg_url, 'code': str(e)}) else: summary['success'].append({'plugin_name': plg_url}) return summary