def add_flatmate(form, user_id):
    name_exist = req.select('name', 'flats', name=flat_name)[0][0]
    if name_exist:
        pwd = req.select('password', 'flats', name=form['flat_name'])[0][0]
        flat_id = req.select('flat_id', 'flats', name=form['flat_name'])[0][0]
        if functions.crypted_string(form['flat_password']) == pwd:
            req.update('users', flat_id=flat_id, user_id=user_id)
def add_user(form):
    response = 0
    if form['flat_name']:
        try:
            name_exist = req.select('name', 'flats',
                                    name=form['flat_name'])[0][0]
            pwd = req.select('password', 'flats', name=form['flat_name'])[0][0]
            flat_id = req.select('flat_id', 'flats',
                                 name=form['flat_name'])[0][0]
            if functions.crypted_string(form['flat_password']) != pwd:
                response = 1
            else:
                req.insert('users', 'first_name,last_name,email,password',\
                                    form['first_name'],\
                                    form['last_name'],\
                                    form['email'],\
                                    functions.crypted_string(form['password']))
                req.update('users', flat_id=flat_id, email=form['email'])
                response = 4
        except:
            response = 2
    else:
        req.insert('users', 'first_name,last_name,email,password',\
                            form['first_name'],\
                            form['last_name'],\
                            form['email'],\
                            functions.crypted_string(form['password']))
        response = 4
    return response
Exemple #3
0
def login():
    """
    vue de la page de connexion
    """
    if 'logged' in session.keys():
        response = redirect(url_for('index'))
    else:
        if request.method == 'GET':
            response = render_template('login.html')
        elif request.method == 'POST':
            if request.form['email'] == "" or request.form['password'] == "":
                response = render_template('login.html', nothing=True)
            elif request.form['email'] in [
                    a[0] for a in req.select('email', 'users')
            ]:
                if functions.crypted_string(
                        request.form['password']) != req.select(
                            'password', 'users',
                            email=request.form['email'])[0][0]:
                    response = render_template('login.html', error=True)
                else:
                    session['logged'] = req.select(
                        'user_id', 'users', email=request.form['email'])[0][0]
                    response = redirect(url_for('index'))
            else:
                response = render_template('login.html', error=True)
        else:
            response = 'Unknown method'
    return response
Exemple #4
0
def index():
    """
    vue de la page d'accueil
    """

    if 'logged' not in session.keys():
        response = redirect(url_for('login'))
    else:
        user_id = session['logged']
        if request.method == 'GET':
            flat_id = req.select('flat_id', 'users', user_id=user_id)[0][0]
            name_user = req.select('first_name', 'users',
                                   user_id=user_id)[0][0]
            if flat_id:
                name_flat = req.select('name', 'flats', flat_id=flat_id)[0][0]
                response = render_template('index.html',
                                           flat=True,
                                           name_us=name_user,
                                           name_fl=name_flat,
                                           flat_id=flat_id)
            else:
                response = render_template('index.html',
                                           flat=False,
                                           name_us=name_user)
        elif request.method == 'POST':
            if request.form['index_btn'] == 'invoice':
                forms.add_invoice(request.form, request.files['file'], user_id)
                functions.upload_file(request.files['file'])
                response = redirect(url_for('index'))
            elif request.form['index_btn'] == 'meal':
                forms.add_meal(request.form, user_id)
                response = redirect(url_for('index'))
        else:
            response = "Unknown method"
    return response
Exemple #5
0
def signup():
    """
    vue de la page d'inscription
    """
    if 'logged' in session.keys():
        response = redirect(url_for('index'))
    else:
        if request.method == 'GET':
            response = render_template('sign.html')
        elif request.method == 'POST':
            if request.form['first_name'] == '' or request.form[
                    'last_name'] == '' or request.form[
                        'email'] == '' or request.form['password'] == '':
                response = render_template('sign.html', nothing=True)
            elif request.form['email'] in [
                    a[0] for a in req.select('email', 'users')
            ]:
                response = render_template('sign.html', existing_email=True)
            else:
                forms.add_user(request.form)
                functions.send_mail(request.form)
                session['logged'] = req.select(
                    'user_id', 'users', email=request.form['email'])[0][0]
                if request.form['flat_name']:
                    try:
                        name_exist = req.select(
                            'name', 'flats',
                            name=request.form['flat_name'])[0][0]
                    except:
                        response = render_template('sign.html',
                                                   wrong_flat_name=True)
                    if name_exist:
                        pwd = req.select('password',
                                         'flats',
                                         name=request.form['flat_name'])[0][0]
                        flat_id = req.select(
                            'flat_id', 'flats',
                            name=request.form['flat_name'])[0][0]
                        if functions.crypted_string(
                                request.form['flat_password']) == pwd:
                            req.update('users',
                                       flat_id=flat_id,
                                       email=request.form['email'])
                            response = redirect(url_for('index'))
                        else:
                            response = render_template(
                                'sign.html', wrong_flat_password=True)
                else:
                    response = redirect(url_for('index'))
        else:
            response = "Unknown method"
    return response
def add_flat(form, user_id):
    req.insert('flats', 'name,address,password',\
                        form['new_name'],\
                        form['new_address'],\
                        functions.crypted_string(form['new_password']))
    flat_id = req.select('flat_id', 'flats', name=form['new_name'])[0][0]
    req.update('users', flat_id=flat_id, user_id=user_id)
Exemple #7
0
def flat():
    """
    vue de la page ajout d'une colocation
    """
    if 'logged' not in session.keys():
        response = redirect(url_for('login'))
    else:
        user_id = session['logged']
        if request.method == 'GET':
            flat_id = req.select('flat_id', 'users', user_id=user_id)[0][0]
            if flat_id:
                response = redirect(url_for('invitation'))
            else:
                response = render_template('flat.html')
        elif request.method == 'POST':
            if request.form['index_btn'] == 'flat':
                forms.add_flat(request.form, user_id)
                functions.mail_to_friend(request.form, user_id)
                response = redirect(url_for('index'))
            elif request.form['index_btn'] == 'person':
                forms.add_flatmate(request.form, user_id)
                response = redirect(url_for('index'))
        else:
            response = "Unknown method"
    return response
def spent_by_user(user_id, month, prorata=False):
    spent_fee = req.select('price',
                           'date',
                           'prorata',
                           'invoices',
                           user_id=user_id)
    this_month_fee = [
        a[0] for a in spent_fee if (a[1].month == month and a[2] == prorata)
    ]
    return sum(this_month_fee)
Exemple #9
0
def dashboard_data(flat_id):
    working_list = [
        a[0] for a in req.select('user_id', 'users', flat_id=flat_id)
    ]
    if len(working_list) <= 1:
        json_content = {}
    else:
        result = []
        for user_id in working_list:
            user_names = req.select('first_name',
                                    'last_name',
                                    'users',
                                    user_id=user_id)[0]
            json_disc = {
                "name": user_names[0] + ' ' + user_names[1],
                "balance": round(functions.overall_balance(user_id), 2)
            }
            result.append(json_disc)
        json_content = result
    return jsonify(json_content)
def mail_to_friend(form, user_id):
    flat_id = req.select('flat_id', 'users', user_id=user_id)[0][0]
    flat_name = req.select('name', 'flats', flat_id=flat_id)[0][0]
    pwd = req.select('password', 'flats', flat_id=flat_id)[0][0]
    response = 0
    if 'friend_name' in form.keys() and 'friend_mail' in form.keys(
    ) and 'flat_password' in form.keys():
        if crypted_string(form['flat_password']) != pwd:
            response = 1
        else:
            mailjet = Client(auth=(api_key, api_secret), version='v3.1')
            data = {
                'Messages': [{
                    "From": {
                        "Email": "*****@*****.**",
                        "Name": "Api'Flat"
                    },
                    "To": [{
                        "Email": form['friend_mail'],
                        "Name": form['friend_name']
                    }],
                    "Subject":
                    "Invitation sur Api'flat",
                    "TextPart":
                    "Invitation",
                    "HTMLPart":
                    "<h3>Bonjour <em> " + form['friend_name'] +
                    "<em>,</h3><br><p>Vous êtes invité à rejoindre le gestionnaire de colocation Api'flat. <br>Veuillez trouver ci-dessous les identifiants à renseigner lors de votre inscription. <br> Nom de la colocation : <em> "
                    + flat_name +
                    "<em> <br>Mot de passe de la colocation : <em> " +
                    form['flat_password'] + "<em></p>",
                    "CustomID":
                    "AppGettingStartedTest"
                }]
            }
            mailjet.send.create(data=data)
            response = 2
    else:
        response = 0
    return response
Exemple #11
0
def see_invoice_ajax(inv_id):
    """
    fonction permettant de voir une facture en détail
    """
    if request.method == 'GET':
        one_invoice = req.select('title',
                                 'date',
                                 'price',
                                 'details',
                                 'file_name',
                                 'invoices',
                                 invoice_id=inv_id)[0]
        return jsonify({
            "title": one_invoice[0],
            "date": one_invoice[1],
            "price": str(one_invoice[2]),
            "details": one_invoice[3],
            "file_name": one_invoice[4]
        })
Exemple #12
0
def invoice():
    """
    vue de la page permettant de voir toutes les factures de la colocation concernée
    """
    if 'logged' not in session.keys():
        response = redirect(url_for('login'))
    else:
        user_id = session['logged']
        if request.method == 'GET':
            list_invoice = req.select('invoice_id', 'title', 'date', 'price',
                                      'invoices')
            new_list_invoice = [(a[0], a[1],
                                 datetime.strftime(a[2], "%d/%m/%Y"), a[3])
                                for a in list_invoice]
            response = render_template('detail_facture.html',
                                       list_invoice=new_list_invoice)
        elif request.method == 'POST':
            forms.add_invoice(request.form, request.files['file'], user_id)
            functions.upload_file(request.files['file'])
            response = redirect(url_for('invoice'))
        else:
            response = "Unknown method"
    return response
def meals_number(user_id, month):
    meals_list = [
        a[0] for a in req.select('number', 'date', 'meals', user_id=user_id)
        if a[1].month == month
    ]
    return sum(meals_list)
def user_flatmates(user_id):
    flat_id = which_flat(user_id)
    flatmates = [a[0] for a in req.select('user_id', 'users', flat_id=flat_id)]
    return flatmates
def which_flat(user_id):
    flat_id = req.select('flat_id', 'users', user_id=user_id)[0][0]
    return flat_id
req.insert('flats', 'name,address,password', 'api flat',
           '6 rue Rougemont, Paris', 'demo')
print('Flat data generated')

for a in Us_name_list.keys():
    password = '******'
    req.insert('users', 'first_name,last_name,email,password,flat_id',\
                         Us_name_list[a][0],\
                         Us_name_list[a][1],\
                         Us_name_list[a][2],\
                         functions.crypted_string(password),\
                         1)
print('Users data generated')

Us_id_list = [a[0] for a in req.select('user_id', 'users')]
for c in Us_id_list:
    for b in range(randint(4, 15)):
        user_n = req.select('first_name', 'last_name', 'users', user_id=c)[0]
        title = user_n[0] + ' ' + user_n[1][0:] + ' invoice n°' + str(b)
        price = randint(30, 600)
        prorata = False
        str_date = str(randint(1, 30)) + '/08/2019'
        date = datetime.strptime(str_date, "%d/%m/%Y")
        details = 'details details details'
        req.insert('invoices', 'title,price,prorata,date,details,user_id',
                   title, price, prorata, date, details, c)
print('Invoices data generated')

for c in Us_id_list:
    date_stock = list()