Skip to content
Snippets Groups Projects
Commit e3908ab3 authored by ComeF2's avatar ComeF2
Browse files

Implémentation post_message

parent 3c6c9b2b
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -10,16 +10,6 @@ def get_db():
return db
def create_database():
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
with open('schema.sql', 'r') as file:
sql = file.read()
cursor.executescript(sql)
conn.commit()
conn.close()
def clean_db():
try:
get_db().execute("DROP table USERS;")
......@@ -113,6 +103,7 @@ def get_data_email(email):
return None
def change_password(email, newpassword):
try:
sql = "UPDATE USERS SET password = 'newpassword1' WHERE email = 'cbdgsdsd11mail.com';"
......@@ -121,3 +112,17 @@ def change_password(email, newpassword):
return True
except:
return False
def check_email_exists(email):
cursor = get_db().cursor()
cursor.execute("SELECT email FROM USERS WHERE email=?", (email,))
result = cursor.fetchone()
return True if result else False
def postMessage(email_sender, email_recipient, message):
try:
get_db().execute("INSERT into MESSAGES (EMAIL_RECIPIENT, EMAIL_SENDER, MESSAGE) values(?, ?, ?)", [email_recipient, email_sender, message])
get_db().commit()
return True
except:
return False
\ No newline at end of file
......@@ -13,4 +13,12 @@ CREATE TABLE LOGGEDINUSERS(
TOKEN VARCHAR(40) NOT NULL,
EMAIL VARCHAR(30) NOT NULL,
PRIMARY KEY (EMAIl)
)
);
CREATE TABLE MESSAGES(
id INTEGER,
EMAIL_RECIPIENT VARCHAR(30) NOT NULL, --Celui qui reçoit le message
EMAIL_SENDER VARCHAR(30) NOT NULL, --Celui qui envoie le mail
MESSAGE TEXT NOT NULL,
PRIMARY KEY(id AUTOINCREMENT)
);
\ No newline at end of file
......@@ -6,7 +6,6 @@ import string
app = Flask(__name__)
#database_helper.create_database()
@app.route("/", methods = ['GET'])
......@@ -121,6 +120,28 @@ def get_user_data_email(token, email):
else:
return "", 401
@app.route("/account/post_message", methods = ['POST'])
def post_message():
data = request.get_json()
if('token' in data
and 'message' in data
and 'email_recipient' in data):
email_sender = database_helper.tokenToEmail(data['token'])
if email_sender:
if database_helper.check_email_exists(data['email_recipient']):
posted = database_helper.postMessage(email_sender, data['email_recipient'], data['message'])
if posted:
return "", 201
else:
return "", 409
else:
return "", 404
else:
return "", 404
else:
return "", 401
def generate_token():
characters = string.ascii_letters + string.digits
return ''.join(random.choices(characters, k=36))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment