Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional lang for retrieving docs & fixed unit test #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions prismic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def form(self, name):
raise Exception("Bad form name %s, valid form names are: %s" % (name, ', '.join(self.forms)))
return SearchForm(self.forms.get(name), self.access_token, self.cache, self.request_handler)

def query(self, q, ref=None, page_size=None, page=None, orderings=None, after=None, fetch_links=None):
def query(self, q, ref=None, page_size=None, page=None, orderings=None, after=None, fetch_links=None, lang=None):
if ref is None:
ref = self.get_master()
form = self.form('everything').ref(ref)
Expand All @@ -131,32 +131,36 @@ def query(self, q, ref=None, page_size=None, page=None, orderings=None, after=No
form.after(after)
if fetch_links is not None:
form.fetch_links(fetch_links)
if lang is not None:
form.lang(lang)
return form.query(q).submit()

def query_first(self, q, ref=None):
documents = self.query(q, ref, page_size=1, page=1).documents
def query_first(self, q, ref=None, lang=None):
documents = self.query(q, ref, page_size=1, page=1, lang=lang).documents
if len(documents) > 0:
return documents[0]

def get_by_uid(self, type, uid, ref=None):
return self.query_first(predicates.at('my.' + type + '.uid', uid), ref)
def get_by_uid(self, type, uid, ref=None, lang=None):
return self.query_first(predicates.at('my.' + type + '.uid', uid), ref, lang=lang)

def get_by_id(self, id, ref=None):
return self.query_first(predicates.at('document.id', id), ref)
def get_by_id(self, id, ref=None, lang=None):
return self.query_first(predicates.at('document.id', id), ref, lang=lang)

def get_by_ids(self, ids, ref=None, page_size=None, page=None, orderings=None, after=None, fetch_links=None):
def get_by_ids(self, ids, ref=None, page_size=None, page=None, orderings=None,
after=None, fetch_links=None, lang=None):
return self.query(
predicates.in_('document.id', ids),
ref,
page_size=page_size,
page=page,
orderings=orderings,
after=after,
fetch_links=fetch_links
fetch_links=fetch_links,
lang=lang
)

def get_single(self, type, ref=None):
return self.query_first(predicates.at('document.type', type), ref)
def get_single(self, type, ref=None, lang=None):
return self.query_first(predicates.at('document.type', type), ref, lang=lang)

class Ref(object):
"""
Expand Down Expand Up @@ -316,6 +320,13 @@ def pageSize(self, nb_results):
"""
return self.page_size(nb_results)

def lang(self, lang):
"""
Set query language eg. "fr-fr" for traditional French
:param lang: string
"""
return self.set("lang", lang)

def count(self):
"""Count the total number of results
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='prismic',
version='1.5.0',
version='1.6.0',
description='Prismic.io development kit',
author='The Prismic.io Team',
author_email='[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_images(self):
url = doc.get_image('all.image').url
self.assertEqual(
url,
'https://prismic-io.s3.amazonaws.com/micro/e185bb021862c2c03a96bea92e170830908c39a3_thermometer.png')
'https://micro.cdn.prismic.io/micro/e185bb021862c2c03a96bea92e170830908c39a3_thermometer.png')

def test_date(self):
api = prismic.get('https://micro.prismic.io/api')
Expand Down