RedirectAnywhere

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
### redirect_anywhere/models.py ###
from django.db import models

class RedirectAnywhere(models.Model):
    md5sum = models.CharField(max_length=40)
    url = models.CharField(max_length=500)

    class Meta:
        verbose_name_plural = "redirect anywhere"



### redirect_anywhere/urls.py ###
from django.conf.urls.defaults import *
from redirect_anywhere.views import *

urlpatterns = patterns('',
    (r'^$', redirect_anywhere),
    (r'^(?P<md5sum>\w+)/$', redirect_anywhere),
)



### redirect_anywhere/views.py ###
import hashlib
from django.shortcuts import render_to_response
from django.template import RequestContext
from redirect_anywhere.models import *

def redirect_anywhere(request, md5sum=None):
    redirect_anywhere = None
    new_redirect_anywhere = None

    if request.method == 'POST':
        if request.POST.has_key('direct_url'):
            direct_url = request.POST['direct_url']
            md5sum = hashlib.md5(request.POST['direct_url']).hexdigest()
            try:
                new_redirect_anywhere = RedirectAnywhere.objects.get(md5sum = md5sum)
            except RedirectAnywhere.DoesNotExist:
                new_redirect_anywhere = RedirectAnywhere(md5sum = md5sum, url = direct_url)
                new_redirect_anywhere.save()
    elif md5sum:
        try:
            redirect_anywhere = RedirectAnywhere.objects.get(md5sum = md5sum)
        except RedirectAnywhere.DoesNotExist:
            pass

    context = {
        'redirect_anywhere': redirect_anywhere,
        'new_redirect_anywhere': new_redirect_anywhere,
    }
    return render_to_response('redirect_anywhere/redirect_anywhere.html', context,
        context_instance = RequestContext(request))



### template/redirect_anywhere/redirect_anywhere.html (simplified example) ###
<form method="post" action=".">
    Your blocked URL:
    <input type="text" name="direct_url" size="40" />
    <input type="submit" value="Get RedirectAnywhere URL" />
</form>

{% if redirect_anywhere %}
<br />Your RedirectAnywhere URL is to unblock the URL below. Click on it to continue..
<h1><a href="{{redirect_anywhere.url}}/">{{redirect_anywhere.url}}</a></h1>
{% endif %}

{% if new_redirect_anywhere %}
<br />Your RedirectAnywhere URL for <a href="{{new_redirect_anywhere.url}}">{{new_redirect_anywhere.url}}</a> is:<br />
<textarea class="textarea">dazzlepod.com/redirect_anywhere/{{new_redirect_anywhere.md5sum}}</textarea>
{% endif %}

More like this

  1. Effective content caching for mass-load site using redirect feature by nnseva 1 year, 10 months ago
  2. Facebook Authentication Backend by barnardo 2 years, 11 months ago
  3. Create short URL redirects for site urls. by matt.geek.nz 4 years, 3 months ago
  4. Decorator to modify reverse() to render SSL urls by AndrewIngram 4 years ago
  5. Mobilize your Django site by stevena0 4 years, 1 month ago

Comments

(Forgotten your password?)