Skip to content

Commit

Permalink
Address TODO items
Browse files Browse the repository at this point in the history
- Sanitize tag description in tag view
- Update subject key derivation in tag view
- Reduce number of DB calls required to decorate a subject page with tags
- Tag edit `GET` handler returns edit form specific to the existing tag's type
- Use correct sub-type validations on tag edit
- Remove `utils.unflatten` call when creating tag
  - `utils.unflatten` is made specifically for processing the book edit form
  • Loading branch information
jimchamp committed Nov 27, 2024
1 parent e89d144 commit e52b1fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion openlibrary/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ def get_url_suffix(self):

@classmethod
def find(cls, tag_name, tag_type=None):
"""Returns a Tag key for a given tag name and tag type."""
"""Returns a Tag key for a given tag name."""
q = {'type': '/type/tag', 'name': tag_name}
if tag_type:
q['tag_type'] = tag_type
Expand Down
18 changes: 13 additions & 5 deletions openlibrary/plugins/upstream/addtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from infogami.utils import delegate

from openlibrary.accounts import get_current_user
from openlibrary.plugins.upstream import spamcheck, utils
from openlibrary.plugins.upstream import spamcheck
from openlibrary.plugins.upstream.models import Tag
from openlibrary.plugins.upstream.addbook import safe_seeother, trim_doc
from openlibrary.plugins.upstream.utils import render_template
Expand Down Expand Up @@ -159,6 +159,11 @@ def GET(self, key):
if tag is None:
raise web.notfound()

if tag.tag_type in SUBJECT_SUB_TYPES:
return render_template("type/tag/subject/edit", tag)
elif tag.tag_type == "collection":
return render_template("type/tag/collection/edit", tag)

return render_template('type/tag/edit', tag)

def POST(self, key):
Expand All @@ -169,8 +174,7 @@ def POST(self, key):
i = web.input(_comment=None)
formdata = self.process_input(i)
try:
# TODO : use type-based validation here
if not formdata or not validate_subject_tag(formdata):
if not formdata or not self.validate(formdata, tag.tag_type):
raise web.badrequest()
elif "_delete" in i:
tag = web.ctx.site.new(
Expand All @@ -187,11 +191,15 @@ def POST(self, key):
return render_template("type/tag/edit", tag)

def process_input(self, i):
# TODO : `unflatten` call not needed for tag form data
i = utils.unflatten(i)
tag = trim_doc(i)
return tag

def validate(self, data, tag_type):
if tag_type in SUBJECT_SUB_TYPES:
return validate_subject_tag(data)
else:
return validate_tag(data)


class add_typed_tag(delegate.page):
path = "/tag/([^/]+)/add"
Expand Down
19 changes: 7 additions & 12 deletions openlibrary/plugins/worksearch/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def GET(self, key):
web.ctx.status = "404 Not Found"
page = render_template('subjects/notfound.tmpl', key)
else:
self.decorate_with_disambiguations(subj)
# TODO : use disambiguations to find linked tag
self.decorate_with_tag(subj)
self.decorate_with_tags(subj)
page = render_template("subjects", page=subj)

return page
Expand All @@ -62,17 +60,14 @@ def normalize_key(self, key):
key = key.replace("/times/", "/time:")
return key

def decorate_with_disambiguations(self, subject):
matches = Tag.find(subject.name)
if matches:
tags = web.ctx.site.get_many(matches)
def decorate_with_tags(self, subject) -> None:
tag_keys = Tag.find(subject.name)
if tag_keys:
tags = web.ctx.site.get_many(tag_keys)
subject.disambiguations = tags

def decorate_with_tag(self, subject):
matches = Tag.find(subject.name, tag_type=subject.subject_type)
if matches:
tag = web.ctx.site.get(matches[0])
subject.tag = tag
if filtered_tags := [tag for tag in tags if tag.tag_type == subject.subject_type]:
subject.tag = filtered_tags[0]


class subjects_json(delegate.page):
Expand Down
9 changes: 4 additions & 5 deletions openlibrary/templates/type/tag/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
key = "/subjects/"
if tag.tag_type != "subject":
key += "%s:" % tag.tag_type
# TODO : normalize the same as subject label URLs
key += tag.name.replace(" ", "_")
key = key.lower()
return key

<div id="contentHead">
$:macros.databarView(tag)
<h1 itemprop="name">
$title
</h1>
<h3 itemprop="description">
$# TODO : sanitize and format
$tag.tag_description
</h3>
<div itemprop="description">
$:sanitize(format(tag.tag_description))
</div>
$if tag.tag_type in get_subject_tag_types():
<div>
$:sanitize(format(tag.body))
Expand Down

0 comments on commit e52b1fe

Please sign in to comment.