コード例 #1
0
    def siblings(self, **kwargs):
        limit = kwargs.pop("limit", 5)
        # get immediate parents
        parent = get_parents(self.attr["id"], self.attr_type)
        parent = self.get_uniques(parent)
        parent = parent[-1]

        siblings = [c for c in get_children(parent["id"], self.attr_type, self.sumlevel()) if c['id'] != self.attr["id"]]
        siblings = siblings[:limit+1]

        return self.make_links(siblings)
コード例 #2
0
ファイル: profile.py プロジェクト: tgarland1/datausa-site
    def siblings(self, **kwargs):
        limit = kwargs.pop("limit", 5)
        # get immediate parents
        parent = get_parents(self.attr["id"], self.attr_type)
        parent = self.get_uniques(parent)
        parent = parent[-1]

        siblings = [c for c in get_children(parent["id"], self.attr_type, self.sumlevel()) if c['id'] != self.attr["id"]]
        siblings = siblings[:limit+1]

        return self.make_links(siblings)
コード例 #3
0
def get_img(attr_kind, attr_id, mode="thumb"):
    gobj = fetch(attr_id, attr_kind)
    my_id = gobj['id']
    if 'image_link' not in gobj or not gobj['image_link']:
        parents = get_parents(attr_id, attr_kind)
        for p in reversed(parents):
            p = fetch(p["id"], attr_kind)
            if "image_link" in p and p['image_link']:
                my_id = p['id']
                break
    static_root_url = SPLASH_IMG_DIR.format(mode, attr_kind)
    img_url = static_root_url.format(mode, attr_kind) + "{}.jpg".format(my_id)
    return redirect(img_url)
コード例 #4
0
ファイル: views.py プロジェクト: DataUSA/datausa-site
def get_img(attr_kind, attr_id, mode="thumb"):
    gobj = fetch(attr_id, attr_kind)
    my_id = gobj['id']
    if 'image_link' not in gobj or not gobj['image_link']:
        parents = get_parents(attr_id, attr_kind)
        for p in reversed(parents):
            p = fetch(p["id"], attr_kind)
            if "image_link" in p and p['image_link']:
                my_id = p['id']
                break
    static_root_url = SPLASH_IMG_DIR.format(mode, attr_kind)
    img_url = static_root_url.format(mode, attr_kind) + "{}.jpg".format(my_id)
    return redirect(img_url)
コード例 #5
0
ファイル: views.py プロジェクト: shaneengel/datausa-site
 def formatImage(attr, attr_type):
     image_attr = False
     if "image_link" in attr and attr["image_link"]:
         image_attr = attr
     else:
         parents = [
             fetch(p["id"], attr_type)
             for p in get_parents(attr["id"], attr_type)
         ]
         for p in reversed(parents):
             if "image_link" in p and p["image_link"]:
                 image_attr = p
                 break
     if image_attr:
         return image_attr["id"]
コード例 #6
0
 def image(self):
     if "image_link" in self.attr:
         url = "/static/img/splash/{}/".format(self.attr_type)
         image_attr = False
         if self.attr["image_link"]:
             image_attr = self.attr
         else:
             parents = [fetch(p["id"], self.attr_type) for p in get_parents(self.attr["id"], self.attr_type)]
             for p in reversed(parents):
                 if p["image_link"]:
                     image_attr = p
                     break
         if image_attr:
             return {
                 "url": "{}{}.jpg".format(url, image_attr["id"]),
                 "link": image_attr["image_link"],
                 "author": image_attr["image_author"],
                 "meta": image_attr.get("image_meta", False)
                 }
     return None
コード例 #7
0
ファイル: profile.py プロジェクト: tgarland1/datausa-site
 def image(self):
     if "image_link" in self.attr:
         url = "/static/img/splash/{}/".format(self.attr_type)
         image_attr = False
         if self.attr["image_link"]:
             image_attr = self.attr
         else:
             parents = [fetch(p["id"], self.attr_type) for p in get_parents(self.attr["id"], self.attr_type)]
             for p in reversed(parents):
                 if p["image_link"]:
                     image_attr = p
                     break
         if image_attr:
             return {
                 "url": "{}{}.jpg".format(url, image_attr["id"]),
                 "link": image_attr["image_link"],
                 "author": image_attr["image_author"],
                 "meta": image_attr.get("image_meta", False)
                 }
     return None
コード例 #8
0
ファイル: profile.py プロジェクト: shaneengel/datausa-site
 def formatImage(attr, attr_type):
     url = "/static/img/splash/{}/".format(attr_type)
     image_attr = False
     if "image_link" in attr and attr["image_link"]:
         image_attr = attr
     else:
         parents = [
             fetch(p["id"], attr_type)
             for p in get_parents(attr["id"], attr_type)
         ]
         for p in reversed(parents):
             if "image_link" in p and p["image_link"]:
                 image_attr = p
                 break
     if image_attr:
         return {
             "url": "{}{}.jpg".format(url, image_attr["id"]),
             "link": image_attr["image_link"],
             "author": image_attr["image_author"],
             "meta": image_attr.get("image_meta", False)
         }
コード例 #9
0
    def parents(self, **kwargs):
        id_only = kwargs.get("id_only", False)
        limit = kwargs.pop("limit", None)
        attr_id = self.id(**kwargs)
        prefix = kwargs.get("prefix", None)

        if (prefix or limit) and id_only == False:
            top = get_parents(attr_id, self.attr_type)
            if prefix:
                top = [p for p in top if p["id"].startswith(prefix)]
            if limit:
                top = top[-int(limit):]
            top = self.make_links(top)
            return top

        if self.attr["id"] == "01000US":

            col = kwargs.get("col", False)
            if col:

                params = {
                    "show": "geo",
                    "required": col,
                    "sumlevel": "state",
                    "order": col,
                    "sort": "desc",
                    "year": "latest"
                }

                query = RequestEncodingMixin._encode_params(params)
                url = "{}/api?{}".format(API, query)

                try:
                    results = [r for r in datafold(requests.get(url).json()) if r[col]]
                except ValueError:
                    app.logger.info("STAT ERROR: {}".format(url))
                    return ""

                results = results[:2] + results[-2:]

                if id_only:
                    return ",".join([r["geo"] for r in results])
                else:
                    return [fetch(r["geo"], "geo") for r in results]
            else:
                results = [
                    {"id": "04000US06", "name": "California", "url_name": "california"},
                    {"id": "04000US48", "name": "Texas", "url_name": "texas"},
                    {"id": "04000US36", "name": "New York", "url_name": "new-york"},
                    {"id": "16000US1150000", "name":"Washington D.C.", "url_name": "washington-dc"},
                    {"id": "16000US3651000", "name":"New York, NY", "url_name": "new-york-ny"},
                    {"id": "16000US0644000", "name":"Los Angeles, CA", "url_name": "los-angeles-ca"},
                    {"id": "16000US1714000", "name":"Chicago, IL", "url_name": "chicago-il"}
                ]

                if id_only:
                    return ",".join([r["id"] for r in results])
                else:
                    return results

        results = [p for p in get_parents(attr_id, self.attr_type) if p["id"] != attr_id]
        results = self.get_uniques(results)
        for p in results:
            if self.attr_type == "geo":
                level = p["id"][:3]
            elif self.attr_type == "cip":
                level = str(len(p["id"]))
            else:
                level = str(fetch(p["id"], self.attr_type)["level"])
            p["sumlevel"] = SUMLEVELS[self.attr_type][level]["label"]

        if prefix:
            results = [r for r in results if r["id"].startswith(prefix)]

        if limit:
            results = results[-int(limit):]

        if id_only:
            return ",".join([r["id"] for r in results])
        else:
            return results
コード例 #10
0
ファイル: profile.py プロジェクト: tgarland1/datausa-site
    def parents(self, **kwargs):
        id_only = kwargs.get("id_only", False)
        limit = kwargs.pop("limit", None)
        attr_id = self.id(**kwargs)
        prefix = kwargs.get("prefix", None)

        if (prefix or limit) and id_only == False:
            top = get_parents(attr_id, self.attr_type)
            if prefix:
                top = [p for p in top if p["id"].startswith(prefix)]
            if limit:
                top = top[-int(limit):]
            top = self.make_links(top)
            return top

        if self.attr["id"] == "01000US":

            col = kwargs.get("col", False)
            if col:

                params = {
                    "show": "geo",
                    "required": col,
                    "sumlevel": "state",
                    "order": col,
                    "sort": "desc",
                    "year": "latest"
                }

                query = RequestEncodingMixin._encode_params(params)
                url = "{}/api?{}".format(API, query)

                try:
                    results = [r for r in datafold(requests.get(url).json()) if r[col]]
                except ValueError:
                    app.logger.info("STAT ERROR: {}".format(url))
                    return ""

                results = results[:2] + results[-2:]

                if id_only:
                    return ",".join([r["geo"] for r in results])
                else:
                    return [fetch(r["geo"], "geo") for r in results]
            else:
                results = [
                    {"id": "04000US06", "name": "California", "url_name": "california"},
                    {"id": "04000US48", "name": "Texas", "url_name": "texas"},
                    {"id": "04000US36", "name": "New York", "url_name": "new-york"},
                    {"id": "16000US1150000", "name":"Washington D.C.", "url_name": "washington-dc"},
                    {"id": "16000US3651000", "name":"New York, NY", "url_name": "new-york-ny"},
                    {"id": "16000US0644000", "name":"Los Angeles, CA", "url_name": "los-angeles-ca"},
                    {"id": "16000US1714000", "name":"Chicago, IL", "url_name": "chicago-il"}
                ]

                if id_only:
                    return ",".join([r["id"] for r in results])
                else:
                    return results

        results = [p for p in get_parents(attr_id, self.attr_type) if p["id"] != attr_id]
        results = self.get_uniques(results)

        if prefix:
            results = [r for r in results if r["id"].startswith(prefix)]

        if limit:
            results = results[-int(limit):]

        if id_only:
            return ",".join([r["id"] for r in results])
        else:
            return results