Skip to content
This repository
Browse code

Improove url generation

  • Loading branch information...
commit 6f64ebd05476e2149e2e71deeefbb10f8edfc412 1 parent cb02c16
Mikhail Podgurskiy authored August 02, 2011
33  django_any/models.py
@@ -10,6 +10,7 @@
10 10
 from random import choice
11 11
 
12 12
 from django.core.exceptions import ValidationError
  13
+from django.core import validators
13 14
 from django.db import models, IntegrityError
14 15
 from django.db.models import Q
15 16
 from django.db.models.fields.files import FieldFile
@@ -370,16 +371,28 @@ def any_url_field(field, **kwargs):
370 371
     >>> re.match(URLValidator.regex, result) is not None
371 372
     True
372 373
     """
373  
-    url = ['http://news.yandex.ru/society.html',
374  
-           'http://video.google.com/?hl=en&tab=wv',
375  
-           'http://www.microsoft.com/en/us/default.aspx',
376  
-           'http://habrahabr.ru/company/opera/',
377  
-           'http://www.apple.com/support/hardware/',
378  
-           'http://ya.ru',
379  
-           'http://google.com',
380  
-           'http://fr.wikipedia.org/wiki/France']
381  
-    url = kwargs.get('url', url)
382  
-    return choice(url)
  374
+    url = kwargs.get('url')
  375
+
  376
+    if not url:
  377
+        verified = [validator for validator in field.validators \
  378
+                    if isinstance(validator, validators.URLValidator) and \
  379
+                    validator.verify_exists == True]
  380
+        if verified:
  381
+            url = choice(['http://news.yandex.ru/society.html',
  382
+                          'http://video.google.com/?hl=en&tab=wv',
  383
+                          'http://www.microsoft.com/en/us/default.aspx',
  384
+                          'http://habrahabr.ru/company/opera/',
  385
+                          'http://www.apple.com/support/hardware/',
  386
+                          'http://ya.ru',
  387
+                          'http://google.com',
  388
+                          'http://fr.wikipedia.org/wiki/France'])
  389
+        else:
  390
+            url = "http://%s.%s/%s" % (
  391
+                xunit.any_string(max_length=10),
  392
+                xunit.any_string(min_length=2, max_length=3),
  393
+                xunit.any_string(max_length=20))
  394
+
  395
+    return url
383 396
 
384 397
 
385 398
 @any_field.register(models.TimeField)
2  django_any/tests/model_creation_simple.py
@@ -24,7 +24,7 @@ class SimpleModel(models.Model):
24 24
     slig_field = models.SlugField()
25 25
     text_field = models.TextField()
26 26
     time_field = models.TimeField()
27  
-    url_field = models.URLField()
  27
+    url_field = models.URLField(verify_exists=False)
28 28
 
29 29
     class Meta:
30 30
         app_label = 'django_any'

0 notes on commit 6f64ebd

Please sign in to comment.
Something went wrong with that request. Please try again.