-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbModels.py
More file actions
66 lines (49 loc) · 1.84 KB
/
Copy pathdbModels.py
File metadata and controls
66 lines (49 loc) · 1.84 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
__author__ = 'andre'
from utils import make_pw_hash, valid_pw
import logging
from handlers import *
from google.appengine.ext import db
from google.appengine.api import memcache
import main
def users_key(group = 'default'):
return db.Key.from_path('users', group)
class User(db.Model):
name = db.StringProperty(required = True)
pw_hash = db.StringProperty(required = True)
email = db.StringProperty()
@classmethod #mean that this method is static and is called on class (ex: User.by_id)
def by_id(cls, uid):
return User.get_by_id(uid, parent = users_key())
#return db.GqlQuery("SELECT * FROM User WHERE __key__=uid")
@classmethod
def by_name(cls, name):
u = User.all().filter('name =', name).get()
#u = db.GqlQuery("SELECT * FROM User WHERE name = name")
return u
@classmethod
def register(cls, name, pw, email = None):
pw_hash = make_pw_hash(name, pw)
return User(parent = users_key(),
name = name,
pw_hash = pw_hash,
email = email)
@classmethod
def login(cls, name, pw):
u = cls.by_name(name)
if u and valid_pw(name, pw, u.pw_hash):
return u
class Blogentry(db.Model):
subject = db.StringProperty(required=True)
content = db.TextProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
last_modified = db.DateTimeProperty(auto_now = True)
def render(self):
self._render_text = self.content.replace('\n', '<br>')
return render_str("blog.html", entries=self)
def as_dict(self):
time_fmt = '%c'
d = {'subject': self.subject,
'content': self.content,
'created': self.created.strftime(time_fmt),
'last_modified': self.last_modified.strftime(time_fmt)}
return d