Kolbynko / Covid-tracker

1 stars 0 forks source link

Spojenie častí projektov #9

Open Kolbynko opened 2 years ago

Kolbynko commented 2 years ago

imports three fundamental scripts

import email_sending, database, admin_access

Kolbynko commented 2 years ago

import os import email,smtplib os.system('python -m pip install mysql-connector-python')

from string import Template from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import mysql.connector

mydb=mysql.connector.connect(host="localhost",user="root",database="covid") mycursor = mydb.cursor()

if (mydb): print("Connection successful")

checks if table "users" already exists. If no it will create one

mycursor.execute("SHOW TABLES LIKE 'users'") result = mycursor.fetchone() if result is True: print("Table already extists") if result is False: tableData=("CREATE TABLE users(meno varchar(20),priezvisko varchar(20),v_kontakte boolean,datumpoztest date,covidpass boolean,ockovanie boolean,email varchar(50));") print("New table created") mycursor.execute(tableData)

covidpass=False

Zadavanie mena

while mydb: meno = input("Zadajte krstne meno použivatela:") if len(meno) > 25: print("Prekročili ste limit znakov.") else: break

Zadavanie priezviska

while mydb: priezvisko = input("Zadajte priezvisko použivatela:") if len(priezvisko) > 25: print("Prekročili ste limit znakov.") else: break

Zadavanie v_kontakte

while mydb: v_kontakte = input("Boli ste v kontakte s pozitívnou osobou? (Ano/Nie):'") if v_kontakte=="nie" or v_kontakte=="Nie" or v_kontakte=="NIE": v_kontakte=False break if v_kontakte=="ANO" or v_kontakte=="ano" or v_kontakte=="Ano": v_kontakte=True break

Zadavanie datumu pozitivneho testu

while mydb: datumpoztest = input("Zadajte datum kedy ste boli pozitive testovany: (YYYY-MM-DD).Alebo ak ste neboli tak napište 'nie:'") if datumpoztest=="nie" or datumpoztest=="Nie" or datumpoztest=="NIE": datumpoztest=None break if len(datumpoztest) > 10: print("Neplatný formát.") if int(datumpoztest[5:6])>12: print("Zly mesiac") if int(datumpoztest[8:9])>32: print("Zly datum dna") break

Zadavanie covid passu

while mydb: covidpass = input("Máte platný Covid Pass? (Ano/Nie):") if covidpass=="ANO" or covidpass=="ano" or covidpass=="Ano": covidpass=True break elif covidpass=="Nie" or covidpass=="nie" or covidpass=="Nie": covidpass=False break else: print("Neplatny format")

Zadavanie ockovania

while mydb: ockovanie = input("Ste očkovaný? (Ano/Nie)") if(ockovanie=="ANO" or ockovanie=="ano" or ockovanie=="Ano"): ockovanie=True break elif(ockovanie=="Nie" or ockovanie=="nie" or ockovanie=="Nie"): ockovanie=False break else: print("Neplatny format")

Zadavanie emailu

while mydb: email = input("Zadajte svoj email:") if len(email) > 40: print("Prekročili ste limit znakov.") if "@" not in email: print("Neplatný email.") else: break

Insertion = ("INSERT INTO users" "(meno, priezvisko, datumpoztest, covidpass, ockovanie, email)" "VALUES (%s,%s,%s,%s,%s,%s)") data_data = (meno, priezvisko, datumpoztest, covidpass, ockovanie, email)

Template declaration

finalTemplate=""

templateName1="Template 1.txt" templateName2="Template 2.txt" templateName3="Template 3.txt" templateName4="Template 4.txt"

Creates template

def createTemplate(template): with open(template, 'r', encoding='utf-8') as template_file: template_file_content = template_file.read() return Template(template_file_content)

Picks template for different situations

def templatePicker(v_kontakte, ockovanie, vysledok_testu): if v_kontakte and ockovanie: finalTemplate=createTemplate(templateName1)

elif v_kontakte==False and datumpoztest==None: finalTemplate=createTemplate(templateName2)

elif vysledok_testu is not None: finalTemplate=createTemplate(templateName3)

elif v_kontakte and ockovanie==False: finalTemplate=createTemplate(templateName4)

else: print("Creating template - FAILED")

return finalTemplate

Creates message

def createMessage(name, email,finalTemplate): msg = MIMEMultipart() #creates a message

Creates message out of template with proper name

message = finalTemplate.substitute(PERSON_NAME=name.title())

msg['From']="covidtracker05@gmail.com" msg['To']=email msg['Subject']="Covid-Informator"

Add in the message body

msg.attach(MIMEText(message, 'plain'))

return msg

def sendMessage(loginEmail,loginPassword,msg):

Set up smtp connection

with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp: smtp.ehlo() smtp.starttls() smtp.ehlo()

  smtp.login(loginEmail, loginPassword)
  # Send the message via the server set up earlier.
  smtp.send_message(msg)

sendMessage("covidtracker05@gmail.com", "rbqs nfva ovmk kbjt",createMessage(meno,email,templatePicker(v_kontakte,ockovanie,datumpoztest)))

mycursor.execute(Insertion,data_data) mydb.commit() mycursor.close() mydb.close()