Skip to content

Commit

Permalink
advertisers
Browse files Browse the repository at this point in the history
  • Loading branch information
peterk committed Aug 28, 2018
1 parent 64e8cb6 commit 7c62f48
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
17 changes: 15 additions & 2 deletions web/app/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from models import db, Ad
from sqlalchemy.orm import load_only
from sqlalchemy import desc, asc
from sqlalchemy import desc, asc, func
import os
from flask_caching import Cache
import time

app = Flask(__name__)
db.init_app(app)
DEBUG = os.environ["FLASK_DEBUG"]
ADS_PER_PAGE = 40
ADS_PER_PAGE = 50

POSTGRES = {
'user': os.environ["POSTGRES_USER"],
Expand All @@ -31,6 +31,11 @@ def get_advertisers():
return [ad.advertiser for ad in Ad.query.options(load_only("advertiser")).order_by(Ad.advertiser).distinct("advertiser").all() if ad.advertiser is not None]


def get_advertisers_and_count():
return [(ad.advertiser, ad.count) for ad in Ad.query.with_entities(Ad.advertiser, func.count(Ad.advertiser).label('count')).group_by(Ad.advertiser).order_by(Ad.advertiser).all() if ad.advertiser is not None]



@app.route("/", methods=['GET'])
@cache.cached(timeout=60*60*12, key_prefix=view_full_path_key)
def index():
Expand Down Expand Up @@ -75,6 +80,14 @@ def ad(fbid):



@app.route("/annonsorer", methods=['GET'])
@cache.cached(timeout=60*60*12)
def advertisers():
advertisers = get_advertisers_and_count()
return render_template('advertisers.html', advertisers=advertisers)



@app.route("/om-insamlingen", methods=['GET'])
@cache.cached(timeout=60*60*12)
def about():
Expand Down
33 changes: 33 additions & 0 deletions web/app/templates/advertisers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "layout.html" %}
{% block title %}Annonsörer - Politiska annonser{% endblock %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-12 text-left mt-5">
<h1>Politiska annonsörer på Facebook</h1>
<p>I tabellen listas de annonsörer och antal annonser som samlats in till ProPublicas databas.</p>

{% if advertisers %}
<div class="table-responsive">
<table class="table table-hover" id="advertisers">
<thead>
<tr>
<th scope="col">Annonsör</th>
<th scope="col" class="text-right">Antal</th>
</tr>
</thead>
<tbody>
{% for advertiser in advertisers %}
<tr>
<td><a href="/?advertiser={{advertiser[0]}}">{{advertiser[0]}}</a></td>
<td class="text-right">{{advertiser[1]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion web/app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="container">
<h1 class="jumbotron-heading">Politisk annonsering på Facebook</h1>
<p class="lead">Politisk annonsering kan idag vara målgruppsanpassad på detaljnivå. Genom att ta hjälp av frivilliga och verktyg från ProPublica har det samlats in <a href="#adsection">{{total_adcount}} politiska annonser</a> från Facebook. Genom att presentera dem här kan vi lära oss mer om hur politisk annonsering går till och vilka budskap som riktas till andra målgrupper än de man själv ingår i.</p>
<p class="lead">Vill du hjälpa till att samla in annonser? Installera denna <a href="https://projects.propublica.org/political-ad-collector/">plugin för Chrome och Firefox</a> och hjälp till att rapportera in annonser du med. Insamlingen sparar ingen information om dig som person.</p>
<p class="lead">Vill du hjälpa till att samla in annonser? Installera denna <a href="https://projects.propublica.org/political-ad-collector/">plugin för Chrome och Firefox</a> och hjälp till att rapportera in annonser och <a href="/annonsorer">annonsörer</a> du med. Insamlingen sparar ingen information om dig som person.</p>
<div class="row" role="main">
<div class="col-md-4 offset-md-4">
{% with messages = get_flashed_messages() %}
Expand Down
3 changes: 3 additions & 0 deletions web/app/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link active" href="/annonsorer">Annonsörer</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/om-insamlingen">Om insamlingen</a>
</li>
Expand Down

0 comments on commit 7c62f48

Please sign in to comment.