예제 #1
0
def test_update_consultant(id):
    c = db_models.Consultant(id)
    c.first_name = "Updated first name"
    c.last_name = "Updated last name"
    active_consultant_id = json.loads(session['active_consultant'])['id']
    c.set_admin(True, db_models.Consultant(active_consultant_id))
    c.email = "updated - " + c.email
    c.active = True
    c.display_name = c.display_name + " - updated"
    return c.update_in_db().to_json()
예제 #2
0
def test_delete_consultant(id):
    if not 'active_consultant' in session:
        abort(401)

    try:
        active_consultant_id = json.loads(session['active_consultant'])['id']
        db_models.Consultant(id).delete_from_db(db_models.Consultant(active_consultant_id))
        return 'Consultant {} removed from DB.'.format(id)
    except db_models.ConsultantNotAdminError:
        abort(403)
    except db_models.DeleteAllAdminConsultantError:
        return 'Cannot remove only admin user!'
예제 #3
0
def api_consulant_actions(consultant_id):

    if not auth_consultant():
        abort(401)

    try:
        consultant = db_models.Consultant(consultant_id)

        if request.method == "GET":
            return consultant.to_json()

        elif request.method == "PUT":

            c = get_active_consultant()
            if not c.admin:
                abort(401)

            consultant.email = request.values['email']
            consultant.display_name = request.values['display_name']
            consultant.first_name = request.values['first_name']
            consultant.last_name = request.values['last_name']
            print(request.values['active'])
            consultant.active = request.values['active'] == 'true'
            consultant.set_admin(request.values['admin'] == 'true', c)

            print(request.values['reset_password'])

            if request.values['reset_password'] != "":
                consultant.change_password("",
                                           request.values['reset_password'], c)

            consultant.update_in_db()

            return consultant.to_json()

        elif request.method == "DELETE":

            c = get_active_consultant()
            if not c.admin:
                abort(401)

            consultant.delete_from_db(c)

            return consultant.to_json()

    except db_models.ConsultantNotFoundError:
        abort(404)

    except db_models.ConsultantUpdateError as e:
        return str(e), 400

    except Exception as e:
        return str(e), 400
예제 #4
0
def consultant_modal_view(consultant_id):

    c = get_active_consultant()
    if not c:
        abort(401)

    try:
        consultant_edit = db_models.Consultant(consultant_id)

        return render_template('dashboard/modals/add_view_edit_consultant.html', consultant=consultant_edit.to_dict(), active_consultant=c, read_only=True)

    except db_models.ConsultantNotFoundError:
        abort(404)
예제 #5
0
def api_projects():

    if not auth_consultant():
        abort(401)

    if request.method == "GET":

        return db_models.Project.get_all_projects_json()

    elif request.method == "POST":

        # Add a project to DB
        start_date = []
        for s in request.values['start_ts'].split('-'):
            start_date.append(int(s))

        start_date_object = datetime.datetime(start_date[0], start_date[1],
                                              start_date[2])

        try:

            p = db_models.Project.add_new_to_db(
                request.values['code'], int(request.values['client']),
                request.values['title'], 1, request.values['active'] == 'true',
                start_date_object)

            c_list = request.values['consultants'].split("consultants=")
            c_ids = []
            for c in c_list:
                if c != '':
                    c = c.rstrip('&')
                    c_ids.append(int(c))

            for id in c_ids:
                c = db_models.Consultant(id)

                p.assign_consultant_to_project(c)

            p.add_multiple_participants(int(request.values['participants']))

            return p.to_json()

        except db_models.ProjectBFCodeError as e:
            return str(e), 400

        except db_models.ConsultantNotFoundError as e:
            return str(e), 400

        except Exception as e:

            return str(e), 400
예제 #6
0
def get_consultant(id):
    if not auth_consultant():
        abort(401)

    try:
        c = db_models.Consultant(id)
        return c.to_json()

    except db_models.ConsultantNotFoundError:
        return json.dumps({
            "error_code":
            "301",
            "error_text":
            "No consultant found with ID {}".format(id)
        }), 404
예제 #7
0
def projects_table_page():

    c = get_active_consultant()
    if not c:
        return redirect(url_for('consultant_login'))

    all_projects = db_models.Project.get_all_projects_dicts()

    for p in all_projects:
        cs = []
        for consultant in p['consultants']:
            cs.append(db_models.Consultant(consultant).to_dict())
        p['consultants'] = cs
        p['num_participants'] = len(p['participants'])

    return render_template('dashboard/projects_table.html', projects=all_projects, active_projects=c.get_active_project_details(), consultant=c.to_dict(), page_data=get_page_data())
예제 #8
0
def get_project(project_id):
    if not auth_consultant():
        abort(401)

    try:
        p = db_models.Project(project_id)

        if request.method == "GET":
            return p.to_json()

        elif request.method == "PUT":

            # Update project's data
            start_date = []
            for s in request.values['start_ts'].split('-'):
                start_date.append(int(s))

            start_date_object = datetime.datetime(start_date[0], start_date[1],
                                                  start_date[2])

            if request.values['code'] != p.bf_code:
                p.update_bf_code(request.values['code'],
                                 get_active_consultant())
            p.title = request.values['title']

            if request.values['active'] == 'true':
                p.activate(get_active_consultant())
            elif request.values['active'] == 'false':
                p.deactivate(get_active_consultant())

            p.client_id = int(request.values['client'])
            p.start_ts = start_date_object

            # If the consultant assignments have been provided, then need to check for any consultant IDs not already assigned and add them, also check if there are assigned consultants to remove. Might be best to simply remove all consultants and reassign.

            c_ids = []
            for id in request.values['consultants'].split("consultants="):
                if id != '':
                    id = id.rstrip('&')
                    c_ids.append(int(id))

            if len(c_ids) > 0:

                for c in p.consultants:
                    p.remove_consultant_from_project(c,
                                                     get_active_consultant())

                for id in c_ids:
                    p.assign_consultant_to_project(db_models.Consultant(id))

            p.update_last_activity_ts()
            p.update_in_db()

            return p.to_json()

        if request.method == "DELETE":
            p.delete_from_db(get_active_consultant())
            return p.to_json()

    except db_models.ProjectBFCodeError as e:
        return str(e), 400

    except db_models.ConsultantNotAdminError as e:
        return str(e), 401

    except db_models.ProjectNotFound as e:
        return str(e), 404

    except db_models.DeleteActiveProjectError as e:
        return str(e), 400

    except Exception as e:
        return str(e), 400
예제 #9
0
def get_active_consultant():
    if 'active_consultant' in session:
        return db_models.Consultant(session['active_consultant'])
    else:
        return None