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

Add support for multi-languages #49

Open
wants to merge 6 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='*'):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we forbid to use a wilcard for a getByUID since uids are defined per language.
https://github.com/prismicio/prismic-javascript/blob/master/src/ResolvedApi.ts#L199
you should implement the same logic there

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add a test to make sure you can't query without a lang

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='*'):
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='*'):
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.1',
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://micro.cdn.prismic.io/micro/e185bb021862c2c03a96bea92e170830908c39a3_thermometer.png')
'https://images.prismic.io/micro/e185bb021862c2c03a96bea92e170830908c39a3_thermometer.png?auto=compress,format')

def test_date(self):
api = prismic.get('https://micro.prismic.io/api')
Expand Down
14 changes: 14 additions & 0 deletions tests/test_prismic.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,30 @@ def test_get_by_uid(self):
doc = self.api.get_by_uid('all', 'all')
self.assertEqual(doc.id, 'WHx-gSYAAMkyXYX_')

def test_get_by_uid_lang(self):
doc = self.api.get_by_uid('all', 'all', lang = 'en-us')
self.assertEqual(doc.id, 'WHx-gSYAAMkyXYX_')

def test_get_by_id(self):
doc = self.api.get_by_id('WHx-gSYAAMkyXYX_')
self.assertEqual(doc.id, 'WHx-gSYAAMkyXYX_')

def test_get_by_id_lang(self):
doc = self.api.get_by_id('WHx-gSYAAMkyXYX_', lang='en-us')
self.assertEqual(doc.id, 'WHx-gSYAAMkyXYX_')

def test_get_by_ids(self):
result = self.api.get_by_ids(['WHx-gSYAAMkyXYX_', 'WHyJqyYAAHgyXbcj'])
ids = sorted([doc.id for doc in result.documents])
self.assertEqual(ids[0], 'WHx-gSYAAMkyXYX_')
self.assertEqual(ids[1], 'WHyJqyYAAHgyXbcj')

def test_get_by_ids_lang(self):
result = self.api.get_by_ids(['WHx-gSYAAMkyXYX_', 'WHyJqyYAAHgyXbcj'], lang="en-us")
ids = sorted([doc.id for doc in result.documents])
self.assertEqual(ids[0], 'WHx-gSYAAMkyXYX_')
self.assertEqual(ids[1], 'WHyJqyYAAHgyXbcj')

def test_get_single(self):
doc = self.api.get_single('single')
self.assertEqual(doc.id, 'V_OplCUAACQAE0lA')
Expand Down