Sort of works now
This commit is contained in:
parent
593f3e6d1c
commit
5972323cc1
15 changed files with 453 additions and 6 deletions
0
links/__init__.py
Normal file
0
links/__init__.py
Normal file
3
links/admin.py
Normal file
3
links/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
27
links/migrations/0001_initial.py
Normal file
27
links/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Link',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('link', models.URLField(max_length=2048)),
|
||||
('base62', models.CharField(max_length=64, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('clicks', models.IntegerField(default=0)),
|
||||
('ip', models.GenericIPAddressField(null=True)),
|
||||
],
|
||||
options={
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
20
links/migrations/0002_auto_20150117_2248.py
Normal file
20
links/migrations/0002_auto_20150117_2248.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('links', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='link',
|
||||
name='id',
|
||||
field=models.AutoField(serialize=False, primary_key=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
24
links/migrations/0003_auto_20150117_2306.py
Normal file
24
links/migrations/0003_auto_20150117_2306.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('links', '0002_auto_20150117_2248'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='link',
|
||||
name='base62',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='link',
|
||||
name='id',
|
||||
field=models.CharField(max_length=12, serialize=False, primary_key=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
19
links/migrations/0004_auto_20150117_2338.py
Normal file
19
links/migrations/0004_auto_20150117_2338.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('links', '0003_auto_20150117_2306'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='link',
|
||||
old_name='link',
|
||||
new_name='url',
|
||||
),
|
||||
]
|
0
links/migrations/__init__.py
Normal file
0
links/migrations/__init__.py
Normal file
54
links/models.py
Normal file
54
links/models.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
from django.db import models
|
||||
from django.forms import ModelForm, CharField, TextInput
|
||||
import random
|
||||
|
||||
|
||||
class Link(models.Model):
|
||||
id = models.CharField(primary_key=True, max_length=12)
|
||||
url = models.URLField(max_length=2048)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
clicks = models.IntegerField(default=0)
|
||||
ip = models.GenericIPAddressField(null=True)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.url
|
||||
|
||||
def generate_unique_id(self, length=12):
|
||||
attempts = 0
|
||||
id = random_id()
|
||||
try:
|
||||
while Link.objects.get(id=id) and attempts < 10:
|
||||
attempts = attempts + 1
|
||||
id = random_id()
|
||||
except:
|
||||
pass
|
||||
return id
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.pk:
|
||||
self.id = self.generate_unique_id()
|
||||
super(Link, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class LinkForm(ModelForm):
|
||||
url = CharField(label='')
|
||||
class Meta:
|
||||
model = Link
|
||||
fields = ['url']
|
||||
|
||||
|
||||
def random_id():
|
||||
rand = ''
|
||||
for i in range(0,12):
|
||||
rand += int_to_char(random.randint(0,61));
|
||||
return rand;
|
||||
|
||||
|
||||
def int_to_char(int):
|
||||
if(int < 10):
|
||||
char = chr(int + 48)
|
||||
elif(int < 36):
|
||||
char = chr(int + 55)
|
||||
else:
|
||||
char = chr(int + 61)
|
||||
return char
|
40
links/templates/index.html
Normal file
40
links/templates/index.html
Normal file
|
@ -0,0 +1,40 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<link rel="stylesheet" href="/static/style.css" />
|
||||
<title>minie links!</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="main-box">
|
||||
<form method="post" action="/">
|
||||
<ul>
|
||||
{{form.as_ul}}
|
||||
<li><input type="submit" value="shrink" name="shrink" id="main-submit" /></li>
|
||||
</ul>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div id="logo-wrap">
|
||||
<div id="logo">
|
||||
<a href="/"><img src="/static/minie.png" alt="min.ie" /></a>
|
||||
</div>
|
||||
{% if short_url %}
|
||||
<form name="url">
|
||||
<label onclick=\"document.url.short_link.focus();document.url.short_link.select();\" for=\"short_link\">Your Link</label>
|
||||
<input name='short_link' value='{{short_url}}' id='short_link' readonly='true' size='25' onclick='javascript:this.focus();this.select();' />
|
||||
</form>
|
||||
{% endif %}
|
||||
<div id="info">Drag this bookmarklet <a onclick="return false" href="javascript:void(window.location%20=%20'http://min.ie/'+window.location)">min.ie</a> to your bookmark bar, or just stick "min.ie/" before any link you want to shrink.</div>
|
||||
<p>Django and Postgres Powered</p>
|
||||
</div>
|
||||
|
||||
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
_uacct = "UA-66209-5";
|
||||
urchinTracker();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
3
links/tests.py
Normal file
3
links/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
26
links/views.py
Normal file
26
links/views.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
from django.shortcuts import render, redirect
|
||||
from django.http import Http404
|
||||
from .models import Link, LinkForm
|
||||
from urlparse import urlparse
|
||||
|
||||
def catchall(request, id):
|
||||
try:
|
||||
link = Link.objects.get(id=id)
|
||||
return redirect(link.url)
|
||||
except:
|
||||
parsed = urlparse(id)
|
||||
if parsed.netloc:
|
||||
link = Link(url=id)
|
||||
link.save();
|
||||
context = {'form': LinkForm}
|
||||
context['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||
return render(request, 'index.html', context)
|
||||
raise Http404("Link does not exist")
|
||||
|
||||
def home(request):
|
||||
context = {'form': LinkForm}
|
||||
if 'link' in request.POST:
|
||||
link = Link(link=request.POST['url'])
|
||||
link.save();
|
||||
context['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||
return render(request, 'index.html', context)
|
Loading…
Add table
Add a link
Reference in a new issue