Exemplo n.º 1
0
 def test_similar(self):
     f = self.api \
         .form("everything") \
         .ref(self.api.get_master()) \
         .query(predicates.similar('idOfSomeDocument', 10))
     self.assertEqual(f.data['q'],
                      ['[[:d = similar("idOfSomeDocument", 10)]]'])
Exemplo n.º 2
0
 def test_all_predicates(self):
     # startgist:d0f7e2b2ae7bd3fefc94:prismic-allPredicates.py
     # "at" predicate: equality of a fragment to a value.
     at = predicates.at("document.type", "article")
     # "any" predicate: equality of a fragment to a value.
     any = predicates.any("document.type", ["article", "blog-post"])
     # "fulltext" predicate: fulltext search in a fragment.
     fulltext = predicates.fulltext("my.article.body", "sausage")
     # "similar" predicate, with a document id as reference
     similar = predicates.similar("UXasdFwe42D", 10)
     # endgist
     self.assertEqual(at, ["at", "document.type", "article"])
     self.assertEqual(any, ["any", "document.type", ["article", "blog-post"]])
Exemplo n.º 3
0
 def test_all_predicates(self):
     # startgist:d0f7e2b2ae7bd3fefc94:prismic-allPredicates.py
     # "at" predicate: equality of a fragment to a value.
     at = predicates.at("document.type", "article")
     # "any" predicate: equality of a fragment to a value.
     any = predicates.any("document.type", ["article", "blog-post"])
     # "fulltext" predicate: fulltext search in a fragment.
     fulltext = predicates.fulltext("my.article.body", "sausage")
     # "similar" predicate, with a document id as reference
     similar = predicates.similar("UXasdFwe42D", 10)
     # endgist
     self.assertEqual(at, ["at", "document.type", "article"])
     self.assertEqual(any,
                      ["any", "document.type", ["article", "blog-post"]])
Exemplo n.º 4
0
def get_posts(filters, full=False):
    form = get_prismic_form()
    preds = []
    preds.append(predicates.at('document.type', 'post'))
    page = filters.get("page", "1")
    if page.isdigit():
        page = int(page)
    else:
        page = 1
    page_size = filters.get("limit", "10")
    if page_size.isdigit():
        page_size = int(page_size)
    else:
        page_size = 1
    if "category" in filters:
        preds.append(predicates.at('my.post.category', filters["category"]))
    q = filters.get("q")
    if q:
        preds.append(predicates.fulltext('document', unidecode.unidecode(q)))
    similar = filters.get("similar")
    if similar:
        preds.append(predicates.similar(similar, 5))
    form.query(*preds).page(page).page_size(page_size).orderings('[my.post.posted_at desc]')
    if not full:
        form.fetch(["post.title", "post.summary", "post.posted_at"])
    try:
        response = form.submit()
        if full:
            results = map(format_post, response.documents, [True]*len(response.documents))
        else:
            results = map(format_post, response.documents)
        return {
            "results": results,
            "total_pages": response.total_pages,
            "page": page
        }
    except:
        return {
            "results": [],
            "total_pages": 0,
            "page": page
        }
Exemplo n.º 5
0
 def test_similar(self):
     f = self.api \
         .form("everything") \
         .ref(self.api.get_master()) \
         .query(predicates.similar('idOfSomeDocument', 10))
     self.assertEqual(f.data['q'], ['[[:d = similar("idOfSomeDocument", 10)]]'])
Exemplo n.º 6
0
def test_similar(api):
    f = api \
        .form("everything") \
        .ref(api.get_master()) \
        .query(predicates.similar('idOfSomeDocument', 10))
    assert f.data['q'] == ['[[:d = similar("idOfSomeDocument", 10)]]']