Reordering comments and functions
This commit is contained in:
parent
02afe8bace
commit
607d371534
|
@ -1,8 +1,8 @@
|
||||||
import os
|
|
||||||
from imbox import Imbox
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from imbox import Imbox
|
||||||
import traceback
|
import traceback
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
# Load .env file
|
# Load .env file
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
|
@ -4,6 +4,7 @@ import openpyxl as xl
|
||||||
import datetime
|
import datetime
|
||||||
import pymongo
|
import pymongo
|
||||||
import urllib
|
import urllib
|
||||||
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
load_dotenv() #Load .env file
|
load_dotenv() #Load .env file
|
||||||
|
@ -15,106 +16,110 @@ database = client["website-class"] #Database name
|
||||||
collection = database["school-time-table"] #Collection school time table current
|
collection = database["school-time-table"] #Collection school time table current
|
||||||
collection_archive = database["archive-school-time-table"] #Collection school time table archive
|
collection_archive = database["archive-school-time-table"] #Collection school time table archive
|
||||||
|
|
||||||
x = collection.delete_many({}) #Delete all documents in collection (school-time-table)
|
def recheck():
|
||||||
|
time.sleep(10)
|
||||||
|
update_time_school()
|
||||||
|
|
||||||
# Load excel file
|
def update_time_school():
|
||||||
namefile_xlsx = "attachments/school_time.xlsx"
|
# Load excel file
|
||||||
workbook = xl.load_workbook(filename=namefile_xlsx)
|
namefile_xlsx = "attachments/school_time.xlsx"
|
||||||
ws = workbook.active
|
workbook = xl.load_workbook(filename=namefile_xlsx)
|
||||||
|
ws = workbook.active
|
||||||
|
|
||||||
#Date
|
#Date
|
||||||
current_time = datetime.datetime.now()
|
current_time = datetime.datetime.now()
|
||||||
day = str(current_time.day)
|
day = str(current_time.day)
|
||||||
month = str(current_time.month)
|
month = str(current_time.month)
|
||||||
year = str(current_time.year)
|
year = str(current_time.year)
|
||||||
hour = str(current_time.hour)
|
hour = str(current_time.hour)
|
||||||
minute = str(current_time.minute)
|
minute = str(current_time.minute)
|
||||||
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
|
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
|
||||||
|
|
||||||
#Create collection
|
#Create collection
|
||||||
mydict = {
|
mydict = {
|
||||||
"Date": long_date,
|
"Date": long_date,
|
||||||
"School Subject": [],
|
"School Subject": [],
|
||||||
"Teacher": [],
|
"Teacher": [],
|
||||||
}
|
}
|
||||||
x = collection.insert_one(mydict) # Add collection on collection (school-time-table)
|
x = collection.delete_many({}) #Delete all documents in collection (school-time-table)
|
||||||
x = collection_archive.insert_one(mydict) # Add collection on collection (archive-school-time-table)
|
x = collection.insert_one(mydict) # Add collection on collection (school-time-table)
|
||||||
|
x = collection_archive.insert_one(mydict) # Add collection on collection (archive-school-time-table)
|
||||||
|
|
||||||
#Search my class in excel file and add in MongoDB
|
#Search my class in excel file and add in MongoDB
|
||||||
for row in range (1, 100):
|
for row in range (1, 100):
|
||||||
# column B ~ column F
|
# column B ~ column F
|
||||||
for column in range (1, 100):
|
for column in range (1, 100):
|
||||||
cell = ws.cell(row, column)
|
cell = ws.cell(row, column)
|
||||||
if cell.value == "2elci":
|
if cell.value == "2elci":
|
||||||
ws.cell(row=cell.row, column=column).value
|
ws.cell(row=cell.row, column=column).value
|
||||||
#Search school time table
|
#Search school time table
|
||||||
for i in range(4,80):
|
for i in range(4,80):
|
||||||
school_subject = ws.cell(row=i, column=column).value # Get school subject from excel file
|
school_subject = ws.cell(row=i, column=column).value # Get school subject from excel file
|
||||||
if school_subject == 0: #If school subject is 0, add "null" in MongoDB
|
if school_subject == 0: #If school subject is 0, add "null" in MongoDB
|
||||||
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
||||||
array_username = find_document_username[0]["_id"]
|
array_username = find_document_username[0]["_id"]
|
||||||
collection.update_one( # Add "null" in MongoDB beacause school subject is 0
|
collection.update_one( # Add "null" in MongoDB beacause school subject is 0
|
||||||
{ "_id": ObjectId(array_username)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": "null" }
|
"$push": { "School Subject": "null" }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
collection_archive.update_one( # Add "null" in MongoDB beacause school subject is 0
|
collection_archive.update_one( # Add "null" in MongoDB beacause school subject is 0
|
||||||
{ "_id": ObjectId(array_username)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": "null" }
|
"$push": { "School Subject": "null" }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else: #If school subject is not 0, add school subject in MongoDB
|
else: #If school subject is not 0, add school subject in MongoDB
|
||||||
#remove_things_in_front = school_subject.split(' ', 1)[1]
|
#remove_things_in_front = school_subject.split(' ', 1)[1]
|
||||||
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
||||||
array_username = find_document_username[0]["_id"]
|
array_username = find_document_username[0]["_id"]
|
||||||
collection.update_one( # Add school subject in MongoDB beacause school subject is not 0
|
collection.update_one( # Add school subject in MongoDB beacause school subject is not 0
|
||||||
{ "_id": ObjectId(array_username)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": school_subject }
|
"$push": { "School Subject": school_subject }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
collection_archive.update_one( # Add school subject in MongoDB beacause school subject is not 0
|
collection_archive.update_one( # Add school subject in MongoDB beacause school subject is not 0
|
||||||
{ "_id": ObjectId(array_username)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": school_subject }
|
"$push": { "School Subject": school_subject }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
#Search teacher
|
|
||||||
for i in range(4, 80):
|
|
||||||
teacher = ws.cell(row=i, column=column+1).value
|
|
||||||
column = column
|
|
||||||
if teacher == 0: #If teacher is 0, add "null" in MongoDB
|
|
||||||
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
|
||||||
array_username = find_document_username[0]["_id"]
|
|
||||||
collection.update_one( # Add "null" in MongoDB beacause teacher is 0
|
|
||||||
{ "_id": ObjectId(array_username)},
|
|
||||||
{
|
|
||||||
"$push": { "Teacher": "null" }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
collection_archive.update_one( # Add "null" in MongoDB beacause teacher is 0
|
|
||||||
{ "_id": ObjectId(array_username)},
|
|
||||||
{
|
|
||||||
"$push": { "Teacher": teacher }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
else: #If teacher is not 0, add teacher in MongoDB
|
|
||||||
find_document_username = list(collection.find({}, {"Date": long_date}))
|
|
||||||
array_username = find_document_username[0]["_id"]
|
|
||||||
collection.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
|
||||||
{ "_id": ObjectId(array_username)},
|
|
||||||
{
|
|
||||||
"$push": { "Teacher": teacher }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
collection_archive.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
|
||||||
{ "_id": ObjectId(array_username)},
|
|
||||||
{
|
|
||||||
"$push": { "Teacher": teacher }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
os.remove(namefile_xlsx) #Delete excel file
|
#Search teacher
|
||||||
|
for i in range(4, 80):
|
||||||
|
teacher = ws.cell(row=i, column=column+1).value
|
||||||
|
column = column
|
||||||
|
if teacher == 0: #If teacher is 0, add "null" in MongoDB
|
||||||
|
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
||||||
|
array_username = find_document_username[0]["_id"]
|
||||||
|
collection.update_one( # Add "null" in MongoDB beacause teacher is 0
|
||||||
|
{ "_id": ObjectId(array_username)},
|
||||||
|
{
|
||||||
|
"$push": { "Teacher": "null" }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
collection_archive.update_one( # Add "null" in MongoDB beacause teacher is 0
|
||||||
|
{ "_id": ObjectId(array_username)},
|
||||||
|
{
|
||||||
|
"$push": { "Teacher": teacher }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else: #If teacher is not 0, add teacher in MongoDB
|
||||||
|
find_document_username = list(collection.find({}, {"Date": long_date}))
|
||||||
|
array_username = find_document_username[0]["_id"]
|
||||||
|
collection.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
||||||
|
{ "_id": ObjectId(array_username)},
|
||||||
|
{
|
||||||
|
"$push": { "Teacher": teacher }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
collection_archive.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
||||||
|
{ "_id": ObjectId(array_username)},
|
||||||
|
{
|
||||||
|
"$push": { "Teacher": teacher }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
os.remove(namefile_xlsx) #Delete excel file
|
Loading…
Reference in New Issue