Rapportsystem API - of D03N
Hovedprosjekt FiV Programmering 20-24
Loading...
Searching...
No Matches
userInfo.py
Go to the documentation of this file.
1from flask_restx import Resource
2from flask_jwt_extended import jwt_required, get_jwt_identity
3from mysql.connector import Error
4#imports os
5import os
6current_directory = os.getcwd()
7#imports sys
8import sys
9sys.path.append(os.path.join(current_directory))
10
11from SQLAdminConnections import SQL_AdminConnector as SQLC
12from SQLAdminConnections import SQL_AdminQuerys as SQLQ
13from Common.Requirements import valid_token as vt
14from flask import jsonify
15
16#defines activity route
18 @ns.route('/user/info')
19 class Information(Resource):
20 @ns.doc('userInfo',
21 description='Info route, returns json with user information.',
22 responses={200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error'})
23
24 #Requires valid jwt token
25 @jwt_required()
26 @vt.require_valid_token
27
28 def get(self):
29 # gets the current user
30 current_user = get_jwt_identity()
31 # connects to the database
32 connection = SQLC.SQLConAdmin()
33 connection.connect()
34 connection.execute_query(SQLQ.SQLQueries.use_users_database())
35 # gets the user information - From adminQuerry
36 user_info = SQLQ.SQLQueries.get_user_information_by_id(current_user['user_id'])
37
38 #executes the query
39 result = connection.execute_query(user_info)
40 connection.cnx.commit()
41
42 #loops through the result if there is any
43 if result:
44 for row in result:
45 #Format activity_timestamp as a string
46 created_timestamp = row[6].strftime('%Y-%m-%d %H:%M:%S') if row[6] else None
47 updated_timestamp = row[7].strftime('%Y-%m-%d %H:%M:%S') if row[7] else None
48
49 user_info = {
50 'user_Id': row[0],
51 'email': row[1],
52 'accountType': row[2],
53 'dbName': row[3],
54 'created_timestamp': created_timestamp,
55 'updater_timestamp': updated_timestamp
56 }
57
58 #Closes the connection
59 connection.close()
60
61 #Returns the user information
62 return user_info
uInfo_route(ns)
Definition userInfo.py:17