Reorganise project layout
This commit is contained in:
parent
206434b7de
commit
2dee05b51e
17 changed files with 10 additions and 103 deletions
|
@ -1,54 +0,0 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue