def get_indexers(self):
     """
     For a given WebResource, get the keywords stored in Indexer for that resource
     :return: a dict() with a "keywords" property, with value is an array of keyword objects
     """
     query = Indexer.query().filter(Indexer.webres == self.key)
     if query.count() != 0:
         results = {
             "keywords": [
                 {
                     "value": q.keyword,
                     "slug": q.keyword.replace(" ", "+"),
                     "related_urls": articles_api_version("04") + 'by?keyword=' + q.keyword
                 }
                 for q in query
             ],
             "url": self.url,
             "uuid": self.key.id()
         }
         return results
     return {
         "keywords": []
         ,
         "url": self.url,
         "uuid": self.key.id()
     }
    def build_response(self, query, bookmark):
        """
        Extends super().build_response
        """
        from config.config import articles_api_version

        # define the right url for the endpoint
        if self._query_type == "ALL":
            url = articles_api_version(self._API_VERSION) + "?bookmark="
        elif self._query_type == "TYPE_OF":
            url = articles_api_version(self._API_VERSION) + "by?type=" + self.request.get("type") + "&bookmark="
        else:
            raise ValueError("JSONBaseHandler.build_response(): self._query_type value error")

        # return the dictionary output
        return {"articles": [webres.dump_to_json() for webres in query], "next": url + bookmark if bookmark else None}
def memcache_articles_pagination(query, bkmark):
    """
    Get or set in the memcache single page for articles
    :param query: the paged query
    :param bkmark: the bookmark's key of the actual page
    :return: dict() with an array of articles and a url to the next bookmarked page
    """
    mkey = "Articles_" + bkmark if bkmark else str("null")
    if not memcache.get(key=mkey):
        listed = {'articles': [webres.dump_to_json()
                               for webres in query],
                  'next': articles_api_version(_VERSION) + '?bookmark=' + bkmark if bkmark else None}
        memcache.add(key=mkey, value=listed, time=15000)
    else:
        listed = memcache.get(key=mkey)
    return listed
def memcache_articles_pagination(query, bkmark):
    """
    Get or set in the memcache single page for articles
    :param query: the paged query
    :param bkmark: the bookmark's key of the actual page
    :return: dict() with an array of articles and a url to the next bookmarked page
    """
    mkey = "Articles_" + bkmark if bkmark else str("null")
    if not memcache.get(key=mkey):
        listed = {
            'articles': [webres.dump_to_json() for webres in query],
            'next':
            articles_api_version(_VERSION) + '?bookmark=' +
            bkmark if bkmark else None
        }
        memcache.add(key=mkey, value=listed, time=15000)
    else:
        listed = memcache.get(key=mkey)
    return listed
    def dump_to_json(self):
        """
        Make property values of an instance JSON serializable and build the response body of the REST API
        """
        result = {
            "uuid": self.key.id()
        }

        ### find a parent if entity is a child
        # try:
        #     result['parent'] = self.key.parent().id()
        # except AttributeError:
        #    result['parent'] = None

        ### find and get children if present
        # ancestor_query = WebResource.query(ancestor=self.key).fetch()
        #result['children'] = [
        #    ndb.Key('WebResource', self.key.id(), 'WebResource', e.key.id()).get().key.id()
        #    if len(ancestor_query) <= 2 else []
        #    for e in ancestor_query[1:]
        #]

        # serializing other properties
        for prop, value in self.to_dict().items():
            # If this is a key, you might want to grab the actual model.
            if prop == 'url':
                result[prop] = value
                result['keywords_url'] = articles_api_version("04") + 'keywords/by?url=' + value
            if isinstance(self, ndb.Model):
                if isinstance(value, datetime):
                    result[prop] = value.isoformat()
                    continue
                elif isinstance(value, bool):
                    result[prop] = value
                    continue
                elif value is None:
                    result[prop] = None
                    continue
                from unidecode import unidecode
                result[prop] = unidecode(unicode(value.strip()))

        return result
    def dump_to_json(self):
        """
        make property values of an instance JSON serializable
        """
        result = {"uuid": self.key.id()}
        for prop, value in self.to_dict().items():
            # If this is a key, you might want to grab the actual model.
            if prop == 'url':
                result[prop] = value
                result['keywords_url'] = articles_api_version(
                    "04") + '?url=' + value
            if isinstance(self, ndb.Model):
                if isinstance(value, datetime):
                    result[prop] = value.isoformat()
                    continue
                elif value is None:
                    result[prop] = None
                    continue
                result[prop] = str(value.encode('ascii', 'replace').strip())

        return result
Example #7
0
    def dump_to_json(self):
        """
        make property values of an instance JSON serializable
        """
        result = {
            "uuid": self.key.id()
        }
        for prop, value in self.to_dict().items():
            # If this is a key, you might want to grab the actual model.
            if prop == 'url':
                result[prop] = value
                result['keywords_url'] = articles_api_version("04") + '?url=' + value
            if isinstance(self, ndb.Model):
                if isinstance(value, datetime):
                    result[prop] = value.isoformat()
                    continue
                elif value is None:
                    result[prop] = None
                    continue
                result[prop] = str(value.encode('ascii', 'replace').strip())

        return result
 def get_indexers(self):
     """
     For a given WebResource, get the keywords stored in Indexer for that resource
     :return: a dict() with a "keywords" property, with value is an array of keyword objects
     """
     query = Indexer.query().filter(Indexer.webres == self.key)
     if query.count() != 0:
         results = {
             "keywords": [{
                 "value":
                 q.keyword,
                 "slug":
                 q.keyword.replace(" ", "+"),
                 "related_urls":
                 articles_api_version("04") + 'keywords?keyword=' +
                 q.keyword
             } for q in query],
             "url":
             self.url,
             "uuid":
             self.key.id()
         }
         return results
     return {"keywords": None}