Exemple #1
0
def get_own_team_schedules():
    team = g.user.team
    if team is None:
        return api_error_helpers.item_not_found(item_name="Team-User",
                                                field_name="user-id",
                                                field_value="g.user.id")
    return team_api.team_get_chart_data(team.id)
Exemple #2
0
def project_get(project_id):
    project = project_util.get_project_by_id(project_id)
    if project:
        return jsonify(
            {"id": project.id, "team_id": project.team_id, "name": project.name}
        )
    return api_error_helpers.item_not_found("project", "id", str(project_id))
Exemple #3
0
def team_get_projects(team_id):
    team = team_util.get_from_id(team_id)

    if team:
        return jsonify([project.id for project in team.projects])

    return api_error_helpers.item_not_found("team", "id", str(team_id))
Exemple #4
0
def get_sparse_schedule(user_id: int):
    if not user_util.get_from_id(user_id):
        return api_error_helpers.item_not_found("user", "id", user_id)

    start_str = request.args.get("start_week", default=None, type=str)
    end_str = request.args.get("end_week", default=None, type=str)
    year = request.args.get("year", default=None, type=int)

    if (start_str or end_str) and year:
        return api_error_helpers.invalid_url_args_combination(
            ["start_str", "end_str", "year"])
    if not ((start_str and end_str) or year):
        if not (start_str and end_str):
            return api_error_helpers.missing_url_arg("start_week and end_week")
        else:
            return api_error_helpers.missing_url_arg("year")

    start_week = Week.fromstring(start_str).toordinal() if start_str else None
    end_week = Week.fromstring(end_str).toordinal() if end_str else None

    if year:
        start_week = Week(year, 1)
        end_week = Week.last_week_of_year(year)

    schedule_map = schedule_util.get_user_schedules(user_id, start_week,
                                                    end_week)

    return jsonify(list(sched.serialize() for sched in schedule_map.values()))
Exemple #5
0
def get_new_join_token(team_id):
    team = team_util.get_from_id(team_id)

    if not team:
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    if team.owner_id != g.user.id:
        return api_error_helpers.not_authorized()

    join_token = join_token_util.add_to_team(team_id, secrets.token_urlsafe(16))

    if not join_token:
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    # return jsonify(join_token.serialize())
    return jsonify(
        link=url_for("team.join_link", code=join_token.token_str, _external=True)
    )
Exemple #6
0
def user_get(user_id):
    user = user_util.get_from_id(user_id)

    if user:
        return jsonify({
            "id": user.id,
            "name": user.name,
            "email": user.email,
            "team_id": user.team.id if user.team else None,
        })

    return api_error_helpers.item_not_found("user", "id", str(user_id))
Exemple #7
0
def get_days_off(team_id: int):
    if not team_util.get_from_id(team_id):
        return api_error_helpers.item_not_found("team", "id", str(team_id))
    start_str = request.args.get("start_week", default=None, type=str)
    end_str = request.args.get("end_week", default=None, type=str)

    start_week = Week.fromstring(start_str).toordinal() if start_str else None
    end_week = Week.fromstring(end_str).toordinal() if end_str else None

    days_off = days_off_util.get_days_off(team_id, start_week, end_week)

    return jsonify([day_off.serialize() for day_off in days_off])
Exemple #8
0
def get_join_token(team_id):
    team = team_util.get_from_id(team_id)

    if not team:
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    if g.user.id != team.owner_id:
        return api_error_helpers.not_authorized()

    join_token = join_token_util.by_team_id(team_id)

    if not join_token:
        # Statistically unlikely that we'll ever get repeated tokens
        join_token = join_token_util.add_to_team(team_id, secrets.token_urlsafe(16))

        if not join_token:
            return api_error_helpers.item_not_found("team", "id", str(team_id))

    # return jsonify(join_token.serialize())
    return jsonify(
        link=url_for("team.join_link", code=join_token.token_str, _external=True)
    )
Exemple #9
0
def get_own_team():
    team = g.user.team
    if team is None:
        return api_error_helpers.item_not_found(item_name="Team-User",
                                                field_name="user-id",
                                                field_value="g.user.id")
    return jsonify({
        "id": team.id,
        "name": team.name,
        "team_members": [user.id for user in team.users],
        "projects": [project.id for project in team.projects],
        "owner_id": team.owner.id,
        "user_is_owner": g.user.id == team.owner_id,
    })
Exemple #10
0
def team_get(team_id):
    team = team_util.get_from_id(team_id)

    if team:
        return jsonify(
            {
                "id": team.id,
                "name": team.name,
                "team_members": [user.id for user in team.users],
                "projects": [project.id for project in team.projects],
                "owner_id": team.owner.id,
            }
        )

    return api_error_helpers.item_not_found("team", "id", str(team_id))
Exemple #11
0
def team_create(json_content):
    team = team_util.create(json_content["name"], g.user.id)

    if team:
        return jsonify(
            {
                "id": team.id,
                "name": team.name,
                "team_members": [user.id for user in team.users],
                "projects": [project.id for project in team.projects],
                "owner_id": team.owner.id,
            }
        )

    return api_error_helpers.item_not_found("user", "id", str(g.user.id))
Exemple #12
0
def join_team_token(json_content):
    team = join_token_util.team_by_join_token(json_content["join_token"])
    join_token = team.join_tokens[0] if team.join_tokens else None

    if not join_token:
        return api_error_helpers.item_not_found("join token", "token",
                                                json_content["join_token"])

    user_util.set_team(g.user.id, team.id)
    return jsonify({
        "id": g.user.id,
        "name": g.user.name,
        "email": g.user.email,
        "team_id": g.user.team.id,
    })
Exemple #13
0
def set_days_off(team_id: int, json_content):
    team = team_util.get_from_id(team_id)

    if not team:
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    try:
        date = date_parse(json_content["date"]).date()
        hours_off = int(json_content["hours_off"])
    except ValueError:
        return api_error_helpers.bad_body_arg()

    result = days_off_util.set_day_off(team_id, date, hours_off)
    if not result:
        return api_error_helpers.bad_body_arg()

    return jsonify({"result": "ok"})
Exemple #14
0
def project_schedules(json_content, project_id: int):
    if not project_util.get_project_by_id(project_id):
        return api_error_helpers.item_not_found("project", "id", str(project_id))

    schedule_dict = schedule_util.get_project_schedules(project_id)

    # Filter out everything before a given week-date if provided
    if "not_before" in json_content:
        try:
            filter_week = Week.fromstring(json_content["not_before"])
        except ValueError:
            return api_error_helpers.invalid_body_arg("not_before")
        schedule_dict = {
            key: sched
            for key, sched in schedule_dict.items()
            if sched.week >= filter_week.toordinal()
        }

    return jsonify([sched.serialize() for sched in schedule_dict.values()])
Exemple #15
0
def set_team(json_content):
    join_token = join_token_util.by_team_id(json_content["team_id"])

    if not join_token:
        return api_error_helpers.item_not_found("join_token", "team_id",
                                                json_content["team_id"])

    if join_token.token_str != json_content["join_token"]:
        return api_error_helpers.invalid_join_token()

    user = user_util.set_team(g.user.id, json_content["team_id"])

    if user:
        return jsonify({
            "id": user.id,
            "name": user.name,
            "email": user.email,
            "team_id": user.team.id,
        })

    return api_error_helpers.could_not_update("user", "id", g.user.id)
Exemple #16
0
def team_get_schedules(team_id: int):
    if not team_util.get_from_id(team_id):
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    start_ahead = request.args.get("start_ahead", default=None)
    look_ahead = request.args.get("look_ahead", default=0, type=int)
    try:
        start = Week.fromstring(start_ahead).toordinal()
    except ValueError:
        return api_error_helpers.invalid_url_arg("start_ahead")

    end = start + look_ahead
    if end < start:
        return api_error_helpers.invalid_url_arg('["start_ahead", "look_ahead"]')

    return jsonify(
        [
            schedule.serialize()
            for schedule in schedule_util.get_team_schedules(team_id, start, end)
        ]
    )
Exemple #17
0
def team_get_chart_data(team_id):
    if not team_util.get_from_id(team_id):
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    start_ahead = request.args.get("start_ahead", default=None)
    look_ahead = request.args.get("look_ahead", default=0, type=int)
    try:
        start = Week.fromstring(start_ahead).toordinal()
    except ValueError:
        return api_error_helpers.invalid_url_arg("start_ahead")
    end = start + look_ahead
    if end < start:
        return api_error_helpers.invalid_url_arg('["start_ahead","look_ahead"]')
    period = float(end - start + 1)
    results = schedule_util.get_team_summary_schedule(team_id, start, end, period)
    return jsonify(
        [
            {"user": item[1], "project": item[2], "hours": round(item[0], 2)}
            for item in results
        ]
    )
Exemple #18
0
def team_transfer_owner(team_id, json_content):
    team = team_util.get_from_id(team_id)
    owner_id = json_content["owner_id"]

    if not team:
        return api_error_helpers.item_not_found("team", "id", str(team_id))

    if team.owner_id != g.user.id:
        return api_error_helpers.not_authorized()

    team = team_util.set_owner(team.id, owner_id)

    if team:
        return jsonify(
            {
                "id": team.id,
                "name": team.name,
                "team_members": [user.id for user in team.users],
                "projects": [project.id for project in team.projects],
                "owner_id": team.owner.id,
            }
        )
Exemple #19
0
def get_schedule(user_id: int):
    if not user_util.get_from_id(user_id):
        return api_error_helpers.item_not_found("user", "id", user_id)

    year = request.args.get("year", default=None, type=int)

    if not year:
        return api_error_helpers.missing_url_arg("year")

    start_week = Week(year, 1)
    end_week = Week.last_week_of_year(year)

    user_projects = user_util.get_projects_for_period(user_id=user_id,
                                                      start_week=start_week,
                                                      end_week=end_week)

    project_index = {
        proj.id: index
        for index, proj in enumerate(user_projects)
    }

    full_schedule = [[0 for project in user_projects]
                     for week in Week.weeks_of_year(year)]

    schedule_dict = schedule_util.get_user_schedules(user_id,
                                                     start_week.toordinal(),
                                                     end_week.toordinal())
    print(schedule_dict)

    for week_project, schedule in schedule_dict.items():
        week_index = Week.fromordinal(week_project.week).week - 1
        full_schedule[week_index][project_index[
            week_project.project_id]] = schedule.hours

    return jsonify(
        projects=list(map(lambda x: x.serialize(), user_projects)),
        schedule=full_schedule,
    )