示例#1
0
 def close_connection(self):
     logger.info('solicitado cerrar conexión con la base de datos')
     try:
         self.connection.close()
     except Exception as e:
         logger.error(f'error cerrando conexión con la base de datos: {e}')
         return {"code": 1, "error": e}
示例#2
0
    def get(self):
        """Return base data set required by the web client."""

        if not is_cache_filler():
            logger.info("Cache miss for {}".format(request.path))

        rv = {"data": dict()}

        # Elections

        try:
            elections = db.session.query(Election).all()
        except SQLAlchemyError as e:
            logger.error(e)
            return json_response({"error": "Server Error"})

        rv["data"]["elections"] = defaultdict(list)
        for election in elections:
            rv["data"]["elections"][election.territory].append(
                election.to_dict(thesis_data=False))

        # Tags

        tagItems = (db.session.query(Tag, func.count(Thesis.id)).join(
            Tag.theses).group_by(Tag.title).all())

        rv["data"]["tags"] = [
            item[0].to_dict(
                thesis_count=item[1],
                query_root_status=True,
                include_related_tags="simple",
            ) for item in tagItems
        ]

        return json_response(rv)
示例#3
0
def call_api():
    hubspot = create_client()
    try:
        hubspot.crm.contacts.basic_api.get_page()
        logger.info("Requesting get_page: success")
    except ApiException as e:
        logger.error("Exception occurred, status code: ".format(e.status))
示例#4
0
def call_api():
    # Pay attention on create_client.
    # It generates a client with reties middlewares.
    hubspot = create_client()
    try:
        page = hubspot.crm.contacts.basic_api.get_page()
        if os.getppid() == 0:
            logger.info("Requesting get_page: success")
    except ApiException as e:
        logger.error("Exception occurred, status code: ".format(e.status))
示例#5
0
    def insert_registro(self, id, timestamp, oficina):
        logger.info(f'solicitado añadir registro a la base de datos',
                    color="amarillo")
        cmd = f"""
            INSERT INTO registros (timestamp, id, oficina) 
            VALUES ("{timestamp}", "{id}", "{oficina}");
        """
        try:
            self.cursor.execute(cmd)
            result = self.cursor.fetchall()
        except Exception as e:
            logger.error(f'error ejecutando comando {cmd} en la bbdd: {e}')
            return {
                "code": 1,
                "error": f'error ejecutando comando {cmd} en la bbdd: {e}'
            }

        logger.info(f'registro añadido correctamente a la base de datos',
                    color='azul')
        return {"code": 0, "error": ''}
示例#6
0
    def get(self):
        """A list of all elections."""

        if not is_cache_filler():
            logger.info("Cache miss for {}".format(request.path))

        try:
            elections = Election.query.all()
        except SQLAlchemyError as e:
            logger.error(e)
            return json_response({"error": "Server Error"})

        thesis_data = request.args.get("thesis_data", False)

        rv = {"data": defaultdict(list)}
        for election in elections:
            rv["data"][election.territory].append(
                election.to_dict(thesis_data=thesis_data))

        return json_response(rv)
示例#7
0
def exceptions(e):
    ts = datetime.utcnow().strftime("[%Y-%b-%d %H:%M]")
    tb = traceback.format_exc()
    logger.error(
        "%s %s %s %s %s 5xx INTERNAL SERVER ERROR\n%s",
        ts,
        request.remote_addr,
        request.method,
        request.scheme,
        request.full_path,
        tb,
    )

    return json_response(
        {
            "error":
            "AAAHH! Serverfehler. Rute ist gezückt, Computer wird bestraft."
        },
        status=500,
    )
示例#8
0
def load_results():
    """Match election records to the existing election datasets."""
    logger.info("Matching election results...")

    with open("../wahlergebnisse/wahlergebnisse.json") as f:
        result_data = json.load(f)
    with open("../userdata/substitutions.json") as f:
        substitutions = defaultdict(list)
        substitutions.update(json.load(f))

    for occ in db.session.query(Election).all():
        dt = occ.date.date()
        occ_results = [
            o
            for o in result_data
            if o["territory"].lower().startswith(occ.territory.lower()[:2])
            and dateutil.parser.parse(o["date"]).date() == dt
        ]

        matched_results = set()

        if len(occ_results) == 0:
            logger.error("Didn't find results for {}. Removing from db..".format(occ))
            for th in occ.theses:
                for pos in th.positions:
                    db.session.delete(pos)
                db.session.delete(th)
            db.session.delete(occ)
        else:
            res = occ_results[0]

            if "preliminary" in res and res["preliminary"] == True:
                logger.warning("Marking {} as preliminary".format(occ))
                occ.preliminary = True
                yield occ

            parties = set([p.party for p in occ.theses[0].positions])
            for p in parties:
                options = [p.name.lower()] + list(map(str.lower, substitutions[p.name]))
                matches = [
                    (name, result)
                    for name, result in res["results"].items()
                    if name.lower() in options
                ]

                if len(matches) > 0:
                    for match in matches:
                        if match[0].lower() != p.name.lower():
                            logger.warning(
                                "Assigned WOM text from {} to election result of {} in {}".format(
                                    p, match[0], res["title"]
                                )
                            )
                        matched_results.add(match[0])
                        votes = match[1]["votes"] if "votes" in match[1] else None
                        yield Result(
                            election=occ,
                            party=p,
                            party_repr=match[0],
                            votes=votes,
                            pct=match[1]["pct"],
                            source_url=res["url"],
                            source_name="Tagesschau Wahlarchiv"
                        )
                else:
                    if occ.preliminary:
                        logger.info("{} missing vote count for  {}".format(occ, p))
                    else:
                        logger.error("No vote count for {} in {}".format(p, occ))

            # Add results missing in Wahl-o-Mat
            for p_name, match in res["results"].items():
                if p_name in list(matched_results):
                    continue

                # Try and assign a unified party instance to this election
                # result to merge parties that have changed their name over
                # time

                party = None
                if p_name in party_instances.keys():
                    party = party_instances[p_name]
                else:
                    for (name, subs) in substitutions.items():
                        if p_name in subs:
                            if name in party_instances.keys():
                                party = party_instances[name]
                                logger.info(
                                    "Linked statement {} to election result of '{}' in {}".format(
                                        party, p_name, res["title"]
                                    )
                                )
                            break

                if party is None:
                    party = Party(name=p_name)
                    party_instances[p_name] = party

                yield Result(
                    election=occ,
                    party_repr=p_name,
                    party=party,
                    votes=match["votes"] if "votes" in match else None,
                    pct=match["pct"],
                    source_url=res["url"],
                    source_name="Tagesschau Wahlarchiv",
                    wom=False,
                )
示例#9
0
    def registros(self, client):
        logger.info(f'solicitando registros a la base de datos',
                    color="amarillo")

        cmd = ''
        if client == 'julio' or client == 'jesus':
            logger.info(
                f'solicitando id de todos los usuarios a la base de datos',
                color="blanco")

            cmd_0 = f'SELECT * FROM RFID.nombres;'

            try:
                self.cursor.execute(cmd_0)
                ids = self.cursor.fetchall()
            except Exception as e:
                logger.error(
                    f'error ejecutando comando {cmd_0} en la bbdd: {e}')
                return {
                    "code": 1,
                    "error":
                    f'error ejecutando comando {cmd_0} en la bbdd: {e}',
                    "result": ''
                }

            res = []
            for element in ids:
                cmd = f'SELECT * FROM RFID.registros WHERE id ="{element["id"]}";'
                try:
                    self.cursor.execute(cmd)
                    result = self.cursor.fetchall()
                except Exception as e:
                    logger.error(
                        f'error ejecutando comando {cmd} en la bbdd: {e}')
                    return {
                        "code": 1,
                        "error":
                        f'error ejecutando comando {cmd} en la bbdd: {e}',
                        "result": ''
                    }
                res.append({element['nombre']: result})

            logger.info(f'registros obtenidos correctamente', color='azul')
            return {"code": 0, "error": '', "result": res}

        else:
            logger.info(f'solicitando id del usuario a la base de datos',
                        color="blanco")

            cmd_0 = f'SELECT * FROM RFID.nombres WHERE nombre ="{client}";'

            try:
                self.cursor.execute(cmd_0)
                id = self.cursor.fetchall()[0]['id']
            except Exception as e:
                logger.error(
                    f'error ejecutando comando {cmd_0} en la bbdd: {e}')
                return {
                    "code": 1,
                    "error":
                    f'error ejecutando comando {cmd_0} en la bbdd: {e}',
                    "result": ''
                }

            cmd = f'SELECT * FROM RFID.registros WHERE id ="{id}";'

            try:
                self.cursor.execute(cmd)
                result = self.cursor.fetchall()
            except Exception as e:
                logger.error(f'error ejecutando comando {cmd} en la bbdd: {e}')
                return {
                    "code": 1,
                    "error": f'error ejecutando comando {cmd} en la bbdd: {e}',
                    "result": ''
                }

            logger.info(f'registros obtenidos correctamente', color='azul')
            return {"code": 0, "error": '', "result": result}