This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·169 lines (130 loc) · 4.27 KB
/
manage.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
"""
UniveCalendar manager
Usage Example
-------------
> python manage.py runserver
"""
import os
import sys
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Manager
from app import create_app, db, models, bot
app = create_app(os.getenv('FLASK_CONFIG') or 'development')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
@bot.message_handler(commands=['start', 'help'])
def connect_user(msg):
token = msg.text.split()[1] if len(msg.text.split()) > 1 else None
if token:
with app.app_context():
u = models.User.load_user_from_unique_code(token)
if u:
u.telegram_chat_id = msg.chat.id
db.session.commit()
reply = "Congratulations {0}, you successfully connect your account.".format(u.username)
else:
reply = "I have no clue who you are..."
else:
reply = """
Hi! I'm UniveCalendar Bot. I can notify you about events of
your university's calendar. Visit our page for more info.
Available Commands
/start <TOKEN> - Authorize bot
/stop - Disconnect your connect
"""
bot.reply_to(msg, reply)
@bot.message_handler(commands=['stop'])
def stop(msg):
with app.app_context():
u = models.User.query.filter(models.User.telegram_chat_id == str(msg.chat.id)).first()
if u:
u.telegram_chat_id = None
db.session.commit()
reply = "Ok, I will not send you any notifications."
else:
reply = "I have no clue who you are..."
bot.reply_to(msg, reply)
@manager.command
def runbot():
import logging
from telebot import logger
logger.setLevel(logging.INFO)
bot.polling()
@manager.command
def cleardb():
"""Delete all tables records."""
for table in reversed(db.metadata.sorted_tables):
db.session.execute(table.delete())
db.session.commit()
@manager.command
def cleartable(tablename):
"""Delete all tables records."""
table = db.metadata.tables[tablename]
db.session.execute(table.delete())
db.session.commit()
@manager.command
def insertfeed(professor_id):
import forgery_py
p = models.Professor.query.get(professor_id)
f = models.Feed(title=forgery_py.lorem_ipsum.title(),
body=forgery_py.lorem_ipsum.paragraphs(2),
author=p)
db.session.add(f)
db.session.commit()
@manager.command
def delaylesson(lesson_id, hours=1):
from datetime import timedelta
l = models.Lesson.query.get(lesson_id)
l.start += timedelta(hours=hours)
l.end += timedelta(hours=hours)
db.session.commit()
@manager.command
def addusers():
u = models.User(email='[email protected]',
username='bobby85',
confirmed=True,
password='123456')
db.session.add(u)
db.session.commit()
u = models.User(email='[email protected]',
username='alice92',
confirmed=True,
password='123456')
db.session.add(u)
db.session.commit()
@manager.command
def dbcreate():
os.system('python manage.py db init')
os.system('python manage.py db migrate')
os.system('python manage.py db upgrade')
@manager.command
def trcompile():
if sys.platform == 'win32':
pybabel = 'flask\\Scripts\\pybabel'
else:
pybabel = 'pybabel'
os.system(pybabel + ' compile -d app/translations')
@manager.command
def trupdate():
if sys.platform == 'win32':
pybabel = 'flask\\Scripts\\pybabel'
else:
pybabel = 'pybabel'
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app')
os.system(pybabel + ' update -i messages.pot -d app/translations')
os.unlink('messages.pot')
@manager.command
def trinit():
if sys.platform == 'win32':
pybabel = 'flask\\Scripts\\pybabel'
else:
pybabel = 'pybabel'
if len(sys.argv) != 2:
print "usage: tr_init <language-code>"
sys.exit(1)
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app')
os.system(pybabel + ' init -i messages.pot -d app/translations -l ' + sys.argv[1])
os.unlink('messages.pot')
if __name__ == '__main__':
manager.run()