Rapportsystem API - of D03N
Hovedprosjekt FiV Programmering 20-24
Loading...
Searching...
No Matches
insertData.py
Go to the documentation of this file.
1#Imports nessesary modules
2from flask_restx import Resource
3from flask import request
4from flask_jwt_extended import jwt_required, get_jwt_identity
5
6#imports os
7import os
8current_directory = os.getcwd()
9#imports sys
10import sys
11sys.path.append(os.path.join(current_directory))
12
13#imports custom modules
14from flask_jwt_extended import get_jwt_identity
15from Models import user_model as UM
16from Common.Requirements import valid_token as vt
17from Requests.dataHandler import dataInsertor as dataIns
18
19# create Disa route
21 @ns.route('/insertData')
22
23 class insert_data_class(Resource):
24 new_data_model = UM.insert_data_model(ns)
25
26 #Documentation for swagger UI
27 @ns.doc('/insertData',
28 description='Takes data -> sets it to given table.\nNote: Date, Time and ID is automatically set by the system, and should not be provided.\nGet the table description from /api/user/get/rapportInfo to see what data is required.',
29 responses={
30 200: 'OK',
31 400: 'Invalid Argument or faulty data',
32 500: 'Internal server error'
33 })
34
35 #Validates input
36 @ns.expect(new_data_model, validate=True)
37
38 #Requires valid JWT token authentication
39 @jwt_required()
40 @vt.require_valid_token
41
42 #recives password data from user
43 def post(self):
44 data = request.get_json()
45 current_user = get_jwt_identity()
46
47 # get the table name and data from the request
48 table_name = data['table_name']
49 main_data = data['data']
50
51 # check if forbidden keys are present in the data
52 if any(key in main_data for key in ['date', 'time', 'id', 'sum_price', 'total_weight_melt', 'sum_kwh_used']):
53 return {"Error": "Date, Time, ID, and columns with generated values should not be provided."}
54
55 print("NOW: " + current_user["email"] + " is trying to insert data into table: " + table_name + " with data: " + str(main_data))
56
57 # check if the main_data is empty
58 if not main_data:
59 return {"Error": "No data provided"}
60
61 # return the result of the insertData function
62 return {"Message":dataIns.Data_insertor(table_name, main_data).insertData()}
63