Skip to content
Snippets Groups Projects
Commit 7a09a9a1 authored by MaximeOLIVA's avatar MaximeOLIVA
Browse files

lab2 signup function

parent 6dd28196
No related branches found
No related tags found
No related merge requests found
File deleted
File added
No preview for this file type
No preview for this file type
import sqlite3
from flask import g
DATABASE_URI = "database.db"
def get_db():
db = getattr(g, 'db', None)
if db is None:
db = g.db = sqlite3.connect(DATABASE_URI)
return db
def create_user(email, password, firstname, familyname, gender, city, country):
try:
get_db().execute("INSERT into USERS values(?, ?, ?, ?, ?, ?, ?);", [firstname, familyname, gender, city, country, email, password]);
get_db().commit()
return True
except:
return False
def clean_db():
try:
get_db().execute("DROP table USERS;")
return True
except:
return False
CREATE TABLE USERS(
FIRST_NAME VARCHAR (25) NOT NULL,
FAMILY_NAME VARCHAR (25) NOT NULL,
GENDER VARCHAR(6) NOT NULL,
CITY VARCHAR(20) NOT NULL,
COUNTRY VARCHAR(20) NOT NULL,
EMAIL VARCHAR(20) NOT NULL,
PASSWORD VARCHAR(20) NOT NULL,
PRIMARY KEY (EMAIL)
);
from flask import Flask
from flask import Flask, request, jsonify
import database_helper
app = Flask(__name__)
@app.route("/")
@app.route("/", methods = ['GET'])
def hello_world():
return "<p>Hello, Lab_2!</p>", 200
@app.route("/sign_up", methods = ['POST'])
def sign_up():
data = request.get_json()
if ('email' in data and isinstance(data['email'], str)
and 'password' in data and isinstance(data['password'], str)
and 'firstname' in data and isinstance(data['firstname'], str)
and 'familyname' in data and isinstance(data['familyname'], str)
and 'gender' in data and isinstance(data['gender'], str)
and 'city' in data and isinstance(data['city'], str)
and 'country' in data and isinstance(data['country'], str)
):
if(len(data['password']) > 5 and len(data['password']) < 21 and len(data['email']) > 0
and len(data['firstname']) > 0 and len(data['familyname']) > 0 and len(data['gender']) > 0
and len(data['city']) > 0 and len(data['country']) > 0 ):
resp = database_helper.create_user(data['email'], data['password'], data['firstname'],
data['familyname'], data['gender'], data['city'], data['country'])
if resp:
return "", 201
else:
return "", 409
else:
return "", 400
else:
return "test", 400
@app.route("/delete", methods = ['GET'])
def delete():
resp = database_helper.clean_db()
if resp:
return "", 200
else:
return "", 409
if __name__ == '__main__':
app.debug = True
app.run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment