This file looks large and may slow your browser down if we attempt
to syntax highlight it, so we are showing it without any
pretty colors.
Highlight
it anyway.
1 |
|
2 |
# -*- coding utf-8 -*- |
3 |
|
4 |
|
5 |
from flask.ext.sqlalchemy import SQLAlchemy |
6 |
from datetime import datetime |
7 |
|
8 |
db = SQLAlchemy() |
9 |
|
10 |
class User(db.Model): |
11 |
id = db.Column(db.Integer, primary_key=True) |
12 |
username = db.Column(db.String(80)) |
13 |
email = db.Column(db.String(120), unique=True) |
14 |
memberships = db.relationship('Membership', backref='user', lazy='dynamic') |
15 |
|
16 |
def __init__(self, username, email): |
17 |
self.username = username |
18 |
self.email = email |
19 |
|
20 |
@staticmethod |
21 |
def getById(id): |
22 |
return User.query.get(id) |
23 |
|
24 |
@staticmethod |
25 |
def getByEmail(email): #Query record by email |
26 |
tofind = User.query.filter_by(email=email).first() |
27 |
return tofind |
28 |
|
29 |
@staticmethod |
30 |
def getByName(username): #Query record by username |
31 |
tofind = User.query.filter_by(username=username).first() |
32 |
return tofind |
33 |
|
34 |
def persist(self): #To create record |
35 |
db.session.add(self) |
36 |
db.session.commit() |
37 |
|
38 |
def delete(self): # To delete a record |
39 |
db.session.delete(self) |
40 |
db.session.commit() |
41 |
|
42 |
|
43 |
#def update_username(self, email, newusername): # to update Username column |
44 |
# self.newusername = newusername |
45 |
# self.email = email |
46 |
# toupdate = self.findrow(email) |
47 |
#toupdate.username = newusername |
48 |
# db.session.add(toupdate) |
49 |
#db.session.commit() |
50 |
|
51 |
def __repr__(self): |
52 |
return '<User %r %r>' % (self.username, self.email) |