コード例 #1
0
def compare():
    available_countries = [
        country["code"] for country in database.get_collection(
            "countries").find({"code": {
                "$ne": None
            }})
    ]

    selected_countries = request.args.getlist("country")
    etag = database.get_stats("etags", location="johns_hopkins")
    stats = database.get_stats(COLLECTION["country"], country="World")
    if not stats:
        return render_template("compare.html",
                               archive=[],
                               stats_last_updated="N/A")
    return render_template(
        "compare.html",
        top_stats=[{
            "name": key,
            "value": value,
            "icon": ICONS.get(key, "user"),
            "change": stats.get(CHANGES.get(key)),
        } for key, value in parse_top_stats(stats).items()],
        archive=parse_countries_for_comparison(selected_countries),
        data_countries=",".join(available_countries),
        search_default=",".join(selected_countries),
        stats_last_updated=etag["last_updated"].strftime("%d.%m.%Y %H:%M")
        if etag else None,
    )
コード例 #2
0
def parse_countries_for_comparison(codes):
    """
    :param codes: country codes
    :return: [{"date": 2020-12-31, "Germany": 4, "Austria": 5}, {..}]
    """

    if not codes:
        return []

    codes = [code.lower() for code in codes]
    countries = database.get_collection("countries").find(
        {"code": {
            "$in": codes
        }})

    results = {}
    no_info_countries = []
    for country in countries:
        for day in country["history"]:
            try:
                results.setdefault(day["date"],
                                   {})[country["country"]] = day["confirmed"]
            except KeyError:
                if country["country"] not in no_info_countries:
                    no_info_countries.append(country["country"])
    if no_info_countries:
        flash("Următoarele țări conțin date incomplete și nu au fost afișate: "
              f"{', '.join(no_info_countries)}")
    return [{
        "date": k,
        **results[k]
    } for k in results if any([int(x) for _, x in results[k].items()])]
コード例 #3
0
ファイル: datelazi.py プロジェクト: andreipradan/telegrambot
 def _fetch_archive(self, days):
     collection = f"archive{'-small' if self.small_archive  else ''}"
     return list(
         database.get_collection(COLLECTION[collection]).find(
             {"Data": {"$in": days}},
             sort=[("Data", -1)],
         )
     )
コード例 #4
0
def set_multiple(countries, collection):
    collection = database.get_collection(collection)
    for country in countries:
        country["country"] = country.pop("country_other")
        for key, val in country.items():
            try:
                country[key] = int(val.replace(",", ""))
            except ValueError:
                pass
    return collection.bulk_write([
        UpdateOne(
            {"country": country["country"]},
            update={"$set": country},
            upsert=True,
        ) for country in countries
    ])
コード例 #5
0
 def create(cls, **kwargs):
     kwargs["_id"] = uuid.uuid4().hex
     password = kwargs.get("password")
     if password:
         kwargs["password"] = hashpw(password.encode("utf8"), gensalt())
     return database.get_collection("Users").insert_one(kwargs)
コード例 #6
0
 def get(cls, **kwargs):
     data = database.get_collection("Users").find_one(kwargs)
     if data is not None:
         return User(**data)
コード例 #7
0
 def test_get_collection(self, client_mock):
     name = mock.MagicMock(return_value="foo")
     client_mock.return_value.__getitem__.return_value.__getitem__ = name
     assert database.get_collection("foo") == mock.ANY
     client_mock.assert_called_once_with()