示例#1
0
def sortTeamsWithPlaceholder(teams):
    placeholder = Team(number='Select',
                       name='Select',
                       affiliation='Select',
                       city='Select',
                       state='Select',
                       is_rookie=False)
    placeholder.id = -1
    sortedTeams = sorted(teams, key=by_team)
    sortedTeams.insert(0, placeholder)

    return sortedTeams
示例#2
0
def disconnect(platform):
    if platform == "facebook":
        team = Team.get(session.get('team_id'))
        team.facebook_connect(None)

    flash(f"{platform} disconnected", 'pink')

    return redirect(url_for('connectors.list_connectors'))
示例#3
0
def team():
    name = 'Team Tester'
    team = Team.create(name)

    yield team

    print('')
    team.remove()
示例#4
0
def test_update_team(team):
    team.update('Team Tester 2', '123456789', 'landing_page_view')

    team = Team.get(team.id)

    assert team.name == 'Team Tester 2'
    assert team.account_id == '123456789'
    assert team.conversion_event == 'landing_page_view'
示例#5
0
def list_connectors():

    if not session.get('team_id'):
        # No team selected
        flash("Please select a team", 'orange')
        return redirect(url_for('teams.list_teams'))
    else:
        team = Team.get(session.get('team_id'))
        print(team.id, current_user.id)

    return render_template('connectors/list_connectors.html', title='Connectors', team=team)
示例#6
0
def callback(platform):
    token = None
    if platform == "facebook":
        # Get authorization code Facebook sent back to you
        code = request.args.get("code")
        print("callback got it: ", code)

        # Construct the token URL
        client_param = "?client_id=" + Config.CONNECTORS['facebook_app_id']
        secret_param = "&client_secret=" + Config.CONNECTORS['facebook_app_secret']
        code_param = '&code=' + code
        callback_param = '&redirect_uri=' + request.base_url

        token_url = Config.CONNECTORS['facebook_token_endpoint'] + client_param + secret_param + callback_param + code_param

        print("trying url: ", token_url)

        # Get the temp token
        temp_token_response = requests.post(token_url)

        # Parse the temp token
        print("temp token dump:", json.dumps(temp_token_response.json()))

        temp_token = temp_token_response.json().get('access_token')

        # Exchange for long lived token
        grant_param = "?grant_type=fb_exchange_token"
        client_param = "&client_id=" + Config.CONNECTORS['facebook_app_id']
        secret_param = "&client_secret=" + Config.CONNECTORS['facebook_app_secret']
        token_param = "&fb_exchange_token=" + temp_token
        
        exchange_url = Config.CONNECTORS['facebook_token_endpoint'] + grant_param + client_param + secret_param + token_param

        # Get the token
        token_response = requests.post(exchange_url)

        # Parse the token
        print("token dump:", json.dumps(token_response.json()))

        token = token_response.json().get('access_token')

        # Save the token
        team = Team.get(session.get('team_id'))
        team.facebook_connect(token)

    if token:
        flash(f"{platform} connected", 'pink')
    else:
        flash(f"Error: {platform} didn't send tokens", 'red')

    return redirect(url_for('connectors.list_connectors'))
示例#7
0
def dashboard():
    form = DateForm()
    if form.validate_on_submit():
        start_date = form.start_date.data.strftime('%Y-%m-%d')
        end_date = form.end_date.data.strftime('%Y-%m-%d')
        print(start_date, end_date)
    else:
        yesterday = datetime.today() - timedelta(days=1)
        week_ago = yesterday - timedelta(days=6)

        form.start_date.data = week_ago
        form.end_date.data = yesterday

        start_date = week_ago.strftime('%Y-%m-%d')
        end_date = yesterday.strftime('%Y-%m-%d')

    if not session.get('team_id'):
        # No team selected
        flash("Please select a team", 'orange')
        return redirect(url_for('teams.list_teams'))
    else:
        team = Team.get(session.get('team_id'))
        account_id = team.account_id

        if not account_id:
            # No account id for team
            flash("Please ask account owner to update Account ID", 'orange')
            return redirect(url_for('teams.edit_team', team_id=team.id))

    # Run the cloud function
    url = f"https://us-central1-{Config.DB['projectId']}.cloudfunctions.net/get_test_data"
    payload = {
        "access_token": Config.ACCESS_TOKEN,
        "account_id": account_id,
        "date_start": start_date,
        "date_end": end_date
    }
    response = requests.get(url, params=payload)
    data = json.loads(response.text)

    spend = sum([float(row['spend']) for row in data])

    return render_template('charts/dashboard.html',
                           title='Dashboard',
                           data=data,
                           spend=spend,
                           form=form,
                           account_id=account_id)
示例#8
0
def list_teams():
    teams_by_user = Membership.get_teams_by_user(current_user.id)

    teams_list = []
    for membership in teams_by_user:
        membership_data = membership.val()
        team_data = Team.get(membership_data['team_id'])

        team = {
            "id": team_data.id,
            "name": team_data.name,
            "role": membership_data['role'],
        }
        teams_list.append(team)

    return render_template('teams/list_teams.html',
                           title='Teams',
                           teams_list=teams_list)
示例#9
0
def add_team():
    form = TeamForm()

    if form.validate_on_submit():
        name = form.name.data

        #create a team
        try:
            team = Team.create(name)

            Membership.create(current_user.id, team.id, "OWNER")

            # Update successful
            flash(
                'Team id={}, created with name={}'.format(team.id, team.name),
                'teal')
            return redirect(url_for('teams.view_team', team_id=team.id))

        except Exception as e:
            # Update unsuccessful
            flash("Error: {}".format(e), 'red')

    return render_template('teams/add_team.html', title='Add Team', form=form)
示例#10
0
def edit_team(team_id):

    role = Membership.user_role(current_user.id, team_id)
    if role not in ["ADMIN", "OWNER"]:
        abort(401, "You don't have access to edit this team.")

    form = TeamForm()

    team = Team.get(team_id)

    if form.validate_on_submit():
        name = form.name.data
        account_id = form.account_id.data
        conversion_event = form.conversion_event.data

        #edit a team
        try:
            team.update(name, account_id, conversion_event)

            # Update successful
            flash('Team {}, updated with name={}'.format(team.id, team.name),
                  'teal')

            return redirect(url_for('teams.view_team', team_id=team.id))

        except Exception as e:
            # Update unsuccessful
            flash("Error: {}".format(e), 'red')

    form.name.data = team.name
    form.account_id.data = team.account_id
    form.conversion_event.data = team.conversion_event

    return render_template('teams/edit_team.html',
                           title='Edit Team',
                           form=form,
                           team=team)
示例#11
0
def invite_user(team_id):
    role = Membership.user_role(current_user.id, team_id)
    if role not in ["ADMIN", "OWNER"]:
        abort(401, "You don't have access to invite to this team.")

    form = InviteForm()

    team = Team.get(team_id)

    if form.validate_on_submit():
        email = form.email.data
        role = form.role.data

        #create a team
        try:
            user = User.get_by_email(email)

            if not user:
                user = User.invite(email)

            membership = Membership.create(user.id, team_id, role)

            # Update successful
            flash(
                'User {} added to team {} with role {}'.format(
                    membership.user_id, membership.team_id, membership.role),
                'teal')
            return redirect(url_for('teams.view_team', team_id=team.id))

        except Exception as e:
            # Update unsuccessful
            flash("Error: {}".format(e), 'red')

    return render_template('teams/invite_user.html',
                           title='Invite User',
                           form=form,
                           team=team)
示例#12
0
def view_team(team_id):
    team = Team.get(team_id)
    users_by_team = Membership.get_users_by_team(team_id)

    team_members = []
    authorized = False
    role = False
    for membership in users_by_team:
        membership_data = membership.val()
        user = User.get(membership_data['user_id'])

        member = {
            "id": user.id,
            "name": user.name,
            "role": membership_data['role'],
            "membership_id": membership.key()
        }
        team_members.append(member)

        if current_user.id == user.id:
            authorized = True
            role = membership_data['role']

    if not authorized:
        abort(401, "You don't have access to that team")

    session["team_id"] = team.id
    session["team_name"] = team.name

    title = 'View Team {}'.format(team.name)

    return render_template('teams/view_team.html',
                           title=title,
                           team=team,
                           team_members=team_members,
                           role=role)
示例#13
0
def facebook_dashboard():
    form = DateForm()
    if form.validate_on_submit():
        start_date = form.start_date.data.strftime('%Y-%m-%d')
        end_date = form.end_date.data.strftime('%Y-%m-%d')
        print(start_date, end_date)
    else:
        yesterday = datetime.today() - timedelta(days=1)
        week_ago = yesterday - timedelta(days=6)

        form.start_date.data = week_ago
        form.end_date.data = yesterday

        start_date = week_ago.strftime('%Y-%m-%d')
        end_date = yesterday.strftime('%Y-%m-%d')

    if not session.get('team_id'):
        # No team selected
        flash("Please select a team", 'orange')
        return redirect(url_for('teams.list_teams'))
    else:
        team = Team.get(session.get('team_id'))
        account_id = team.account_id
        conversion_event = team.conversion_event

        if not account_id:
            # No account id for team
            flash("Please ask account owner to update Account ID", 'orange')
            return redirect(url_for('teams.edit_team', team_id=team.id))

        if not conversion_event:
            # No account id for team
            flash("Please ask account owner to update Conversion Event",
                  'orange')
            return redirect(url_for('teams.edit_team', team_id=team.id))

    # Run the cloud function
    try:
        url = f"https://us-central1-{Config.DB['projectId']}.cloudfunctions.net/get_facebook_data"
        payload = {
            "access_token": team.facebook_token,
            "account_id": account_id,
            "date_start": start_date,
            "date_end": end_date,
            "conversion_event": conversion_event
        }
        response = requests.get(url, params=payload)
        data = json.loads(response.text)
    except Exception as e:
        # No account id for team
        flash(f"Error: {e}", 'red')
        flash("Make sure Account ID, Conversion Event are correct", 'orange')
        return redirect(url_for('teams.edit_team', team_id=team.id))

    spend = sum([float(row['spend']) for row in data])
    conversions = sum([float(row['conversions']) for row in data])
    if conversions:
        cpa = round(spend / conversions, 2)
    else:
        cpa = 0

    return render_template('charts/facebook_dashboard.html',
                           title='Facebook Dashboard',
                           data=data,
                           spend=spend,
                           conversions=conversions,
                           cpa=cpa,
                           form=form,
                           account_id=account_id)