summaryrefslogtreecommitdiff
path: root/django/parts
diff options
context:
space:
mode:
Diffstat (limited to 'django/parts')
-rw-r--r--django/parts/__init__.py0
-rw-r--r--django/parts/admin/__init__.py0
-rw-r--r--django/parts/admin/doc.py93
-rw-r--r--django/parts/auth/__init__.py0
-rw-r--r--django/parts/auth/anonymoususers.py48
-rw-r--r--django/parts/auth/formfields.py46
-rw-r--r--django/parts/media/__init__.py0
-rw-r--r--django/parts/media/photos.py6
8 files changed, 193 insertions, 0 deletions
diff --git a/django/parts/__init__.py b/django/parts/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/parts/__init__.py
diff --git a/django/parts/admin/__init__.py b/django/parts/admin/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/parts/admin/__init__.py
diff --git a/django/parts/admin/doc.py b/django/parts/admin/doc.py
new file mode 100644
index 0000000000..b94f45b198
--- /dev/null
+++ b/django/parts/admin/doc.py
@@ -0,0 +1,93 @@
+"""
+Misc. utility functions/classes for documentation generator
+"""
+
+import re
+from email.Parser import HeaderParser
+from email.Errors import HeaderParseError
+import docutils.core
+import docutils.nodes
+import docutils.parsers.rst.roles
+
+#
+# reST roles
+#
+ROLES = {
+ # role name, base role url (in the admin)
+ 'model' : '/doc/models/%s/',
+ 'view' : '/doc/views/%s/',
+ 'template' : '/doc/templates/%s/',
+ 'filter' : '/doc/filters/#%s',
+ 'tag' : '/doc/tags/#%s',
+}
+
+def trim_docstring(docstring):
+ """
+ Uniformly trims leading/trailing whitespace from docstrings.
+
+ Based on http://www.python.org/peps/pep-0257.html#handling-docstring-indentation
+ """
+ if not docstring or not docstring.strip():
+ return ''
+ # Convert tabs to spaces and split into lines
+ lines = docstring.expandtabs().splitlines()
+ indent = min([len(line) - len(line.lstrip()) for line in lines if line.lstrip()])
+ trimmed = [lines[0].lstrip()] + [line[indent:].rstrip() for line in lines[1:]]
+ return "\n".join(trimmed).strip()
+
+def parse_docstring(docstring):
+ """
+ Parse out the parts of a docstring. Returns (title, body, metadata).
+ """
+ docstring = trim_docstring(docstring)
+ parts = re.split(r'\n{2,}', docstring)
+ title = parts[0]
+ if len(parts) == 1:
+ body = ''
+ metadata = {}
+ else:
+ parser = HeaderParser()
+ try:
+ metadata = parser.parsestr(parts[-1])
+ except HeaderParseError:
+ metadata = {}
+ body = "\n\n".join(parts[1:])
+ else:
+ metadata = dict(metadata.items())
+ if metadata:
+ body = "\n\n".join(parts[1:-1])
+ else:
+ body = "\n\n".join(parts[1:])
+ return title, body, metadata
+
+def parse_rst(text, default_reference_context, thing_being_parsed=None):
+ """
+ Convert the string from reST to an XHTML fragment.
+ """
+ overrides = {
+ 'input_encoding' : 'unicode',
+ 'doctitle_xform' : True,
+ 'inital_header_level' : 3,
+ }
+ if thing_being_parsed:
+ thing_being_parsed = "<%s>" % thing_being_parsed
+ parts = docutils.core.publish_parts(text, source_path=thing_being_parsed,
+ destination_path=None, writer_name='html',
+ settings_overrides={'default_reference_context' : default_reference_context})
+ return parts['fragment']
+
+def create_reference_role(rolename, urlbase):
+ def _role(name, rawtext, text, lineno, inliner, options={}, content=[]):
+ node = docutils.nodes.reference(rawtext, text, refuri=(urlbase % text), **options)
+ return [node], []
+ docutils.parsers.rst.roles.register_canonical_role(rolename, _role)
+
+def default_reference_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
+ context = inliner.document.settings.default_reference_context
+ node = docutils.nodes.reference(rawtext, text, refuri=(ROLES[context] % text), **options)
+ return [node], []
+docutils.parsers.rst.roles.register_canonical_role('cmsreference', default_reference_role)
+docutils.parsers.rst.roles.DEFAULT_INTERPRETED_ROLE = 'cmsreference'
+
+for (name, urlbase) in ROLES.items():
+ create_reference_role(name, urlbase)
diff --git a/django/parts/auth/__init__.py b/django/parts/auth/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/parts/auth/__init__.py
diff --git a/django/parts/auth/anonymoususers.py b/django/parts/auth/anonymoususers.py
new file mode 100644
index 0000000000..2a86911f15
--- /dev/null
+++ b/django/parts/auth/anonymoususers.py
@@ -0,0 +1,48 @@
+"""
+Anonymous users
+"""
+
+class AnonymousUser:
+
+ def __init__(self):
+ pass
+
+ def __repr__(self):
+ return 'AnonymousUser'
+
+ def save(self):
+ raise NotImplementedError
+
+ def delete(self):
+ raise NotImplementedError
+
+ def set_password(self, raw_password):
+ raise NotImplementedError
+
+ def check_password(self, raw_password):
+ raise NotImplementedError
+
+ def get_groups(self):
+ return []
+
+ def set_groups(self, group_id_list):
+ raise NotImplementedError
+
+ def get_permissions(self):
+ return []
+
+ def set_permissions(self, permission_id_list):
+ raise NotImplementedError
+
+ def has_perm(self, perm):
+ return False
+
+ def get_and_delete_messages(self):
+ return []
+
+ def add_session(self, session_md5, start_time):
+ "Creates Session for this User, saves it, and returns the new object"
+ raise NotImplementedError
+
+ def is_anonymous(self):
+ return True
diff --git a/django/parts/auth/formfields.py b/django/parts/auth/formfields.py
new file mode 100644
index 0000000000..3565331c8f
--- /dev/null
+++ b/django/parts/auth/formfields.py
@@ -0,0 +1,46 @@
+from django.models.auth import sessions, users
+from django.core import formfields, validators
+
+class AuthenticationForm(formfields.Manipulator):
+ """
+ Base class for authenticating users. Extend this to get a form that accepts
+ username/password logins.
+ """
+ def __init__(self, request=None):
+ """
+ If request is passed in, the manipulator will validate that cookies are
+ enabled. Note that the request (a HttpRequest object) must have set a
+ cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
+ running this validator.
+ """
+ self.request = request
+ self.fields = [
+ formfields.TextField(field_name="username", length=15, maxlength=30, is_required=True,
+ validator_list=[self.isValidUser, self.hasCookiesEnabled]),
+ formfields.PasswordField(field_name="password", length=15, maxlength=30, is_required=True,
+ validator_list=[self.isValidPasswordForUser]),
+ ]
+ self.user_cache = None
+
+ def hasCookiesEnabled(self, field_data, all_data):
+ if self.request and (not self.request.COOKIES.has_key(sessions.TEST_COOKIE_NAME) or self.request.COOKIES[sessions.TEST_COOKIE_NAME] != sessions.TEST_COOKIE_VALUE):
+ raise validators.ValidationError, "Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in."
+
+ def isValidUser(self, field_data, all_data):
+ try:
+ self.user_cache = users.get_object(username__exact=field_data)
+ except users.UserDoesNotExist:
+ raise validators.ValidationError, "Please enter a correct username and password. Note that both fields are case-sensitive."
+
+ def isValidPasswordForUser(self, field_data, all_data):
+ if self.user_cache is not None and not self.user_cache.check_password(field_data):
+ self.user_cache = None
+ raise validators.ValidationError, "Please enter a correct username and password. Note that both fields are case-sensitive."
+
+ def get_user_id(self):
+ if self.user_cache:
+ return self.user_cache.id
+ return None
+
+ def get_user(self):
+ return self.user_cache
diff --git a/django/parts/media/__init__.py b/django/parts/media/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/parts/media/__init__.py
diff --git a/django/parts/media/photos.py b/django/parts/media/photos.py
new file mode 100644
index 0000000000..a14b3de19b
--- /dev/null
+++ b/django/parts/media/photos.py
@@ -0,0 +1,6 @@
+import re
+
+def get_thumbnail_url(photo_url, width):
+ bits = photo_url.split('/')
+ bits[-1] = re.sub(r'(?i)\.(gif|jpg)$', '_t%s.\\1' % width, bits[-1])
+ return '/'.join(bits)