This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
manage.py
151 lines (118 loc) · 3.99 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
# -*- coding: utf-8 -*-
"""
manage.py
~~~~~~~~~
Description of the module goes here...
:copyright: (c) 2010 by Dan Jacob.
:license: BSD, see LICENSE for more details.
"""
import sys
import feedparser
from flask import current_app
from flaskext.script import Manager, prompt, prompt_pass, \
prompt_bool, prompt_choices
from flaskext.mail import Message
from newsmeme import create_app
from newsmeme.extensions import db, mail
from newsmeme.models import Post, User, Comment, Tag
manager = Manager(create_app)
@manager.option("-u", "--url", dest="url", help="Feed URL")
@manager.option("-n", "--username", dest="username", help="Save to user")
def importfeed(url, username):
"""
Bulk import news from a feed. For testing only !
"""
user = User.query.filter_by(username=username).first()
if not user:
print "User %s does not exist" % username
sys.exit(1)
d = feedparser.parse(url)
for entry in d['entries']:
post = Post(author=user,
title=entry.title[:200],
link=entry.link)
db.session.add(post)
db.session.commit()
@manager.option('-u', '--username', dest="username", required=False)
@manager.option('-p', '--password', dest="password", required=False)
@manager.option('-e', '--email', dest="email", required=False)
@manager.option('-r', '--role', dest="role", required=False)
def createuser(username=None, password=None, email=None, role=None):
"""
Create a new user
"""
if username is None:
while True:
username = prompt("Username")
user = User.query.filter(User.username==username).first()
if user is not None:
print "Username %s is already taken" % username
else:
break
if email is None:
while True:
email = prompt("Email address")
user = User.query.filter(User.email==email).first()
if user is not None:
print "Email %s is already taken" % email
else:
break
if password is None:
password = prompt_pass("Password")
while True:
password_again = prompt_pass("Password again")
if password != password_again:
print "Passwords do not match"
else:
break
roles = (
(User.MEMBER, "member"),
(User.MODERATOR, "moderator"),
(User.ADMIN, "admin"),
)
if role is None:
role = prompt_choices("Role", roles, resolve=int, default=User.MEMBER)
user = User(username=username,
email=email,
password=password,
role=role)
db.session.add(user)
db.session.commit()
print "User created with ID", user.id
@manager.command
def createall():
"Creates database tables"
db.create_all()
@manager.command
def dropall():
"Drops all database tables"
if prompt_bool("Are you sure ? You will lose all your data !"):
db.drop_all()
@manager.command
def mailall():
"Sends an email to all users"
subject = prompt("Subject")
message = prompt("Message")
from_address = prompt("From", default="support@thenewsmeme.com")
if prompt_bool("Are you sure ? Email will be sent to everyone!"):
with mail.connect() as conn:
for user in User.query:
message = Message(subject=subject,
body=message,
sender=from_address,
recipients=[user.email])
conn.send(message)
@manager.shell
def make_shell_context():
return dict(app=current_app,
db=db,
Post=Post,
User=User,
Tag=Tag,
Comment=Comment)
manager.add_option('-c', '--config',
dest="config",
required=False,
help="config file")
if __name__ == "__main__":
manager.run()