Skip to content

Instantly share code, notes, and snippets.

@andymckay
Created February 3, 2010 23:02
Show Gist options
  • Save andymckay/294147 to your computer and use it in GitHub Desktop.
Save andymckay/294147 to your computer and use it in GitHub Desktop.
# a URL field that supports webcal:// links for Django
#
from django.forms.fields import URLField, URL_VALIDATOR_USER_AGENT, RegexField
import re
url_re = re.compile(
r'^(https?|webcal)://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|/\S+)$', re.IGNORECASE)
class URLWebCalField(URLField):
def __init__(self, max_length=None, min_length=None, verify_exists=False,
validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
RegexField.__init__(self, url_re, max_length, min_length,
*args, **kwargs)
self.verify_exists = verify_exists
self.user_agent = validator_user_agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment