This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·138 lines (115 loc) · 3.89 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
#!/usr/bin/env python
import os
import datetime
import subprocess
from flask.ext.script import Manager, Shell, Server
#from application import app
from application.models import *
manager = Manager(app)
manager.add_command("runserver",Server(host='0.0.0.0'))
manager.add_command("shell",Shell())
def random_password():
return ''.join(random.choice("abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345678") for _ in range(8))
@manager.command
def createdb():
"""
Create the database.
"""
db.create_all()
@manager.command
@manager.option('-u', '--username', help='Username')
def clearuserdata(username):
"""
Will clear all data for a given user!!! Use with care!!!
"""
user=User.query.filter(User.username==username).one()
print'* User and Data deleted'
@manager.command
def backup():
"""
Creates a backup of the entire site and database.
"""
_datadir=app.config["DATADIR"]
backupfile=os.path.join(_datadir,"backup","fwo_backup_"+datetime.datetime.now().strftime("%Y%m%d_%H%M%S")+".tar.gz")
c=[
'/bin/tar',
'zcf',
backupfile,
'-C',app.config["DATADIR"],
'campaigns',
'fieldwork.sqlite'
]
rc=subprocess.call(c)
if(rc==0):
print " * Backup ok! Saved to %s"%(backupfile)
else:
print " * Something went wrong! Tar command returned code %d"%(rc)
@manager.command
def initdb():
"""
Initialize the database with default data.
* Add the administrator, supervisor, and student roles
* Create an admin, supervisor, and student user
* The random passwords are stored in install.txt in the data directory
"""
pw_admin=random_password()
pw_supervisor=random_password()
pw_student=random_password()
db.session.add_all([
Role(name="administrator"),
Role(name="supervisor"),
Role(name="student")
])
db.session.commit()
flash("Created <code>administrator</code>, <code>supervisor</code> and <code>student</code> roles.","ok")
admin = User(username='admin', fullname='Site Admin', email='[email protected]', active=True, password=user_manager.hash_password(pw_admin))
admin.roles.append(Role.query.filter(Role.name=='administrator').first())
admin.roles.append(Role.query.filter(Role.name=='supervisor').first())
supervisor = User(username='supervisor', fullname='Site Supervisor', email='[email protected]', active=True, password=user_manager.hash_password(pw_supervisor))
supervisor.roles.append(Role.query.filter(Role.name=='supervisor').first())
student = User(username='student', fullname='Sam Student', email='[email protected]', active=True, password=user_manager.hash_password(pw_student))
student.roles.append(Role.query.filter(Role.name=='student').first())
db.session.add_all([admin,supervisor,student])
db.session.commit()
campaign = Campaign(name="Demo Project",description="A fieldwork campaign for demonstration purposes. This project showcases basic functionality and lets you try out the interface.")
campaign.users.append(admin)
db.session.add(campaign)
db.session.commit()
print " * Created default users, roles, and demo project."
print " * User credentials (you way want to write these down, it is the only time they're visible):"
print " - Administrator: admin/%s"%(pw_admin)
print " - Supervisor: supervisor/%s"%(pw_student)
print " - Student: student/%s"%(pw_student)
@manager.command
def dropdb():
"""
Drop the database. Use with care!
"""
db.drop_all()
@manager.command
def purgedb():
"""
Drop and create the database. Use with care!
"""
dropdb()
createdb()
initdb()
@manager.command
def createblueprint():
"""
Add a new blueprint to this project.
"""
pass
@manager.command
@manager.option('-u', '--username', help='Username')
def reset_password(username):
"""
Hard reset of user password
"""
user=User.query.filter(User.username==username).one()
pw_user=random_password()
user.password=user_manager.hash_password(pw_user)
db.session.commit()
print "New password for %s: %s"%(username,pw_user)
if __name__=="__main__":
manager.run()