pep8
This commit is contained in:
parent
df10c67999
commit
b397561cce
5 changed files with 83 additions and 80 deletions
|
@ -4,51 +4,55 @@ 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)
|
||||
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 __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)
|
||||
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']
|
||||
|
||||
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;
|
||||
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
|
||||
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