-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
186 lines (154 loc) · 7.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import datetime
from dataclasses import dataclass
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, LargeBinary, Float, \
UniqueConstraint, Text
from sqlalchemy.orm import relationship
from sqlalchemy_utils import EmailType, ChoiceType, PasswordType
from Fast_blog.database.databaseconnection import Base
@dataclass
class UserPrivileges(Base):
Typeofuserchoices = [
('admin', 'admin'),
('editer', 'editer'),
('NULL', 'NULL')
]
__tablename__ = "AdminPrivileges"
__table_args__ = {'extend_existing': True}
NameId = Column(Integer, primary_key=True, index=True)
privilegeName = Column(ChoiceType(Typeofuserchoices), default="1")
@dataclass
class User(Base):
ActivationStateType = [
('YA', 'Activated'),
('NA', 'Pending Activation'),
('NULL', 'NULL')
]
__tablename__ = "usertable"
__table_args__ = {'extend_existing': True}
UserId = Column(Integer, primary_key=True, index=True)
username = Column(String(255), unique=True)
userpassword = Column(PasswordType(schemes=['pbkdf2_sha256']))
creation_time = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
Last_Login_Time = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
UserUuid = Column(String(255))
UserEmail = Column(EmailType(255))
UserAvatar = Column(String(255), unique=True)
comments = relationship("Comment", back_populates="user")
ActivationCode = Column(Integer, default=None)
ActivationState = Column(ChoiceType(ActivationStateType), default="NA")
def to_dict(self):
return dict(UserId=self.UserId, username=self.username, userpassword=self.userpassword,
UserEmail=self.UserEmail, UserUuid=self.UserUuid, ActivationCode=self.ActivationCode,
ActivationState=self.ActivationState)
@dataclass
class Comment(Base):
__tablename__ = "comments"
id = Column(Integer, primary_key=True, index=True)
parentId = Column(Integer, ForeignKey('comments.id'), nullable=True)
uid = Column(Integer, ForeignKey('usertable.UserId'))
blog_id = Column(Integer, ForeignKey('blogtable.BlogId'))
blog = relationship("Blog", back_populates="comments")
address = Column(String(255))
content = Column(String(255), nullable=False) # 修改此行,为 content 指定长度
likes = Column(Integer, default=0)
createTime = Column(DateTime, default=datetime.datetime.now) # 修改此行,为 createTime 指定长度
contentImg = Column(String(255)) # 修改此行,为 contentImg 指定长度
user = relationship("User", back_populates="comments")
replies = relationship("Comment", backref="parent_comment", remote_side=[id])
def to_dict(self):
return dict(parentId=self.parentId, uid=self.uid, blog_id=self.blog_id, content=self.content, likes=self,
createTime=self.createTime, contentImg=self.contentImg, user=self.user, replies=self.replies)
@dataclass
class AdminUser(Base):
choices = [
('0', 'woman'),
('1', 'man'),
('2', 'NULL')
]
__tablename__ = "Admintable"
__table_args__ = {'extend_existing': True}
UserId = Column(Integer, primary_key=True, index=True)
username = Column(String(255), unique=True)
userpassword = Column(PasswordType(schemes=['pbkdf2_sha256']))
gender = Column(ChoiceType(choices), default="2")
creation_time = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
Last_Login_Time = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
UserUuid = Column(String(255))
UserEmail = Column(EmailType(255))
userPrivileges = Column(Integer, ForeignKey('AdminPrivileges.NameId'))
privileges = relationship("UserPrivileges", foreign_keys=[userPrivileges], lazy="select")
def to_dict(self):
return dict(UserId=self.UserId, username=self.username, gender=self.gender, UserEmail=self.UserEmail,
UserUuid=self.UserUuid, userPrivileges=self.userPrivileges)
@dataclass
class Blog(Base):
__tablename__ = "blogtable"
__table_args__ = {'extend_existing': True}
BlogId = Column(Integer, primary_key=True, index=True)
title = Column(String(255))
content = Column(LargeBinary)
BlogIntroductionPicture = Column(String(255))
created_at = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
NumberLikes = Column(Integer)
NumberViews = Column(Integer)
author = Column(String(255))
BlogTags = relationship("BlogTag", back_populates="blogs", primaryjoin="Blog.BlogId == BlogTag.blog_id")
admin_id = Column(Integer, ForeignKey('Admintable.UserId'))
ratings = relationship("BlogRating", back_populates="blog")
comments = relationship("Comment", back_populates="blog")
def to_dict(self):
return dict(BlogId=self.BlogId, title=self.title, content=self.content, author=self.author,
BlogIntroductionPicture=self.BlogIntroductionPicture, created_at=self.created_at)
class BlogTag(Base):
__tablename__ = "blogtag"
__allow_unmapped__ = True
id = Column(Integer, primary_key=True, index=True)
Article_Type = Column(String(255), index=True)
tag_created_at = Column(DateTime, default=datetime.datetime.now)
blog_id = Column(Integer, ForeignKey('blogtable.BlogId'))
blogs = relationship("Blog", back_populates="BlogTags", primaryjoin="BlogTag.blog_id == Blog.BlogId", uselist=True)
def to_dict(self):
return dict(blog_id=self.blog_id, id=self.id, blog_type=self.Article_Type,
tag_created_at=self.tag_created_at.timestamp(),
Related_Blogs=[blog.to_dict() for blog in self.blogs])
@dataclass
class PowerMeters(Base):
__tablename__ = "powertable"
__table_args__ = {'extend_existing': True}
PowerId = Column(Integer, primary_key=True, index=True)
DataNum = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d"))
electricityNum = Column(String(255))
PowerConsumption = Column(String(255))
AveragePower = Column(String(255))
def to_dict(self):
return dict(DataNum=self.DataNum, electricityNum=self.electricityNum, PowerConsumption=self.PowerConsumption,
AveragePower=self.AveragePower)
@dataclass
class BlogRating(Base):
__tablename__ = "blog_ratings"
id = Column(Integer, primary_key=True, index=True)
blog_id = Column(Integer, ForeignKey("blogtable.BlogId"))
rating = Column(Float)
blog = relationship("Blog", back_populates="ratings")
def to_dict(self):
return dict(blog_id=self.blog_id, rating=self.rating, blog=self.blog)
@dataclass
class Vote(Base):
__tablename__ = "votes"
id = Column(Integer, primary_key=True, index=True)
device_id = Column(String(255), index=True) # 指定了长度为 255 字符
blog_id = Column(String(255), index=True)
vote_count = Column(Integer, default=0)
__table_args__ = (UniqueConstraint('device_id', 'blog_id'),)
@dataclass
class ReptileInclusion(Base):
__tablename__ = "ReptileInclusion"
id = Column(Integer, primary_key=True, index=True)
blog_id = Column(Integer, ForeignKey("blogtable.BlogId"))
GoogleSubmissionStatus = Column(String(255))
BingSubmissionStatus = Column(String(255))
Submissiontime = Column(DateTime, default=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
ReturnLog = Column(Text)
def to_dict(self):
return dict(id=self.id, blog_id=self.blog_id, GoogleSubmissionStatus=self.googleSubmissionStatus,
BingSubmissionStatus=self.bingSubmissionStatus, Submissiontime=self.Submissiontime)