Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django object pattern, in order to match for example a numeric id in URI... #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed __init__.py
Empty file.
49 changes: 49 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

try:
from setuptools import setup, find_packages
except ImportError:
import ez_setup
ez_setup.use_setuptools()
from setuptools import setup, find_packages

VERSION = __import__('uriredirect').__version__

import os


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
name = 'uriredirect',
version = VERSION,
description = 'URI Redirecttion for Django',
packages = ['uriredirect.admin',
'uriredirect.http',
'uriredirect.models',
'uriredirect.tests',
'uriredirect.views',
],
include_package_data = True,
author = 'Ryan Clark',
zip_safe = False,
install_requires = ['mimeparse', ],
long_description = open('README.md').read(),
url = 'https://github.com/usgin/uriredirect',
download_url = 'https://github.com/usgin/uriredirect/tarball/master',
classifiers = [
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Framework :: Django',
'Natural Language :: English',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
],

)

8 changes: 8 additions & 0 deletions uriredirect/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
VERSION = (0, 1, 0)


def get_version():
version = '%s.%s.%s' % (VERSION[0], VERSION[1], VERSION[2])
return version

__version__ = get_version()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 19 additions & 1 deletion models/RewriteRule.py → uriredirect/models/RewriteRule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import models
from AcceptMapping import AcceptMapping
import mimeparse, re
from django.db.models.loading import get_model


class RewriteRule(models.Model):
class Meta:
Expand Down Expand Up @@ -53,4 +55,20 @@ def resolve_url_template(self, requested_uri, url_template):
if match.lastindex != None:
for i in range(match.lastindex):
url_template = re.sub('\$' + str(i + 1), match.group(i + 1), url_template)
return url_template
# Django model pattern matching
model_match = re.match(u'.*@(\w+:\w+\.\w+)@.*', url_template)
if(model_match):
try:
django_pattern = model_match.group(1)
lookup_field = django_pattern.split(':')[0] # field name of the filter we'll use to select the django object
lookup_model = django_pattern.split(':')[1] # app_label.model_name
app_label = lookup_model.split('.')[0]
model_label = lookup_model.split('.')[1]
model = get_model(app_label, model_label)
obj = model.objects.get(**{lookup_field: match.groupdict()[lookup_field]})
url_template = re.sub(u'@' + django_pattern + u'@', obj.slug, url_template)
except model.DoesNotExist:
return None
return url_template


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions views/Resolver.py → uriredirect/views/Resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ def resolve_uri(request, registry_label, requested_uri):
return HttpResponseServerError('More than one representation is defined for the acceptable MIME type: %s' % content_type)
else:
url_template = url_templates[0]

# Convert the URL template to a resolvable URL
url = rule.resolve_url_template(requested_uri, url_template)

# Perform the redirection
return HttpResponseSeeOther(url)
# Perform the redirection if the resolver returns something, or a 404 instead
if url:
return HttpResponseSeeOther(url)
else:
return HttpResponseNotFound('The requested URI did not return any document')
File renamed without changes.