Reorganise project layout
This commit is contained in:
parent
206434b7de
commit
2dee05b51e
17 changed files with 10 additions and 103 deletions
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
2
app/admin.py
Normal file
2
app/admin.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from django.contrib import admin
|
||||
|
0
app/migrations/__init__.py
Normal file
0
app/migrations/__init__.py
Normal file
54
app/models.py
Normal file
54
app/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=8):
|
||||
attempts = 0
|
||||
id = random_id(length)
|
||||
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(widget=TextInput(attrs={'class':'u-full-width', 'placeholder': 'Paste the link you want to shorten'}), label='')
|
||||
class Meta:
|
||||
model = Link
|
||||
fields = ['url']
|
||||
|
||||
|
||||
def random_id(length):
|
||||
rand = ''
|
||||
for i in range(0,length):
|
||||
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
|
28
app/templates/404.html
Normal file
28
app/templates/404.html
Normal file
|
@ -0,0 +1,28 @@
|
|||
<!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 class="container not-found">
|
||||
<div id="logo">
|
||||
<a href="/"><img src="/static/minie.png" alt="min.ie" /></a>
|
||||
</div>
|
||||
|
||||
<h2>Page not found</h2>
|
||||
<p>Try this one instead <a href="https://min.ie/WVFdWJMa" target="_blank">https://min.ie/WVFdWJMa</a></p>
|
||||
|
||||
<footer><a href="https://twitter.com/kudoz">Tweet</a> or <a href="https://github.com/kudos/min.ie">Fork</a>. © <a href="http://crem.in">Jonathan Cremin</a></footer>
|
||||
</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>
|
40
app/templates/index.html
Normal file
40
app/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 class="container">
|
||||
<div id="logo">
|
||||
<a href="/"><img src="/static/minie.png" alt="min.ie" /></a>
|
||||
</div>
|
||||
|
||||
<form method="post" action="/">
|
||||
{{form.url}}
|
||||
<input type="submit" value="shrink" name="shrink" class="button-primary" />
|
||||
</form>
|
||||
|
||||
{% if short_url %}
|
||||
<form name="url">
|
||||
<label onclick="document.url.short_link.focus();document.url.short_link.select();" for="short_link">Short Link</label>
|
||||
<input type="text" class="u-full-width" name='short_link' value='{{short_url}}' readonly='true' onclick='javascript:this.focus();this.select();' />
|
||||
</form>
|
||||
{% endif %}
|
||||
<div id="info">
|
||||
<p>Makes links shorter, like this: <a href="https://min.ie/WVFdWJMa" target="_blank">https://min.ie/WVFdWJMa</a></p>
|
||||
<p>Drag this bookmarklet <a onclick="return false" class="bookmark" href="javascript:void(window.location='http://min.ie/'+window.location)">min.ie</a> to your bookmark bar for quick shrinking. Then just click the bookmark on a page you want a short url for.</p>
|
||||
</div>
|
||||
<footer><a href="https://twitter.com/kudoz">Tweet</a> or <a href="https://github.com/kudos/min.ie">Fork</a>. © <a href="http://crem.in">Jonathan Cremin</a></footer>
|
||||
</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
app/tests.py
Normal file
3
app/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
44
app/views.py
Normal file
44
app/views.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from urlparse import urlparse
|
||||
from django.shortcuts import render, redirect
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.contrib import messages
|
||||
from .models import Link, LinkForm
|
||||
|
||||
|
||||
def get_client_ip(request):
|
||||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||
if x_forwarded_for:
|
||||
ip = x_forwarded_for.split(',')[0]
|
||||
else:
|
||||
ip = request.META.get('REMOTE_ADDR')
|
||||
return ip
|
||||
|
||||
|
||||
def catchall(request, id):
|
||||
try:
|
||||
link = Link.objects.get(id=id)
|
||||
parsed = urlparse(link.url)
|
||||
if parsed.scheme:
|
||||
return redirect(link.url)
|
||||
return redirect("http://" + link.url)
|
||||
except Exception as e:
|
||||
parsed = urlparse(id)
|
||||
if parsed.netloc:
|
||||
link = Link(url=id, ip=get_client_ip(request))
|
||||
link.save();
|
||||
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||
return redirect('/')
|
||||
raise Http404("Link does not exist")
|
||||
|
||||
|
||||
def home(request):
|
||||
context = {'form': LinkForm}
|
||||
if 'short_url' in request.session and request.session['short_url']:
|
||||
context['short_url'] = request.session['short_url']
|
||||
request.session['short_url'] = None
|
||||
if 'url' in request.POST:
|
||||
link = Link(url=request.POST['url'], ip=get_client_ip(request))
|
||||
link.save();
|
||||
request.session['short_url'] = "http://" + request.get_host() + "/" + link.id
|
||||
return redirect('/')
|
||||
return render(request, 'index.html', context)
|
Loading…
Add table
Add a link
Reference in a new issue