| 1 |
"""
|
| 2 |
|
| 3 |
Dash.py
|
| 4 |
|
| 5 |
peewee ORM model for the MCP and flask app
|
| 6 |
|
| 7 |
Created by
|
| 8 |
Emanuel Borges
|
| 9 |
07.15.2016
|
| 10 |
|
| 11 |
"""
|
| 12 |
|
| 13 |
import os,sys
|
| 14 |
|
| 15 |
from peewee import *
|
| 16 |
from flask import Flask
|
| 17 |
from flask import url_for, abort, render_template, flash,request, redirect, url_for
|
| 18 |
|
| 19 |
from MCP2.Trunk import MCP2
|
| 20 |
#import MCP2
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
app = Flask(__name__)
|
| 25 |
|
| 26 |
# create a peewee database instance -- our models will use this database to
|
| 27 |
# persist information
|
| 28 |
if sys.platform == "darwin":
|
| 29 |
database = SqliteDatabase(r'/Users/ninoborges/Dropbox/Documents/Advanced Discovery/MCP2/MCP2.db')
|
| 30 |
else:
|
| 31 |
database = SqliteDatabase(r'C:\Users\Emanual Borges\Dropbox\Advanced Discovery\MCP2\MCP2.db')
|
| 32 |
app.config.update(SECRET_KEY='jdskhaskdufky829209skjhasf-00--1==-9988*(!Q&(#$(&!#$%^')
|
| 33 |
|
| 34 |
|
| 35 |
# model definitions -- the standard "pattern" is to define a base model class
|
| 36 |
# that specifies which database to use. then, any subclasses will automatically
|
| 37 |
# use the correct storage.
|
| 38 |
class BaseModel(Model):
|
| 39 |
class Meta:
|
| 40 |
database = database
|
| 41 |
|
| 42 |
|
| 43 |
class Client(BaseModel):
|
| 44 |
name = CharField()
|
| 45 |
local_path = CharField()
|
| 46 |
#birthday = DateField()
|
| 47 |
#is_relative = BooleanField()
|
| 48 |
|
| 49 |
class Meta:
|
| 50 |
order_by = ('name',)
|
| 51 |
|
| 52 |
class Project(BaseModel):
|
| 53 |
client = ForeignKeyField(Client, related_name='projects')
|
| 54 |
name = CharField()
|
| 55 |
status = CharField() # Choices should only be dormant, active and executed
|
| 56 |
sales_person = CharField(null = True)
|
| 57 |
pts = CharField(null = True)
|
| 58 |
project_manager = CharField(null = True)
|
| 59 |
pm_team = CharField(null = True)
|
| 60 |
jira_number = CharField(null = True)
|
| 61 |
local_path = CharField()
|
| 62 |
#animal_type = CharField()
|
| 63 |
|
| 64 |
class Demo(BaseModel):
|
| 65 |
client = ForeignKeyField(Client, related_name='demos')
|
| 66 |
demo_date = DateTimeField()
|
| 67 |
sales_person = CharField()
|
| 68 |
demo_type = CharField()
|
| 69 |
pre_notes = TextField(null = True)
|
| 70 |
post_notes = TextField(null = True)
|
| 71 |
jira_number = CharField(null = True)
|
| 72 |
|
| 73 |
class TimeEntry(BaseModel):
|
| 74 |
project = ForeignKeyField(Project, related_name='project_time_entries', null = True)
|
| 75 |
demo = ForeignKeyField(Demo, related_name='demo_time_entries', null = True)
|
| 76 |
time_entry_date = DateTimeField()
|
| 77 |
entry_amount = CharField()
|
| 78 |
entry = TextField()
|
| 79 |
status = CharField() # Entered or Not Entered only (default is Not Entered)
|
| 80 |
|
| 81 |
|
| 82 |
def create_tables():
|
| 83 |
database.connect()
|
| 84 |
database.create_tables([Client, Project, Demo, TimeEntry],True)
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#############
|
| 89 |
|
| 90 |
|
| 91 |
@app.before_request
|
| 92 |
def before_request():
|
| 93 |
database.connect()
|
| 94 |
|
| 95 |
@app.after_request
|
| 96 |
def after_request(response):
|
| 97 |
database.close()
|
| 98 |
return response
|
| 99 |
|
| 100 |
@app.route("/")
|
| 101 |
def MainPage():
|
| 102 |
activeInternalProjectsQuery = (Project.select(Project, Client).join(Client).where((Project.status=='Active') & (Client.name=='Internal Projects')).order_by(Client.name))
|
| 103 |
activeClientProjectsQuery = (Project.select(Project, Client).join(Client).where((Project.status=='Active') & ~(Client.name=='Internal Projects')).order_by(Client.name))
|
| 104 |
dormantProjectsQuery = (Project.select(Project, Client).join(Client).where(Project.status=='Dormant'))
|
| 105 |
executedProjectsQuery = (Project.select(Project, Client).join(Client).where(Project.status=='Executed'))
|
| 106 |
|
| 107 |
return render_template('index.html', activeInternalProjects = activeInternalProjectsQuery, activeClientProjects = activeClientProjectsQuery, dormantProjects = dormantProjectsQuery,executedProjects = executedProjectsQuery)
|
| 108 |
|
| 109 |
@app.route("/Detail/<projId>")
|
| 110 |
def ProjectDetailPage(projId):
|
| 111 |
"""The page for just that 1 project"""
|
| 112 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Project.id==projId))
|
| 113 |
for x in singleProjectQuery:
|
| 114 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 115 |
singleProject = x
|
| 116 |
projectTimeEntriesQuery = (TimeEntry.select(TimeEntry, Project).join(Project).where(Project.name==singleProject.name))
|
| 117 |
return render_template('project_detail.html', singleProject = singleProject, projectTimeEntries = projectTimeEntriesQuery)
|
| 118 |
|
| 119 |
@app.route("/ProjectUpdate/<projId>")
|
| 120 |
def ProjectUpdatePage(projId):
|
| 121 |
"""This page allows you to update parts of the project"""
|
| 122 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Project.id==projId))
|
| 123 |
for x in singleProjectQuery:
|
| 124 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 125 |
singleProject = x
|
| 126 |
return render_template('project_update.html', singleProject = singleProject)
|
| 127 |
|
| 128 |
@app.route("/UpdateProjectInformation/<projId>", methods=['POST'])
|
| 129 |
def UpdateProjectInformation(projId):
|
| 130 |
"""Callback for updating project info"""
|
| 131 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Project.id==projId))
|
| 132 |
for x in singleProjectQuery:
|
| 133 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 134 |
singleProject = x
|
| 135 |
if request.form['sales_person'] != "None":
|
| 136 |
singleProject.sales_person = request.form['sales_person']
|
| 137 |
if request.form['pts_number'] != "None":
|
| 138 |
singleProject.pts = request.form['pts_number']
|
| 139 |
if request.form['jira_number'] != "None":
|
| 140 |
singleProject.jira_number = request.form['jira_number']
|
| 141 |
if request.form['project_manager'] != "None":
|
| 142 |
singleProject.project_manager = request.form['project_manager']
|
| 143 |
if request.form['pm_team'] != "None":
|
| 144 |
singleProject.pm_team = request.form['pm_team']
|
| 145 |
|
| 146 |
singleProject.save()
|
| 147 |
flash('Project has been updated')
|
| 148 |
return redirect(url_for('ProjectDetailPage',projId=projId))
|
| 149 |
|
| 150 |
@app.route("/OpenProjectFolder/<projId>", methods=['POST'])
|
| 151 |
def OpenProjectFolder(projId):
|
| 152 |
"""Opens the project folder for this project"""
|
| 153 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Project.id==projId))
|
| 154 |
for x in singleProjectQuery:
|
| 155 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 156 |
singleProject = x
|
| 157 |
MCP2Lib = MCP2.Console()
|
| 158 |
MCP2Lib.OpenProjectFolder(singleProject.client.name,singleProject.name,caseProjectPath = singleProject.local_path)
|
| 159 |
return ('',204)
|
| 160 |
|
| 161 |
|
| 162 |
@app.route("/AddProjectPage/")
|
| 163 |
@app.route("/AddProjectPage/<clientId>")
|
| 164 |
def AddProjectPage(clientId = None):
|
| 165 |
"""Page that allows you to add just a project or a client and project"""
|
| 166 |
if clientId:
|
| 167 |
## They want to add a project to this client
|
| 168 |
singleClientQuery = (Client.select(Client).where(Client.id==clientId))
|
| 169 |
for x in singleClientQuery:
|
| 170 |
## There should only be 1 client in there, so this should work but rewrite this
|
| 171 |
singleClient = x
|
| 172 |
|
| 173 |
else:
|
| 174 |
## They want to add both a client and project
|
| 175 |
singleClient = None
|
| 176 |
return render_template('add_project.html', singleClient = singleClient)
|
| 177 |
|
| 178 |
@app.route("/AddProject/", methods=['POST'])
|
| 179 |
@app.route("/AddProject/<clientId>", methods=['POST'])
|
| 180 |
def AddProject(clientId = None):
|
| 181 |
"""Adds a new project to an existing client or client and project"""
|
| 182 |
projectName = request.form['project_name']
|
| 183 |
if clientId:
|
| 184 |
## They are asking for a new project only
|
| 185 |
clientProjectQuery = (Project.select(Project, Client).join(Client).where(Client.id==clientId))
|
| 186 |
## check to see if the project name is taken
|
| 187 |
projectNameExists = False
|
| 188 |
for p in clientProjectQuery:
|
| 189 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 190 |
if p.name == projectName:
|
| 191 |
## Project name already taken. make an error for this one day
|
| 192 |
projectNameExists = True
|
| 193 |
else:
|
| 194 |
pass
|
| 195 |
if projectNameExists:
|
| 196 |
return redirect(url_for('MainPage'))
|
| 197 |
else:
|
| 198 |
MCP2Lib = MCP2.Console()
|
| 199 |
newProjectPath,errCode = MCP2Lib.AddProject(p.client.name,projectName)
|
| 200 |
if errCode == 0:
|
| 201 |
newProject = Project.create(client=p.client, name=projectName, status='Active',local_path = newProjectPath)
|
| 202 |
newProject.save()
|
| 203 |
return redirect(url_for('ProjectDetailPage',projId=newProject.id))
|
| 204 |
else:
|
| 205 |
## They are asking for both client and project
|
| 206 |
clientName = request.form['client_name']
|
| 207 |
## Check to see if the client already exists
|
| 208 |
clientNameExists = False
|
| 209 |
projectNameExists = False
|
| 210 |
singleClientQuery = (Client.select(Client).where(Client.name==clientName))
|
| 211 |
for singleClient in singleClientQuery:
|
| 212 |
## There should only be 1 client in there, so this should work but rewrite this
|
| 213 |
if clientName == singleClient.name:
|
| 214 |
clientNameExists = True
|
| 215 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Client.name==clientName))
|
| 216 |
for singleProject in singleProjectQuery:
|
| 217 |
## check to see if the project name is taken
|
| 218 |
if singleProject.name == projectName:
|
| 219 |
projectNameExists = True
|
| 220 |
if clientNameExists:
|
| 221 |
if projectNameExists:
|
| 222 |
## Both the client name and the project name exists, so error out.
|
| 223 |
return redirect(url_for('MainPage'))
|
| 224 |
else:
|
| 225 |
## Client name exists but the project name is okay
|
| 226 |
singleClientQuery = (Client.select(Client).where(Client.name==clientName))
|
| 227 |
for c in singleClientQuery:
|
| 228 |
singleClient = c
|
| 229 |
|
| 230 |
MCP2Lib = MCP2.Console()
|
| 231 |
newProjectPath,errCode = MCP2Lib.AddProject(singleClient.name,projectName)
|
| 232 |
if errCode == 0:
|
| 233 |
newProject = Project.create(client = singleClient, name=projectName, status='Active',local_path = newProjectPath)
|
| 234 |
newProject.save()
|
| 235 |
return redirect(url_for('ProjectDetailPage',projId=newProject.id))
|
| 236 |
else:
|
| 237 |
## This client doenst exist, so make both
|
| 238 |
MCP2Lib = MCP2.Console()
|
| 239 |
newProjectPath,errCode = MCP2Lib.AddClient(clientName,projectName)
|
| 240 |
if errCode == 0:
|
| 241 |
newClient = Client.create(name=clientName, local_path = os.path.split(newProjectPath)[0])
|
| 242 |
newClient.save()
|
| 243 |
newProject = Project.create(client = newClient, name=projectName, status='Active',local_path = newProjectPath)
|
| 244 |
newProject.save()
|
| 245 |
return redirect(url_for('ProjectDetailPage',projId=newProject.id))
|
| 246 |
|
| 247 |
def AddDemo(clientName, demoType, demoTime):
|
| 248 |
"""Adds a new demo entry to a client"""
|
| 249 |
pass
|
| 250 |
|
| 251 |
@app.route("/MakeProjectExecuted/<projId>", methods=['POST'])
|
| 252 |
def MakeProjectExecuted(projId):
|
| 253 |
"""Makes this project dormant by changing values in db and calling MCP2 to move the dirs"""
|
| 254 |
## Look up the project name and client name.
|
| 255 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Project.id==projId))
|
| 256 |
for x in singleProjectQuery:
|
| 257 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 258 |
singleProject = x
|
| 259 |
clientName = singleProject.client.name
|
| 260 |
projectName = singleProject.name
|
| 261 |
MCP2Lib = MCP2.Console()
|
| 262 |
newProjectPath,err = MCP2Lib.MakeProjectExecuted(clientName,projectName)
|
| 263 |
if err:
|
| 264 |
pass
|
| 265 |
else:
|
| 266 |
singleProject.status="Executed"
|
| 267 |
singleProject.local_path = newProjectPath
|
| 268 |
singleProject.save()
|
| 269 |
return ('',204)
|
| 270 |
|
| 271 |
@app.route("/MakeProjectDormant/<projId>", methods=['POST'])
|
| 272 |
def MakeProjectDormant(projId):
|
| 273 |
"""Makes this project dormant by changing values in db and calling MCP2 to move the dirs"""
|
| 274 |
singleProjectQuery = (Project.select(Project, Client).join(Client).where(Project.id==projId))
|
| 275 |
for x in singleProjectQuery:
|
| 276 |
## There should only be 1 project in there, so this should work but rewrite this
|
| 277 |
singleProject = x
|
| 278 |
clientName = singleProject.client.name
|
| 279 |
projectName = singleProject.name
|
| 280 |
MCP2Lib = MCP2.Console()
|
| 281 |
newProjectPath,err = MCP2Lib.MakeProjectDormant(clientName,projectName)
|
| 282 |
if err:
|
| 283 |
pass
|
| 284 |
else:
|
| 285 |
singleProject.status="Dormant"
|
| 286 |
singleProject.local_path = newProjectPath
|
| 287 |
singleProject.save()
|
| 288 |
return ('',204)
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
| 291 |
app.run(debug = True)
|
| 292 |
|
| 293 |
|