-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
98 lines (77 loc) · 2.54 KB
/
models.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from exts import db
from datetime import datetime
# text code model
"""
class EditorCode:
id:int primary key
title:str
description:str (text)
code = str (text)
language:str
"""
class EditorCode(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=True)
description = db.Column(db.Text, nullable=True)
code = db.Column(db.Text, nullable=False)
language = db.Column(db.String(50), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
user = db.relationship("User", backref=db.backref("editor_codes", lazy=True))
def __repr__(self):
return f"<EditorCode {self.title} >"
def save(self):
"""
The save function is used to save the changes made to a model instance.
It takes in no arguments and returns nothing.
:param self: Refer to the current instance of the class
:return: The object that was just saved
:doc-author:jod35
"""
db.session.add(self)
db.session.commit()
def delete(self):
"""
The delete function is used to delete a specific row in the database. It takes no parameters and returns nothing.
:param self: Refer to the current instance of the class, and is used to access variables that belongs to the class
:return: Nothing
:doc-author:jod35
"""
db.session.delete(self)
db.session.commit()
def update(self, title, description, code, language):
self.title = title
self.description = description
self.code = code
self.language = language
db.session.commit()
# user model
"""
class User:
id:integer
username:string
email:string
password:string
"""
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(25), nullable=False, unique=True)
email = db.Column(db.String(80), nullable=False, unique=True)
password = db.Column(db.Text(), nullable=False)
def to_dict(self):
return {
"id": self.id,
"username": self.username,
"email": self.email,
}
def __repr__(self):
"""
returns string rep of object
"""
return f"<User {self.username}>"
def save(self):
db.session.add(self)
db.session.commit()