def test_urlfield_clean_required(self): f = URLField() msg = "'This field is required.'" with self.assertRaisesMessage(ValidationError, msg): f.clean(None) with self.assertRaisesMessage(ValidationError, msg): f.clean("")
def test_urlfield_clean_invalid(self): f = URLField() tests = [ "foo", "com.", ".", "http://", "http://example", "http://example.", "http://.com", "http://invalid-.com", "http://-invalid.com", "http://inv-.alid-.com", "http://inv-.-alid.com", "[a", "http://[a", # Non-string. 23, # Hangs "forever" before fixing a catastrophic backtracking, # see #11198. "http://%s" % ("X" * 60, ), # A second example, to make sure the problem is really addressed, # even on domains that don't fail the domain label length check in # the regex. "http://%s" % ("X" * 200, ), # urlsplit() raises ValueError. "////]@N.AN", # Empty hostname. "#@A.bO", ] msg = "'Enter a valid URL.'" for value in tests: with self.subTest(value=value): with self.assertRaisesMessage(ValidationError, msg): f.clean(value)
def test_urlfield_7(self): f = URLField() self.assertEqual('http://example.com', f.clean('http://example.com')) self.assertEqual('http://example.com/test', f.clean('http://example.com/test')) self.assertEqual('http://example.com?some_param=some_value', f.clean('http://example.com?some_param=some_value'))
def test_urlfield_7(self): f = URLField() self.assertEqual("http://example.com", f.clean("http://example.com")) self.assertEqual("http://example.com/test", f.clean("http://example.com/test")) self.assertEqual( "http://example.com?some_param=some_value", f.clean("http://example.com?some_param=some_value") )
def validate_url(url): url_form_field = URLField() try: url_form_field.clean(url) except ValidationError: return False return True
def test_urlfield_5(self): f = URLField(min_length=15, max_length=20) self.assertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" />') with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 12).'"): f.clean('http://f.com') self.assertEqual('http://example.com', f.clean('http://example.com')) with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 37).'"): f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
def test_urlfield_7(self): f = URLField() self.assertEqual('http://example.com', f.clean('http://example.com')) self.assertEqual('http://example.com/test', f.clean('http://example.com/test')) self.assertEqual( 'http://example.com?some_param=some_value', f.clean('http://example.com?some_param=some_value') )
def url_valid(url): is_valid = True f = URLField() try: f.clean(url) except: is_valid = False return is_valid
def test_url_regex_ticket11198(self): f = URLField() # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://%s' % ("X" * 200,)) # a second test, to make sure the problem is really addressed, even on # domains that don't fail the domain label length check in the regex with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://%s' % ("X" * 60,))
def test_urlfield_widget_max_min_length(self): f = URLField(min_length=15, max_length=20) self.assertEqual("http://example.com", f.clean("http://example.com")) self.assertWidgetRendersTo( f, '<input id="id_f" type="url" name="f" maxlength="20" ' 'minlength="15" required>', ) msg = "'Ensure this value has at least 15 characters (it has 12).'" with self.assertRaisesMessage(ValidationError, msg): f.clean("http://f.com") msg = "'Ensure this value has at most 20 characters (it has 37).'" with self.assertRaisesMessage(ValidationError, msg): f.clean("http://abcdefghijklmnopqrstuvwxyz.com")
def _extract_url(cls, text_url_field): ''' >>> url_text = 'http://www.google.com blabla' >>> FacebookAPI._extract_url(url_text) u'http://www.google.com/' >>> url_text = 'http://www.google.com/' >>> FacebookAPI._extract_url(url_text) u'http://www.google.com/' >>> url_text = 'google.com/' >>> FacebookAPI._extract_url(url_text) u'http://google.com/' >>> url_text = 'http://www.fahiolista.com/www.myspace.com/www.google.com' >>> FacebookAPI._extract_url(url_text) u'http://www.fahiolista.com/www.myspace.com/www.google.com' ''' import re text_url_field = str(text_url_field) seperation = re.compile('[ |,|;]+') parts = seperation.split(text_url_field) for part in parts: from django.forms import URLField url_check = URLField(verify_exists=False) try: clean_url = url_check.clean(part) return clean_url except ValidationError, e: continue
def inner(request): # reading data url = request.GET.get('url') or request.POST.get('url') or json.loads( request.body).get('url') if not url: # return in case no data is sent return JsonResponse({ "error": True, "value": 'blank', "msg": "No url sent" }) # verifying syntax of url try: url_field = URLField() url = url_field.clean(url) url = re.match(URL_PATH_REGEX, url).group( 2) # extracting the domain and directory name except ValidationError as e: # return if syntax is wrong print(e) return JsonResponse({ "error": True, "value": 'invalid', "msg": "Invalid syntax" }) return func(request, url)
def is_valid_url(url): url_form_field = URLField() try: url = url_form_field.clean(url) except ValidationError as e: return False return True
def get_url_informations(url): """Get informations about a url""" error_invalid = False, {"error": _(u"Invalid URL")} error_exist = False, {"error": _(u"This URL has already been submitted")} error_connect = False, {"error": _(u"Failed at opening the URL")} # Check url is valid try: url_field = URLField() clean_url = url_field.clean(url) except: return error_invalid # Check url exist if exist_url(clean_url): return error_exist # Get informations req = requests.get(clean_url) if 200 == req.status_code: final_url = req.url soup = BeautifulSoup(req.text) title = soup.title.string description = soup.findAll("meta", attrs={"name": re.compile("^description$", re.I)})[0].get("content") else: return error_connect # Check final url exist if different from clean url if final_url != clean_url: if exist_url(final_url): return error_exist return True, {"url": final_url, "title": title, "description": description}
def validate_url(url): protocols = ['http://', 'https://'] flag = 0 url_form_field = URLField() email_field = EmailField() try: email_field.clean(url) except ValidationError: if url != '': for protocol in protocols: n = len(protocol) if url[0:n] == protocol: flag = 1 break if flag == 0: flag1 = 1 for protocol in protocols: new_url = protocol + url try: new_url == url_form_field.clean(new_url) except ValidationError: flag1 = 0 else: url = new_url break if flag1 == 1: return True, url return False, url return True, url return False, url else: return False, url
def _extract_url(cls, text_url_field): ''' >>> url_text = 'http://www.google.com blabla' >>> FacebookAPI._extract_url(url_text) u'http://www.google.com/' >>> url_text = 'http://www.google.com/' >>> FacebookAPI._extract_url(url_text) u'http://www.google.com/' >>> url_text = 'google.com/' >>> FacebookAPI._extract_url(url_text) u'http://google.com/' >>> url_text = 'http://www.fahiolista.com/www.myspace.com/www.google.com' >>> FacebookAPI._extract_url(url_text) u'http://www.fahiolista.com/www.myspace.com/www.google.com' >>> url_text = u"""http://fernandaferrervazquez.blogspot.com/\r\nhttp://twitter.com/fferrervazquez\r\nhttp://comunidad.redfashion.es/profile/fernandaferrervazquez\r\nhttp://www.facebook.com/group.php?gid3D40257259997&ref3Dts\r\nhttp://fernandaferrervazquez.spaces.live.com/blog/cns!EDCBAC31EE9D9A0C!326.trak\r\nhttp://www.linkedin.com/myprofile?trk3Dhb_pro\r\nhttp://www.youtube.com/account#profile\r\nhttp://www.flickr.com/\r\n Mi galer\xeda\r\nhttp://www.flickr.com/photos/wwwfernandaferrervazquez-showroomrecoletacom/ \r\n\r\nhttp://www.facebook.com/pages/Buenos-Aires-Argentina/Fernanda-F-Showroom-Recoleta/200218353804?ref3Dts\r\nhttp://fernandaferrervazquez.wordpress.com/wp-admin/""" >>> FacebookAPI._extract_url(url_text) u'http://fernandaferrervazquez.blogspot.com/a' ''' import re text_url_field = text_url_field.encode('utf8') seperation = re.compile('[ ,;\n\r]+') parts = seperation.split(text_url_field) for part in parts: from django.forms import URLField url_check = URLField(verify_exists=False) try: clean_url = url_check.clean(part) return clean_url except ValidationError: continue
def _extract_url(cls, text_url_field): ''' >>> url_text = 'http://www.google.com blabla' >>> FacebookAPI._extract_url(url_text) u'http://www.google.com/' >>> url_text = 'http://www.google.com/' >>> FacebookAPI._extract_url(url_text) u'http://www.google.com/' >>> url_text = 'google.com/' >>> FacebookAPI._extract_url(url_text) u'http://google.com/' >>> url_text = 'http://www.fahiolista.com/www.myspace.com/www.google.com' >>> FacebookAPI._extract_url(url_text) u'http://www.fahiolista.com/www.myspace.com/www.google.com' >>> url_text = u"""http://fernandaferrervazquez.blogspot.com/\r\nhttp://twitter.com/fferrervazquez\r\nhttp://comunidad.redfashion.es/profile/fernandaferrervazquez\r\nhttp://www.facebook.com/group.php?gid3D40257259997&ref3Dts\r\nhttp://fernandaferrervazquez.spaces.live.com/blog/cns!EDCBAC31EE9D9A0C!326.trak\r\nhttp://www.linkedin.com/myprofile?trk3Dhb_pro\r\nhttp://www.youtube.com/account#profile\r\nhttp://www.flickr.com/\r\n Mi galer\xeda\r\nhttp://www.flickr.com/photos/wwwfernandaferrervazquez-showroomrecoletacom/ \r\n\r\nhttp://www.facebook.com/pages/Buenos-Aires-Argentina/Fernanda-F-Showroom-Recoleta/200218353804?ref3Dts\r\nhttp://fernandaferrervazquez.wordpress.com/wp-admin/""" >>> FacebookAPI._extract_url(url_text) u'http://fernandaferrervazquez.blogspot.com/a' ''' import re text_url_field = text_url_field.encode('utf8') seperation = re.compile('[ ,;\n\r]+') parts = seperation.split(text_url_field) for part in parts: from django.forms import URLField url_check = URLField(verify_exists=False) try: clean_url = url_check.clean(part) return clean_url except ValidationError, e: continue
def event(wf_module, **kwargs): #number of rows we want to retrieve from Enigma. If you leave this blank/let it use the default, #you get all of 0 rows, so it should have a value > 0. wf_module.set_busy(notify=False) try: num_rows = int(wf_module.get_param_string("num_rows")) except ValueError: wf_module.set_error("The number of rows specified must be an integer, but it's {}." .format(wf_module.get_param_string("num_rows"))) return None # We can get one of two _types_ of Enigma URLs here: # - any URL with the .com TLD indicates that the URL is publicly browsable, and so we'll extract # the name of the dataset, construct the URL, and make a call using our API key. # - any URL with the .io TLD indicates that the URL is probably already using the API, so we we can$ # simply send off the request to Enigma. url = wf_module.get_param_string('enigma_url') url_form_field = URLField() try: url = url_form_field.clean(url) except ValidationError: wf_module.set_error('Invalid URL entered: {}'.format((url))) return #let's break the url down to its components split_url = urlsplit(url) netloc = split_url.netloc # quick basic validation: ensure it's an Enigma URL$ if netloc.split(".")[1].lower() != 'enigma': wf_module.set_error("The URL entered, {}, is not an Enigma URL.".format(netloc)) return None # there's no point going any further for obvious reasons # quick basic validation: ensure the TLD is .com or .io. if netloc.split(".")[2].lower() not in ["com", "io"]: wf_module.set_error("The top-level domain specified has to be .com or .io, but " + " the top-level domain in the URL received is {}.".format(netloc.split(".")[2])) return None # there's no point going any further for obvious reasons # Can wrap this around a single try because only one or the other will be called. try: if netloc.endswith("io"): data = handle_dotio_url(wf_module, url, split_url, num_rows) else: # this has to be ".com" as we've already done the check above for dodgy URLs. # this returns the Pandas table. data = handle_dotcom_url(wf_module, url, split_url, num_rows) except Exception as ex: wf_module.set_error("Caught error whilst attempting to retrieve details from Enigma: {}".format(str(ex))) # If we have got this far, and not run into any issues, we should do some data versioning magic. if wf_module.status != wf_module.ERROR: wf_module.set_ready(notify=False) csv_data = data.to_csv(index=False) updated = wf_module.auto_update_data or event.get('type') == 'click' save_fetched_table_if_changed(wf_module, csv_data, auto_change_version=updated)
def test_urlfield_10(self): """URLField correctly validates IPv6 (#18779).""" f = URLField() urls = ( 'http://[12:34::3a53]/', 'http://[a34:9238::]:8080/', ) for url in urls: self.assertEqual(url, f.clean(url))
def TaskUpdateView(request, pk): if request.is_ajax(): t = Task.objects.filter(id=pk).first() if not t: return Http404("No such task") status = request.POST.get("status", None) description = request.POST.get("description", None) url = request.POST.get("url", None) assigned_to = request.POST.get("assigned_to", None) if status: if not isinstance(status, str): return HttpResponseBadRequest("Status must be a string") # TODO: add check for choices t.status = status elif description: if not isinstance(description, str): return HttpResponseBadRequest("Description must be a string") t.description = description elif url: if not isinstance(url, str): return HttpResponseBadRequest("URL must be a string") else: try: f = URLField() f.clean(url) except ValidationError: return HttpResponseBadRequest("Not a valid URL") except Exception: return HttpResponseBadRequest("Unknown error with URL") t.url = url elif assigned_to: if not isinstance(assigned_to, str): return HttpResponseBadRequest("Assigned_to must be a string") t.assigned_to = assigned_to else: return HttpResponseBadRequest("No valid field to edit") t.save() return JsonResponse({"result": True, "id": t.id, "title": t.title}) else: return HttpResponseBadRequest("Must be ajax")
def parseUrl(instance): """ Takes instance of UrlMapper model and creates a dict with url and minified_url keys """ return_object = {} url_field = URLField() return_object.update({"url": url_field.clean(instance.url)}) if instance.hashcode: return_object.update( {"minified_url": "{}/{}".format(DOMAIN_URL, instance.hashcode)}) return return_object
def mutate_and_get_payload(cls, root, info, **input): """Registra el organizador en la base de datos.""" organizer_data = input.get("organizer") url_cleaner = URLField(required=False) organizer = Organizer(name=organizer_data.name, phone=organizer_data.phone, website=url_cleaner.clean( organizer_data.website), email=organizer_data.email) organizer.full_clean() organizer.save() return cls(new_organizer=organizer)
def test_urlfield_clean(self): f = URLField(required=False) tests = [ ("http://localhost", "http://localhost"), ("http://example.com", "http://example.com"), ("http://example.com/test", "http://example.com/test"), ("http://example.com.", "http://example.com."), ("http://www.example.com", "http://www.example.com"), ("http://www.example.com:8000/test", "http://www.example.com:8000/test"), ( "http://example.com?some_param=some_value", "http://example.com?some_param=some_value", ), ("valid-with-hyphens.com", "http://valid-with-hyphens.com"), ("subdomain.domain.com", "http://subdomain.domain.com"), ("http://200.8.9.10", "http://200.8.9.10"), ("http://200.8.9.10:8000/test", "http://200.8.9.10:8000/test"), ("http://valid-----hyphens.com", "http://valid-----hyphens.com"), ( "http://some.idn.xyzäöüßabc.domain.com:123/blah", "http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah", ), ( "www.example.com/s/http://code.djangoproject.com/ticket/13804", "http://www.example.com/s/http://code.djangoproject.com/ticket/13804", ), # Normalization. ("http://example.com/ ", "http://example.com/"), # Valid IDN. ("http://עברית.idn.icann.org/", "http://עברית.idn.icann.org/"), ("http://sãopaulo.com/", "http://sãopaulo.com/"), ("http://sãopaulo.com.br/", "http://sãopaulo.com.br/"), ("http://пример.испытание/", "http://пример.испытание/"), ("http://مثال.إختبار/", "http://مثال.إختبار/"), ("http://例子.测试/", "http://例子.测试/"), ("http://例子.測試/", "http://例子.測試/"), ( "http://उदाहरण.परीक्षा/", "http://उदाहरण.परीक्षा/", ), ("http://例え.テスト/", "http://例え.テスト/"), ("http://مثال.آزمایشی/", "http://مثال.آزمایشی/"), ("http://실례.테스트/", "http://실례.테스트/"), ("http://العربية.idn.icann.org/", "http://العربية.idn.icann.org/"), # IPv6. ("http://[12:34::3a53]/", "http://[12:34::3a53]/"), ("http://[a34:9238::]:8080/", "http://[a34:9238::]:8080/"), ] for url, expected in tests: with self.subTest(url=url): self.assertEqual(f.clean(url), expected)
def mutate_and_get_payload(cls, root, info, **input): """Registra el lugar en la base de datos.""" venue_data = input.get("venue") url_cleaner = URLField(required=False) venue = Venue(name=venue_data.name, address=venue_data.address, city=venue_data.city, state=venue_data.state, phone=venue_data.phone, website=url_cleaner.clean(venue_data.website), latitude=venue_data.latitude, longitude=venue_data.longitude) venue.full_clean() venue.save() return cls(new_venue=venue)
def mutate_and_get_payload(cls, root, info, **input): """Actualiza un organizador en la base de datos.""" relay_id = input.get("id") model_id = from_global_id(relay_id)[1] organizer_instance = Organizer.objects.get(id=model_id) if organizer_instance: organizer_data = input.get("organizer") url_cleaner = URLField(required=False) organizer_instance.name = organizer_data.name organizer_instance.phone = organizer_data.phone organizer_instance.website \ = url_cleaner.clean(organizer_data.website) organizer_instance.email = organizer_data.email organizer_instance.full_clean() organizer_instance.save() return cls(updated_organizer=organizer_instance) raise Organizer.DoesNotExist
def sanitise_url(url): if url.strip() == "": raise ValidationError("Empty URL entered.") # verify if this is a valid GitHub url url_form_field = URLField() try: url = url_form_field.clean(url) if "github" not in url: raise ValidationError('Invalid GitHub URL entered: %s' % (url)) # - if entered url has a tailing '/', remove it if url[-1] == '/': url = url[:-1] # - strip out '.git' if it exists in the URL if url.endswith('.git'): url = url[0:-4] return url except ValidationError: raise ValidationError('Invalid GitHub URL entered: %s' % (url))
def test_urlfield_9(self): f = URLField() urls = ( 'http://עברית.idn.icann.org/', 'http://sãopaulo.com/', 'http://sãopaulo.com.br/', 'http://пример.испытание/', 'http://مثال.إختبار/', 'http://例子.测试/', 'http://例子.測試/', 'http://उदाहरण.परीक्षा/', 'http://例え.テスト/', 'http://مثال.آزمایشی/', 'http://실례.테스트/', 'http://العربية.idn.icann.org/', ) for url in urls: # Valid IDN self.assertEqual(url, f.clean(url))
def mutate_and_get_payload(cls, root, info, **input): """Actualiza un evento en la base de datos.""" # recuperar la instancia de Event y asignar los nuevos valores relay_id = input.get("id") model_id = from_global_id(relay_id)[1] event_instance = Event.objects.get(id=model_id) if event_instance: event_data = input.get("event") url_cleaner = URLField(required=False) event_instance.name = event_data.name event_instance.start_datetime = event_data.start_datetime event_instance.finish_datetime = event_data.finish_datetime event_instance.abstract = event_data.abstract event_instance.description = event_data.description event_instance.price = event_data.price event_instance.website = url_cleaner.clean(event_data.website) event_instance.limited = event_data.limited event_instance.active = event_data.active # recuperar y re-asignar el lugar de encuentro venue_id = from_global_id(event_data.venue)[1] event_instance.venue = Venue.objects.get(id=venue_id) event_instance.full_clean() # re-asignar las categorías if event_data.categories: event_instance.categories.clear() for category_id in event_data.categories: decoded_id = from_global_id(category_id)[1] category = Category.objects.get(id=decoded_id) event_instance.categories.add(category) else: raise ValueError("Events need at least one category") # re-asignar los organizadores if event_data.organizers: event_instance.organizers.clear() for organizer_id in event_data.organizers: decoded_id = from_global_id(organizer_id)[1] organizer = Organizer.objects.get(id=decoded_id) event_instance.organizers.add(organizer) else: raise ValueError("Events need at least one organizer") event_instance.save() return cls(updated_event=event_instance) raise Event.DoesNotExist
def mutate_and_get_payload(cls, root, info, **input): """Registra los enlaces relacionados en la base de datos.""" category_relay_id = input.get("id") category_model_id = from_global_id(category_relay_id)[1] category_instance = Category.objects.get(id=category_model_id) if category_instance: related_links = input.get("related_links") if related_links: url_cleaner = URLField() for related_link in related_links: link = RelatedLink(name=related_link.name, link=url_cleaner.clean( related_link.link), category=category_instance) link.full_clean() link.save() return cls(category=category_instance) else: raise IndexError("Debes enviar enlaces válidos.") raise Category.DoesNotExist
def mutate_and_get_payload(cls, root, info, **input): """Actualiza un lugar en la base de datos.""" relay_id = input.get("id") model_id = from_global_id(relay_id)[1] venue_instance = Venue.objects.get(id=model_id) if venue_instance: venue_data = input.get("venue") url_cleaner = URLField(required=False) venue_instance.name = venue_data.name venue_instance.address = venue_data.address venue_instance.city = venue_data.city venue_instance.state = venue_data.state venue_instance.phone = venue_data.phone venue_instance.website = url_cleaner.clean(venue_data.website) venue_instance.latitude = venue_data.latitude venue_instance.longitude = venue_data.longitude venue_instance.full_clean() venue_instance.save() return cls(updated_venue=venue_instance) raise Venue.DoesNotExist
def get_url_informations(url): """Get informations about a url""" error_invalid = False, {'error': _(u'Invalid URL')} error_exist = False, {'error': _(u'This URL has already been submitted')} error_connect = False, {'error': _(u'Failed at opening the URL')} # Check url is valid try: url_field = URLField() clean_url = url_field.clean(url) except: return error_invalid # Check url exist if exist_url(clean_url): return error_exist # Get informations req = requests.get(clean_url) if req.status_code == 200: final_url = req.url soup = BeautifulSoup(req.text) title = soup.title.string description = soup.findAll( 'meta', attrs={'name': re.compile("^description$", re.I)})[0].get('content') else: return error_connect # Check final url exist if different from clean url if final_url != clean_url: if exist_url(final_url): return error_exist return True, { 'url': final_url, 'title': title, 'description': description, }
def get_url_informations(url): """Get informations about a url""" error_invalid = False, {'error': _(u'Invalid URL')} error_exist = False, {'error': _(u'This URL has already been submitted')} error_connect = False, {'error': _(u'Failed at opening the URL')} # Check url is valid try: url_field = URLField() clean_url = url_field.clean(url) except: return error_invalid # Check url exist if exist_url(clean_url): return error_exist # Get informations req = requests.get(clean_url) if req.status_code == 200: final_url = req.url soup = BeautifulSoup(req.text) title = soup.title.string description = soup.findAll('meta', attrs={'name': re.compile("^description$", re.I)})[0].get('content') else: return error_connect # Check final url exist if different from clean url if final_url != clean_url: if exist_url(final_url): return error_exist return True, { 'url': final_url, 'title': title, 'description': description, }
def mutate_and_get_payload(cls, root, info, **input): """Registra el evento en la base de datos.""" # crear la instancia de Event event_data = input.get("event") url_cleaner = URLField(required=False) event = Event(name=event_data.name, start_datetime=event_data.start_datetime, finish_datetime=event_data.finish_datetime, abstract=event_data.abstract, description=event_data.description, price=event_data.price, website=url_cleaner.clean(event_data.website), limited=event_data.limited, active=event_data.active) # asignar el lugar de encuentro. venue_id = from_global_id(event_data.venue)[1] event.venue = Venue.objects.get(id=venue_id) event.full_clean() event.save() # recuperar y asignar las categorías if event_data.categories: for organizer_id in event_data.organizers: decoded_id = from_global_id(organizer_id)[1] event.organizers.add(Organizer.objects.get(id=decoded_id)) else: # deshacer la transacción event.delete() raise ValueError("Events need at least one category") # recuperar y asignar los organizadores if event_data.organizers: for category_id in event_data.categories: decoded_id = from_global_id(category_id)[1] event.categories.add(Category.objects.get(id=decoded_id)) else: # deshacer la transacción event.delete() raise ValueError("Events need at least one organizer") return cls(new_event=event)
def test_urlfield_6(self): f = URLField(required=False) self.assertEqual('http://example.com', f.clean('example.com')) self.assertEqual('', f.clean('')) self.assertEqual('https://example.com', f.clean('https://example.com'))
def test_urlfield_1(self): f = URLField() self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" required />') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean("") with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean(None) self.assertEqual("http://localhost", f.clean("http://localhost")) self.assertEqual("http://example.com", f.clean("http://example.com")) self.assertEqual("http://example.com.", f.clean("http://example.com.")) self.assertEqual("http://www.example.com", f.clean("http://www.example.com")) self.assertEqual("http://www.example.com:8000/test", f.clean("http://www.example.com:8000/test")) self.assertEqual("http://valid-with-hyphens.com", f.clean("valid-with-hyphens.com")) self.assertEqual("http://subdomain.domain.com", f.clean("subdomain.domain.com")) self.assertEqual("http://200.8.9.10", f.clean("http://200.8.9.10")) self.assertEqual("http://200.8.9.10:8000/test", f.clean("http://200.8.9.10:8000/test")) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("foo") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://example") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://example.") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("com.") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean(".") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://.com") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://invalid-.com") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://-invalid.com") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://inv-.alid-.com") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://inv-.-alid.com") self.assertEqual("http://valid-----hyphens.com", f.clean("http://valid-----hyphens.com")) self.assertEqual( "http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah", f.clean("http://some.idn.xyzäöüßabc.domain.com:123/blah"), ) self.assertEqual( "http://www.example.com/s/http://code.djangoproject.com/ticket/13804", f.clean("www.example.com/s/http://code.djangoproject.com/ticket/13804"), ) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("[a") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://[a")
def test_urlfield_2(self): f = URLField(required=False) self.assertEqual("", f.clean("")) self.assertEqual("", f.clean(None)) self.assertEqual("http://example.com", f.clean("http://example.com")) self.assertEqual("http://www.example.com", f.clean("http://www.example.com")) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("foo") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://example") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://example.") with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean("http://.com")
def test_urlfield_2(self): f = URLField(required=False) self.assertEqual('', f.clean('')) self.assertEqual('', f.clean(None)) self.assertEqual('http://example.com', f.clean('http://example.com')) self.assertEqual('http://www.example.com', f.clean('http://www.example.com')) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('foo') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://example') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://example.') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://.com')
def test_urlfield_6(self): f = URLField(required=False) self.assertEqual("http://example.com", f.clean("example.com")) self.assertEqual("", f.clean("")) self.assertEqual("https://example.com", f.clean("https://example.com"))
def test_urlfield_not_string(self): f = URLField(required=False) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean(23)
def test_urlfield_strip_on_none_value(self): f = URLField(required=False, empty_value=None) self.assertIsNone(f.clean(None))
def test_urlfield_1(self): f = URLField() self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" />') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean('') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean(None) self.assertEqual('http://localhost', f.clean('http://localhost')) self.assertEqual('http://example.com', f.clean('http://example.com')) self.assertEqual('http://example.com.', f.clean('http://example.com.')) self.assertEqual('http://www.example.com', f.clean('http://www.example.com')) self.assertEqual('http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test')) self.assertEqual('http://valid-with-hyphens.com', f.clean('valid-with-hyphens.com')) self.assertEqual('http://subdomain.domain.com', f.clean('subdomain.domain.com')) self.assertEqual('http://200.8.9.10', f.clean('http://200.8.9.10')) self.assertEqual('http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test')) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('foo') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://example') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://example.') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('com.') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('.') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://.com') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://invalid-.com') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://-invalid.com') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://inv-.alid-.com') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://inv-.-alid.com') self.assertEqual('http://valid-----hyphens.com', f.clean('http://valid-----hyphens.com')) self.assertEqual( 'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah') ) self.assertEqual( 'http://www.example.com/s/http://code.djangoproject.com/ticket/13804', f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804') ) with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('[a') with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"): f.clean('http://[a')
def test_urlfield_normalization(self): f = URLField() self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/')
def _clean_url(url): url_field = URLField() return url_field.clean(url.strip())