Skip to content

Commit

Permalink
Merge branch 'master' of github.com:blockspeiser/Sefaria-Project
Browse files Browse the repository at this point in the history
  • Loading branch information
blockspeiser committed May 28, 2015
2 parents 7e9adce + e8a193c commit f570a38
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 18 deletions.
74 changes: 74 additions & 0 deletions reader/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,80 @@ def test_logged_in(self):
self.assertTrue(response.content.find("accountMenuName") > -1)


class PostV2IndexTest(SefariaTestCase):
def setUp(self):
self.make_test_user()

def tearDown(self):
IndexSet({"title": "Complex Book"}).delete()

def test_add_alt_struct(self):
# Add a simple Index
index = {
"title": "Complex Book",
"titleVariants": [],
"sectionNames": ["Chapter", "Paragraph"],
"categories": ["Musar"],
}
response = c.post("/api/index/Complex_Book", {'json': json.dumps(index)})
self.assertEqual(200, response.status_code)
data = json.loads(response.content)
self.assertNotIn("error", data)

# Get it in raw v2 form
response = c.get("/api/v2/raw/index/Complex_Book")
data = json.loads(response.content)
self.assertNotIn("error", data)

# Add some alt structs to it
data["alt_structs"] = {
"Special Sections" : {
"nodes": [
{
"nodeType": "ArrayMapNode",
"depth": 1,
"titles": [
{
"lang": "en",
"text": "Idrah Rabbah",
"primary": True
},
{
"lang": "he",
"text": u"אידרה רבה",
"primary": True
}
],
"addressTypes": [
"Integer"
],
"sectionNames": [
"Paragraph"
],
"wholeRef": "Complex Book 3:4-7:1",
"refs" : [
"Complex Book 3:4-4:1",
"Complex Book 4:2-6:3",
"Complex Book 6:4-7:1"
]
}
]
}
}
# Save
response = c.post("/api/v2/raw/index/Complex_Book", {'json': json.dumps(data)})
self.assertEqual(200, response.status_code)
data = json.loads(response.content)
self.assertNotIn("error", data)

# Load and validate alt structs
response = c.get("/api/v2/raw/index/Complex_Book")
data = json.loads(response.content)
self.assertNotIn("error", data)
self.assertIn("alt_structs", data)
self.assertIn("Special Sections", data["alt_structs"])


class PostIndexTest(SefariaTestCase):
def setUp(self):
self.make_test_user()
Expand Down
10 changes: 5 additions & 5 deletions reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,15 +552,15 @@ def index_node_api(request, title):

@catch_error_as_json
@csrf_exempt
def index_api(request, title):
def index_api(request, title, v2=False, raw=False):
"""
API for manipulating text index records (aka "Text Info")
"""
if request.method == "GET":
try:
i = model.get_index(title).contents()
i = model.get_index(title).contents(v2=v2, raw=raw)
except InputError as e:
node = library.get_schema_node(title)
node = library.get_schema_node(title) # If the request were for v1 and fails, this falls back to v2.
if not node:
raise e
i = node.as_index_contents()
Expand All @@ -581,7 +581,7 @@ def index_api(request, title):
apikey = db.apikeys.find_one({"key": key})
if not apikey:
return jsonResponse({"error": "Unrecognized API key."})
return jsonResponse(func(apikey["uid"], model.Index, j, method="API").contents())
return jsonResponse(func(apikey["uid"], model.Index, j, method="API", v2=v2, raw=raw).contents(v2=v2, raw=raw))
else:
title = j.get("oldTitle", j.get("title"))
try:
Expand All @@ -593,7 +593,7 @@ def index_api(request, title):
@csrf_protect
def protected_index_post(request):
return jsonResponse(
func(request.user.id, model.Index, j).contents()
func(request.user.id, model.Index, j, v2=v2, raw=raw).contents(v2=v2, raw=raw)
)
return protected_index_post(request)

Expand Down
23 changes: 23 additions & 0 deletions sefaria/helper/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
from sefaria.datatype.jagged_array import JaggedTextArray


def add_spelling(category, old, new, lang="en"):
"""
For a given category, on every index in that title that matches 'old' create a new title with 'new' replacing 'old'
:param category:
:param old:
:param new:
:return:
"""
indxs = library.get_indexes_in_category(category)
for ind in indxs:
i = get_index(ind)
print
assert isinstance(i, Index)
schema = i.nodes
assert isinstance(schema, JaggedArrayNode)
for title in schema.all_node_titles(lang):
if old in title:
new_title = title.replace(old, new)
print new_title
schema.add_title(new_title, lang)
i.save()


def create_commentator_and_commentary_version(commentator_name, existing_book, lang, vtitle, vsource):
existing_index = Index().load({'title':existing_book})
if existing_index is None:
Expand Down
2 changes: 1 addition & 1 deletion sefaria/model/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _saveable_attr_keys(self):
def _saveable_attrs(self):
return {k: getattr(self, k) for k in self._saveable_attr_keys() if hasattr(self, k)}

def contents(self):
def contents(self, **kwargs):
""" Build a savable/portable dictionary from the object
Extended by subclasses with derived attributes passed along with portable object
:return: dict
Expand Down
2 changes: 1 addition & 1 deletion sefaria/model/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _saveable_attr_keys(self):
attrs.remove("categories")
return attrs

def contents(self):
def contents(self, **kwargs):
attrs = super(Count, self).contents()
for key in self.index_attr_keys:
attrs[key] = getattr(self, key, None)
Expand Down
6 changes: 3 additions & 3 deletions sefaria/model/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


class AbstractIndex(object):
def contents(self, v2=False, raw=False):
def contents(self, v2=False, raw=False, **kwargs):
pass

def is_new_style(self):
Expand Down Expand Up @@ -123,7 +123,7 @@ def _set_derived_attributes(self):
def is_complex(self):
return getattr(self, "nodes", None) and self.nodes.has_children()

def contents(self, v2=False, raw=False):
def contents(self, v2=False, raw=False, **kwargs):
if not getattr(self, "nodes", None) or raw: # Commentator
return super(Index, self).contents()
elif v2:
Expand Down Expand Up @@ -549,7 +549,7 @@ def toc_contents(self):
}

#todo: this needs help
def contents(self, v2=False, raw=False):
def contents(self, v2=False, raw=False, **kwargs):
if v2:
return self.nodes.as_index_contents()

Expand Down
2 changes: 1 addition & 1 deletion sefaria/model/translation_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def check_complete(self):
return True
return False

def contents(self):
def contents(self, **kwargs):
contents = super(TranslationRequest, self).contents()
contents["first_requested"] = contents["first_requested"].isoformat()
contents["last_requested"] = contents["last_requested"].isoformat()
Expand Down
2 changes: 1 addition & 1 deletion sefaria/model/version_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__(self, index=None, attrs=None):
self.refresh()
self.is_new_state = True # variable naming: don't override 'is_new' - a method of the superclass

def contents(self):
def contents(self, **kwargs):
c = super(VersionState, self).contents()
c.update(self.index.contents())
return c
Expand Down
12 changes: 6 additions & 6 deletions sefaria/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def add(user, klass, attrs, **kwargs):
else:
obj = klass().load({klass.criteria_field: attrs[klass.criteria_field]})
if obj:
old_dict = obj.contents()
old_dict = obj.contents(**kwargs)
obj.load_from_dict(attrs).save()
model.log_update(user, klass, old_dict, obj.contents(), **kwargs)
model.log_update(user, klass, old_dict, obj.contents(**kwargs), **kwargs)
return obj
obj = klass(attrs).save()
model.log_add(user, klass, obj.contents(), **kwargs)
model.log_add(user, klass, obj.contents(**kwargs), **kwargs)
return obj


Expand All @@ -77,15 +77,15 @@ def update(user, klass, attrs, **kwargs):
obj = klass().load_by_id(attrs[klass.id_field])
else:
obj = klass().load({klass.criteria_field: attrs[klass.criteria_field]})
old_dict = obj.contents()
old_dict = obj.contents(**kwargs)
obj.load_from_dict(attrs).save()
model.log_update(user, klass, old_dict, obj.contents(), **kwargs)
model.log_update(user, klass, old_dict, obj.contents(**kwargs), **kwargs)
return obj


def delete(user, klass, _id, **kwargs):
obj = klass().load_by_id(_id)
old_dict = obj.contents()
old_dict = obj.contents(**kwargs)
obj.delete()
model.log_delete(user, klass, old_dict, **kwargs)
return {"response": "ok"}
Expand Down
2 changes: 2 additions & 0 deletions sefaria/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
(r'^api/texts/(?P<tref>.+)$', 'texts_api'),
(r'^api/index/?$', 'table_of_contents_api'),
(r'^api/index/titles/?$', 'text_titles_api'),
(r'^api/v2/raw/index/(?P<title>.+)$', 'index_api', {'v2': True, 'raw': True}),
(r'^api/v2/index/(?P<title>.+)$', 'index_api', {'v2': True}),
(r'^api/index/(?P<title>.+)$', 'index_api'),
(r'^api/links/bare/(?P<book>.+)/(?P<cat>.+)$', 'bare_link_api'),
(r'^api/links/(?P<link_id_or_ref>.*)$', 'links_api'),
Expand Down

0 comments on commit f570a38

Please sign in to comment.