-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathurlModel.py
More file actions
53 lines (42 loc) · 1.27 KB
/
urlModel.py
File metadata and controls
53 lines (42 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from google.appengine.ext import db
from shortener import Shortener
class UrlModel(db.Model):
url = db.StringProperty(multiline=False)
shortname = db.StringProperty(multiline=False)
creation_date = db.DateTimeProperty(auto_now_add=True)
hits = db.IntegerProperty()
class UrlController():
def find_shortname(self, url):
shortener = Shortener()
nb = 2
while nb < 10:
shortname = shortener.shorten(url, nb)
o = self.get_by_key(shortname)
if not o: return shortname
nb = nb + 1
def save(self, url):
if len(url) == 0: return
o = self.get_by_url(url)
if not o:
o = UrlModel()
o.url = url
o.shortname = self.find_shortname(url)
o.hits = 0
o.put()
return o
def get_by_url(self, url):
l = db.GqlQuery('SELECT * FROM UrlModel WHERE url = :1', url).fetch(1)
if len(l) == 1: return l[0]
return None
def get_by_key(self, shortname):
l = db.GqlQuery('SELECT * FROM UrlModel WHERE shortname = :1', shortname).fetch(1)
if len(l) == 1:
o = l[0]
o.hits = o.hits + 1
o.put()
return o
return None
def get_lasts(self, number = 25):
l = db.GqlQuery('SELECT * FROM UrlModel ORDER BY creation_date DESC').fetch(number)
if len(l) > 0: return l
return None