min.ie/links/models.py

54 lines
1.2 KiB
Python
Raw Normal View History

2015-01-17 23:54:26 +00:00
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)
2015-01-18 01:58:33 +00:00
created_at = models.DateTimeField(auto_now_add=True)
2015-01-17 23:54:26 +00:00
clicks = models.IntegerField(default=0)
ip = models.GenericIPAddressField(null=True)
def __unicode__(self):
return self.url
def generate_unique_id(self, length=8):
2015-01-17 23:54:26 +00:00
attempts = 0
id = random_id(length)
2015-01-17 23:54:26 +00:00
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(length):
2015-01-17 23:54:26 +00:00
rand = ''
for i in range(0,length):
2015-01-17 23:54:26 +00:00
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