def sync_from_fortnox():
    try:
        fortnox_data = Fortnox()
        customers = fortnox_data.get_all_customers()
        ret = []

        for element in customers:
            for customer in element:

                cust = {'FortnoxID': customer["CustomerNumber"],
                        'OrganisationNumber': customer['OrganisationNumber'],
                        'Name': customer["Name"],
                        'Email': customer['Email'],
                        'Phone': customer['Phone'],
                        'Address1': customer['Address1'],
                        'Address2': customer['Address2'],
                        'City': customer['City'],
                        'Zipcode': customer['ZipCode']}

                ret.append(cust)

        for customer in ret:
            if User.query.filter_by(fortnox_id=customer['FortnoxID']).first() is not None:
                update_user_in_local_db_from_fortnox(customer)
            else:
                add_user_to_local_db_from_fortnox(customer)

        flash("Members from Fortnox is added to the database!")
    except:
        flash("Error retriving from Fortnox, Wrong credentials?")
def edit_user(user_index=None):
    user = User.query.filter_by(index=user_index).first()
    if user is None:
        return "No user have this ID"
    form = EditUser(obj=user)
    tagevents = get_tagevents_user_dict(user_index)
    if form.validate_on_submit():
        user.name = form.name.data
        user.email = form.email.data
        user.phone = form.phone.data
        user.address = form.address.data
        user.address2 = form.address2.data
        user.city = form.city.data
        user.zip_code = form.zip_code.data
        user.tag_id = form.tag_id.data
        user.gender = form.gender.data
        user.expiry_date = form.expiry_date.data
        user.status = form.status.data
        db.session.commit()
        # If we successfully edited the user, redirect back to userpage.
        fortnox_data = Fortnox()
        fortnox_data.update_customer(user)
        return redirect("/user_page/"+str(user.index))
    if user:
        return render_template('edit_user.html',
                               title='Edit User',
                               form=form,
                               data=user.dict(),
                               tags=tagevents,
                               error=form.errors)
    else:
        return "she wrote upon it; no such number, no such zone"
def add_new_user():
    form = NewUser()
    print("errors", form.errors)
    if form.validate_on_submit():
        tmp_usr = User(form.name.data, form.email.data, form.phone.data,
                       form.address.data, form.address2.data, form.city.data,
                       form.zip_code.data, form.tag_id.data, form.fortnox_id.data,
                       form.expiry_date.data, form.birth_date.data,
                       form.gender.data)
        db.session.add(tmp_usr)
        db.session.commit()
        flash('Created new user: %s with id: %s' % (form.name.data,
                                                    tmp_usr.index))
        tagevent = get_last_tag_event()
        fortnox_data = Fortnox()
        fortnox_data.insert_customer(tmp_usr)
        msg = None
        if tagevent is None:
            msg = None
        else:
            msg = (tmp_usr.index, tagevent.tag_id)
        form = NewUser()
        return render_template('new_user.html',
                               title='New User',
                               form=form,
                               message=msg)
    return render_template('new_user.html',
                           title='New User',
                           form=form)
def fortnox_specific_user(fortnox_id):
    fortnox_data = Fortnox()
    ret = fortnox_data.get_customer_by_id(fortnox_id)
    return render_template('fortnox.html',
                           plot_paths='',
                           data=ret)