|
| 1 | +from flask import Flask, render_template, request, make_response, redirect; |
| 2 | +from user import User; |
| 3 | +import sqlite3; |
| 4 | +from time import strftime; |
| 5 | +import md5; |
| 6 | + |
| 7 | +app = Flask( __name__ ); |
| 8 | +app.debug = True; |
| 9 | + |
| 10 | +user = User(); |
| 11 | + |
| 12 | +connection = sqlite3.connect( 'data.db' ); |
| 13 | +cursor = connection.cursor(); |
| 14 | + |
| 15 | +# ... # |
| 16 | + |
| 17 | +def isInstalled(): |
| 18 | + cursor.execute( "SELECT COUNT( 1 ) FROM USERS" ); |
| 19 | + hasUser = cursor.fetchone()[ 0 ]; |
| 20 | + |
| 21 | + return ( hasUser > 0 ); |
| 22 | + |
| 23 | +def addRequest( reqType ): |
| 24 | + creationDate = strftime( '%d-%m-%Y %H:%M:%S' ); |
| 25 | + |
| 26 | + cursor.execute( "INSERT INTO UPDATE_REQUESTS( UPDATE_TYPE, DONE, CREATION_DATE ) VALUES ( ?, 'N', ? )", [ reqType, creationDate ] ); |
| 27 | + |
| 28 | + connection.commit(); |
| 29 | + |
| 30 | +@app.before_request |
| 31 | +def beforeRequest(): |
| 32 | + if request.path == '/install': |
| 33 | + return None; |
| 34 | + |
| 35 | + if not isInstalled(): |
| 36 | + return render_template( 'install_prettypi.html' ); |
| 37 | + |
| 38 | + if request.path == '/login': |
| 39 | + return None; |
| 40 | + |
| 41 | + if request.cookies.get( 'prettypi_username' ) != None: |
| 42 | + user.setUsername( request.cookies.get( 'prettypi_username' ) ); |
| 43 | + user.setHashedPassword( request.cookies.get( 'prettypi_password' ) ); |
| 44 | + |
| 45 | + if not user.hasPermission(): |
| 46 | + return render_template( 'login.html' ); |
| 47 | + else: |
| 48 | + return None; |
| 49 | + |
| 50 | + return render_template( 'login.html' ); |
| 51 | + |
| 52 | +# ... # |
| 53 | + |
| 54 | +@app.route( '/' ) |
| 55 | +def main(): |
| 56 | + return render_template( 'main_page.html', name = user.getName() ); |
| 57 | + |
| 58 | +# ... # |
| 59 | + |
| 60 | +@app.route( '/install', methods = [ 'POST' ] ) |
| 61 | +def install(): |
| 62 | + if isInstalled(): |
| 63 | + return render_template( 'installer_message.html', message = 'PrettyPi Already Installed', type = 'danger' ); |
| 64 | + |
| 65 | + if not request.form[ 'username' ] or not request.form[ 'password' ] or not request.form[ 'name' ]: |
| 66 | + return render_template( 'installer_message.html', message = 'Please fill all fields', type = 'danger' ); |
| 67 | + |
| 68 | + hashFunction = md5.new(); |
| 69 | + hashFunction.update( request.form[ 'password' ] ); |
| 70 | + |
| 71 | + hashedPassword = hashFunction.hexdigest(); |
| 72 | + |
| 73 | + cursor.execute( "INSERT INTO USERS( USER_ID, USERNAME, PASSWORD, NAME ) VALUES ( NULL, ?, ?, ? )", [ request.form[ 'username' ], hashedPassword, request.form[ 'name' ] ] ); |
| 74 | + |
| 75 | + connection.commit(); |
| 76 | + |
| 77 | + return render_template( 'installer_message.html', message = 'Congratulations! PrettyPi has been initialized. Go back to homepage to start using it', type = 'success' ); |
| 78 | + |
| 79 | +# ... # |
| 80 | + |
| 81 | +@app.route( '/login', methods = [ 'POST' ] ) |
| 82 | +def login(): |
| 83 | + user.setUsername( request.form[ 'username' ] ); |
| 84 | + user.setPassword( request.form[ 'password' ] ); |
| 85 | + |
| 86 | + if user.hasPermission(): |
| 87 | + response = make_response( redirect( '/' ) ); |
| 88 | + response.set_cookie( 'prettypi_username', user.getUsername() ); |
| 89 | + response.set_cookie( 'prettypi_password', user.getHashedPassword() ); |
| 90 | + |
| 91 | + return response; |
| 92 | + else: |
| 93 | + return 'Invalid'; |
| 94 | + |
| 95 | +# ... # |
| 96 | + |
| 97 | +@app.route( '/logout' ) |
| 98 | +def logout(): |
| 99 | + response = make_response( redirect( '/' ) ); |
| 100 | + response.set_cookie( 'prettypi_username', '' ); |
| 101 | + response.set_cookie( 'prettypi_password', '' ); |
| 102 | + |
| 103 | + return response; |
| 104 | + |
| 105 | +# ... # |
| 106 | + |
| 107 | +@app.route( '/tasks' ) |
| 108 | +def tasksMain(): |
| 109 | + cursor.execute( "SELECT * FROM TODO WHERE DONE = 'N' ORDER BY CREATION_DATE DESC" ); |
| 110 | + |
| 111 | + tasks = cursor.fetchall(); |
| 112 | + |
| 113 | + # ... # |
| 114 | + |
| 115 | + cursor.execute( "SELECT * FROM TODO WHERE DONE = 'Y' ORDER BY CREATION_DATE DESC" ); |
| 116 | + |
| 117 | + doneTasks = cursor.fetchall(); |
| 118 | + |
| 119 | + # ... # |
| 120 | + |
| 121 | + cursor.execute( "SELECT * FROM TODO WHERE WORKING_ON = 'Y'" ); |
| 122 | + |
| 123 | + workingTask = cursor.fetchone(); |
| 124 | + |
| 125 | + print workingTask; |
| 126 | + |
| 127 | + # ... # |
| 128 | + |
| 129 | + return render_template( 'tasks.html', tasks = tasks, doneTasks = doneTasks, name = user.getName(), workingTask = workingTask ); |
| 130 | + |
| 131 | +# ... # |
| 132 | + |
| 133 | +@app.route( '/add_task', methods = [ 'POST' ] ) |
| 134 | +def newTask(): |
| 135 | + if not request.form[ 'task_details' ]: |
| 136 | + return render_template( 'message.html', message = 'The details should not be empty', type = 'warning' ); |
| 137 | + |
| 138 | + creationDate = strftime( '%d-%m-%Y %H:%M:%S' ); |
| 139 | + |
| 140 | + cursor.execute( "INSERT INTO TODO( TASK_ID, TASK, CREATION_DATE ) VALUES ( NULL, ?, ? )", [ request.form[ 'task_details' ], creationDate ] ); |
| 141 | + |
| 142 | + connection.commit(); |
| 143 | + |
| 144 | + addRequest( "UPDATE_TODO_LIST" ); |
| 145 | + |
| 146 | + return redirect( 'tasks' ); |
| 147 | + |
| 148 | +# ... # |
| 149 | + |
| 150 | +@app.route( '/delete_task', methods = [ 'GET' ] ) |
| 151 | +def deleteTask(): |
| 152 | + taskId = request.args.get( 'task_id', None ); |
| 153 | + |
| 154 | + if taskId is None: |
| 155 | + return 'Invalid'; |
| 156 | + |
| 157 | + cursor.execute( "DELETE FROM TODO WHERE TASK_ID = ?", [ taskId ] ); |
| 158 | + cursor.execute( "DELETE FROM TASKS_LOG WHERE TASK_ID = ?", [ taskId ] ); |
| 159 | + |
| 160 | + connection.commit(); |
| 161 | + |
| 162 | + addRequest( "UPDATE_TODO_LIST" ); |
| 163 | + |
| 164 | + return redirect( 'tasks' ); |
| 165 | + |
| 166 | +@app.route( '/task_done', methods = [ 'GET' ] ) |
| 167 | +def markTaskAsDone(): |
| 168 | + taskId = request.args.get( 'task_id', None ); |
| 169 | + |
| 170 | + if taskId is None: |
| 171 | + return 'Invalid'; |
| 172 | + |
| 173 | + currentDate = strftime( '%d-%m-%Y %H:%M:%S' ); |
| 174 | + |
| 175 | + cursor.execute( "UPDATE TODO SET WORKING_ON = 'N', DONE = 'Y', DONE_AT = ? WHERE TASK_ID = ?", [ currentDate, taskId ] ); |
| 176 | + |
| 177 | + connection.commit(); |
| 178 | + |
| 179 | + addRequest( "UPDATE_TODO_LIST" ); |
| 180 | + |
| 181 | + return redirect( 'tasks' ); |
| 182 | + |
| 183 | +@app.route( '/start_task', methods = [ 'GET' ] ) |
| 184 | +def startWorkingOnTask(): |
| 185 | + taskId = request.args.get( 'task_id', None ); |
| 186 | + |
| 187 | + if taskId is None: |
| 188 | + return 'Invalid'; |
| 189 | + |
| 190 | + # ... # |
| 191 | + |
| 192 | + cursor.execute( "SELECT COUNT( 1 ) FROM TODO WHERE WORKING_ON = 'Y' AND TASK_ID <> ?", [ taskId ] ); |
| 193 | + |
| 194 | + currentlyWorkingOn = cursor.fetchone()[ 0 ]; |
| 195 | + |
| 196 | + if currentlyWorkingOn > 0: |
| 197 | + return render_template( 'message.html', message = "You're already working on another task!", type = 'warning' ); |
| 198 | + |
| 199 | + # ... # |
| 200 | + |
| 201 | + creationDate = strftime( '%d-%m-%Y %H:%M:%S' ); |
| 202 | + |
| 203 | + cursor.execute( "UPDATE TODO SET WORKING_ON = 'Y' WHERE TASK_ID = ?", [ taskId ] ); |
| 204 | + cursor.execute( "INSERT INTO TASKS_LOG( TASK_ID, START_AT ) VALUES ( ?, ? )", [ taskId, creationDate ] ); |
| 205 | + |
| 206 | + connection.commit(); |
| 207 | + |
| 208 | + # ... # |
| 209 | + |
| 210 | + addRequest( "UPDATE_TODO_LIST" ); |
| 211 | + |
| 212 | + return redirect( 'tasks' ); |
| 213 | + |
| 214 | +# ... # |
| 215 | + |
| 216 | +@app.route( '/stop_task', methods = [ 'GET' ] ) |
| 217 | +def stopWorkingOnTask(): |
| 218 | + taskId = request.args.get( 'task_id', None ); |
| 219 | + |
| 220 | + if taskId is None: |
| 221 | + return 'Invalid'; |
| 222 | + |
| 223 | + # ... # |
| 224 | + |
| 225 | + cursor.execute( "SELECT MAX( log_id ) FROM tasks_log WHERE TASK_ID = ? AND ENDED_AT IS NULL", [ taskId ] ); |
| 226 | + |
| 227 | + currTaskLogId = cursor.fetchone()[ 0 ]; |
| 228 | + |
| 229 | + # ... # |
| 230 | + |
| 231 | + currentDate = strftime( '%d-%m-%Y %H:%M:%S' ); |
| 232 | + |
| 233 | + cursor.execute( "UPDATE TODO SET WORKING_ON = 'N' WHERE TASK_ID = ?", [ taskId ] ); |
| 234 | + cursor.execute( "UPDATE TASKS_LOG SET ENDED_AT = ? WHERE LOG_ID = ?", [ currentDate, currTaskLogId ] ); |
| 235 | + |
| 236 | + connection.commit(); |
| 237 | + |
| 238 | + # ... # |
| 239 | + |
| 240 | + addRequest( "UPDATE_TODO_LIST" ); |
| 241 | + |
| 242 | + return redirect( 'tasks' ); |
0 commit comments