generated from uwidcit/flask-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
141 lines (121 loc) · 5.32 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
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from main import app
from models import db
manager = Manager(app)
migrate = Migrate(app, db)
from models import *
manager.add_command('db', MigrateCommand)
# initDB command
@manager.command
def initDB():
db.create_all(app=app)
clubs = []
clubs.append(Club(clubName="UWI Computing Society", clubDescription="The University of the West Indies Computing Society welcomes all who wish to partake in the discussion of new and innovative technologies of the modern world."))
clubs.append(Club(clubName="UWI Biological Society", clubDescription="The University of the West Indies Biological Society is an environmental conservation organization founded in 1987, STA UWI."))
clubs.append(Club(clubName="UWI Law Society", clubDescription="The University of the West Indies Law Society provides a forum for members to discuss prevalent issues that plague the judicial systems across the globe."))
clubs.append(Club(clubName="UWI Charity and Outreach Society", clubDescription="The University of the West Indies Law Society's Charity and Outreach Committee devotes to ensuring each student has an extraordinary and memorable university volunteer experience."))
for club in clubs:
try:
db.session.add(club)
db.session.commit()
except:
db.session.rollback()
print("Club already exists in database!")
print('Database Initialized!')
# serve command
@manager.command
def serve():
print('Application running in ' +app.config['ENV'] + ' mode')
app.run(host='0.0.0.0', port = 8080, debug = app.config['ENV']=='development')
@manager.command
def addClub(clubName, clubDescription, clubImage):
if clubImage == "none":
clubImage = None
if clubName and clubDescription:
newClub = Club(clubName=clubName, clubDescription=clubDescription, clubImage=clubImage)
try:
db.session.add(newClub)
db.session.commit()
print("Successfully added club to database!")
except:
db.session.rollback()
print("Unable to add club to database!")
@manager.command
def editClubDescription(clubName, clubDescription):
if clubName:
newClub = db.session.query(Club).filter_by(clubName=clubName).first()
if not newClub:
return "Unable to find club by that name."
if clubDescription:
newClub.clubDescription = clubDescription
try:
db.session.add(newClub)
db.session.commit()
print("Successfully updated club description!")
except:
db.session.rollback()
print("Unable to update club description!")
else:
print("No club name provided!")
@manager.command
def editClubImage(clubName, clubImage):
if clubName:
newClub = db.session.query(Club).filter_by(clubName=clubName).first()
if not newClub:
return "Unable to find club by that name."
if clubImage:
newClub.clubImage = clubImage
try:
db.session.add(newClub)
db.session.commit()
print("Successfully updated club image!")
except:
db.session.rollback()
print("Unable to update club image!")
else:
print("No description provided for club!")
else:
print("No club name provided!")
@manager.command
def removeClub(clubName):
if clubName:
deletionClub = db.session.query(Club).filter_by(clubName=clubName).first()
if deletionClub:
elections = db.session.query(Election).filter_by(clubID=deletionClub.clubID).all()
if elections:
for election in elections:
try:
db.session.delete(election)
db.session.commit()
print("Successfully deleted election from database!")
except:
db.session.rollback()
print("Unable to delete election from database!")
clubMembers = db.session.query(ClubMember).filter_by(clubID=deletionClub.clubID).all()
if clubMembers:
for clubMember in clubMembers:
try:
db.session.delete(clubMember)
db.session.commit()
print("Successfully deleted club member from database!")
except:
db.session.rollback()
print("Unable to delete club member from database!")
try:
db.session.delete(deletionClub)
db.session.commit()
print("Successfully removed club from database!")
except:
db.session.rollback()
print("Unable to remove club from database!")
else:
print("Unable to find club in database!")
else:
print("No club name provided!")
@manager.command
def resetDatabase():
pass
print("Unable to clear database completely!")
if __name__ == "__main__":
manager.run()