Now the API is ready and everything works correctly

This commit is contained in:
Stefano Assenzo 2022-12-24 16:38:40 +00:00
parent a0c2b9382e
commit 0cd208930b
4 changed files with 144 additions and 9 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
test.xlsx
test.xlsx
.env

73
api/app.py Normal file
View File

@ -0,0 +1,73 @@
from flask import Flask, render_template, request, redirect, session, url_for, jsonify
from dotenv import load_dotenv
import requests
import os
import urllib
import pymongo
app = Flask(__name__)
load_dotenv() #Load .env file
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB
mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB #URL for MongoDB (with password)
client = pymongo.MongoClient(mongo_url) #Connect to MongoDB
database = client["website-class"] #Database name
collection = database["school-time-table"]
@app.route('/', methods = ['GET', 'POST'])
def api():
if(request.method == 'GET'):
collection_find_username = list(collection.find({}, {"School Subject": 1,}))
array_username = collection_find_username[0]['School Subject']
test = {
"subject" : {
"monday": {
"Subject 1": array_username[0],
"Subject 2": array_username[1],
"Subject 3": array_username[2],
"Subject 4": array_username[3],
"Subject 5": array_username[4],
"Subject 6": array_username[5],
},
"tuesday": {
"Subject 1": array_username[6],
"Subject 2": array_username[7],
"Subject 3": array_username[8],
"Subject 4": array_username[9],
"Subject 5": array_username[10],
"Subject 6": array_username[11],
},
"wednesday": {
"Subject 1": array_username[12],
"Subject 2": array_username[13],
"Subject 3": array_username[14],
"Subject 4": array_username[15],
"Subject 5": array_username[16],
"Subject 6": array_username[17],
},
"thursday": {
"Subject 1": array_username[18],
"Subject 2": array_username[19],
"Subject 3": array_username[20],
"Subject 4": array_username[21],
"Subject 5": array_username[22],
"Subject 6": array_username[23],
},
"friday": {
"Subject 1": array_username[24],
"Subject 2": array_username[25],
"Subject 3": array_username[26],
"Subject 4": array_username[27],
"Subject 5": array_username[28],
"Subject 6": array_username[29],
}
}
}
return jsonify(test)
if __name__ == '__main__':
app.run()

9
app.py
View File

@ -1,4 +1,5 @@
from flask import Flask, render_template, request, redirect, session, url_for
from src.script import api_test
from flask import Flask, render_template, request, redirect, session, url_for, jsonify
import requests
import os
@ -21,5 +22,11 @@ def orario():
def calendario():
return render_template('html/calendario.html')
@app.route('/api', methods = ['GET', 'POST'])
def api():
if(request.method == 'GET'):
data = "hello world"
return jsonify({'data': data})
if __name__ == '__main__':
app.run()

View File

@ -1,21 +1,75 @@
from dotenv import load_dotenv
import openpyxl as xl
import urllib
import pymongo
import os
import datetime
from bson.objectid import ObjectId
load_dotenv() #Load .env file
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB
mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB #URL for MongoDB (with password)
client = pymongo.MongoClient(mongo_url) #Connect to MongoDB
database = client["website-class"] #Database name
collection = database["school-time-table"]
#using read_excel() method to read our excel file and storing the same in the variable named "df "
workbook = xl.load_workbook(filename="test.xlsx")
ws = workbook.active
current_time = datetime.datetime.now()
day = str(current_time.day)
month = str(current_time.month)
year = str(current_time.year)
si = day + "-" + month + "-" + year
mydict = {
"Date": si,
"School Subject": [],
"Teacher": [],
}
x = collection.insert_one(mydict)
# row 4
for row in range (1, 100):
# column B ~ column F
for column in range (1, 100):
cell = ws.cell(row, column)
if cell.value == "2elci":
print(ws.cell(row=cell.row, column=column).value)
print(cell.row, column)
ws.cell(row=cell.row, column=column).value
#Search school time table
for i in range(4,50):
print(ws.cell(row=i, column=column).value)
for i in range(4, 55):
print(print(ws.cell(row=i, column=column+1).value))
column = column
for i in range(4,80):
school_subject = ws.cell(row=i, column=column).value
if school_subject == 0:
find_document_username = list(collection.find({}, {"Date": si}))
array_username = find_document_username[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": "null" }
})
else:
remove_things_in_front = school_subject.split(' ', 1)[1]
find_document_username = list(collection.find({}, {"Date": si}))
array_username = find_document_username[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": str(remove_things_in_front) }
})
#Search teacher
for i in range(4, 80):
teacher = ws.cell(row=i, column=column+1).value
column = column
if teacher == 0:
pass
else:
find_document_username = list(collection.find({}, {"Date": si}))
array_username = find_document_username[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_username)},
{
"$push": { "Teacher": teacher }
})