-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
126 lines (99 loc) · 4.64 KB
/
model.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from peewee import *
from playhouse.migrate import *
from datetime import datetime
try:
raise ImportError
import conf
import psycopg2
from playhouse.pool import PooledPostgresqlExtDatabase
db = PooledPostgresqlExtDatabase(
conf.dbname,
max_connections=8,
stale_timeout=300,
user=conf.user,
host=conf.host,
password=conf.passwd,
autorollback=True,
register_hstore=False)
migrator = PostgresqlMigrator(db)
except ImportError:
db = SqliteDatabase('db.sqlite')
migrator = SqliteMigrator(db)
class BaseModel(Model):
class Meta:
database = db
class Provs(BaseModel):
id = BigIntegerField(null=False, primary_key=True, verbose_name='id')
prov = CharField(null=False, unique=True, verbose_name='省份')
class Industry(BaseModel):
id = BigIntegerField(null=False, primary_key=True, verbose_name='id')
industry = CharField(null=False, unique=True, verbose_name='行业')
class Users(BaseModel):
id = BigIntegerField(null=False, primary_key=True, verbose_name='id')
name = CharField(max_length=255, null=False, verbose_name='用户名')
linkname = CharField(max_length=255, unique=True, null=False, verbose_name='链接名')
signature = CharField(max_length=1023, null=True, verbose_name='签名')
prestige = IntegerField(null=True, default=None, verbose_name='威望')
approve = IntegerField(null=True, default=None, verbose_name='赞同')
thank = IntegerField(null=True, default=None, verbose_name='感谢')
coins = IntegerField(null=True, default=None, verbose_name='金币')
visits = IntegerField(null=True, verbose_name='访问量')
prov = ForeignKeyField(Provs, null=True, related_name='users', verbose_name='省份')
locate = CharField(null=True, verbose_name='地区')
industry = ForeignKeyField(Industry, null=True, related_name='users', verbose_name='行业')
level = IntegerField(null=True, verbose_name='用户等级')
last_signin_at = DateTimeField(null=True, verbose_name='最后登录时间')
crawled_at = DateTimeField(default=datetime.now, verbose_name='抓取时间')
class Posts(BaseModel):
id = BigIntegerField(null=False, primary_key=True, verbose_name='id')
title = CharField(max_length=255, verbose_name='帖子主题')
content = TextField(null=True, verbose_name='帖子内容')
user = ForeignKeyField(Users, null=False, related_name='posts', verbose_name='用户')
views = IntegerField(null=True, verbose_name='浏览量')
focus = IntegerField(null=True, verbose_name='关注数')
last_actived_at = DateTimeField(null=True, verbose_name='最后活动时间')
updated_at = DateTimeField(verbose_name='创建时间')
crawled_at = DateTimeField(default=datetime.now, verbose_name='抓取时间')
class Replies(BaseModel):
id = BigIntegerField(null=False, primary_key=True, verbose_name='id')
post = ForeignKeyField(Posts, related_name='replies', verbose_name='主题')
content = TextField(null=False, verbose_name='回复内容')
user = ForeignKeyField(Users, related_name='replies', verbose_name='用户')
updated_at = DateTimeField(verbose_name='创建时间')
crawled_at = DateTimeField(default=datetime.now, verbose_name='抓取时间')
class Topics(BaseModel):
id = BigIntegerField(null=False, primary_key=True, verbose_name='id')
topic = CharField(max_length=255, unique=True, null=False, verbose_name='主题')
class TopicUser(BaseModel):
user = ForeignKeyField(Users, related_name='topicuser', verbose_name='用户')
topic = ForeignKeyField(Topics, related_name='topicuser', verbose_name='主题')
approve = IntegerField(default=0, verbose_name='赞同')
thank = IntegerField(default=0, verbose_name='感谢')
class Meta:
primary_key = CompositeKey('user', 'topic')
if __name__ == '__main__':
try:
Industry.create_table()
Provs.create_table()
Users.create_table()
Posts.create_table()
Replies.create_table()
Topics.create_table()
TopicUser.create_table()
print('create table completed!')
except Exception as err:
print(err)
try:
raise Exception('NO NEED IN PRODUCT ENV!')
migrate(
migrator.add_not_null('provs', 'prov'),
migrator.drop_column('users', 'level'),
migrator.add_column('users', 'level', Users.level),
migrator.drop_column('users', 'industry'),
migrator.add_column('users', 'industry_id', Users.industry),
)
print('migration completed!')
except Exception as err:
print(err)