Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def post(self, project_id, document_id):
obj = self.get_json()
document = self.get_document(project_id, document_id, True)
start, end = obj['start_offset'], obj['end_offset']
snippet = extract.extract(document.contents, start, end)
hl = database.Highlight(document=document,
start_offset=start,
end_offset=end,
snippet=snippet)
self.db.add(hl)
self.db.flush() # Need to flush to get hl.id
self.db.bulk_insert_mappings(database.HighlightTag, [
dict(
highlight_id=hl.id,
tag_id=tag,
)
for tag in obj.get('tags', [])
])
cmd = database.Command.highlight_add(
self.current_user,
document,
hl,
def get(self, project_id, path):
project = self.get_project(project_id)
tag = aliased(database.Tag)
hltag = aliased(database.HighlightTag)
highlights = (
self.db.query(database.Highlight)
.options(joinedload(database.Highlight.document))
.join(hltag, hltag.highlight_id == database.Highlight.id)
.join(tag, hltag.tag_id == tag.id)
.filter(tag.path.startswith(path))
.filter(tag.project == project)
).all()
html = self.render_string('export_highlights.html', path=path,
highlights=highlights)
return 'path', html
def get(self, project_id, path):
project, _ = self.get_project(project_id)
if path:
tag = aliased(database.Tag)
hltag = aliased(database.HighlightTag)
highlights = (
self.db.query(database.Highlight)
.join(hltag, hltag.highlight_id == database.Highlight.id)
.join(tag, hltag.tag_id == tag.id)
.filter(tag.path.startswith(path))
.filter(tag.project == project)
.order_by(database.Highlight.document_id,
database.Highlight.start_offset)
).all()
else:
# Special case to select all highlights: we also need to select
# highlights that have no tag at all
document = aliased(database.Document)
highlights = (
self.db.query(database.Highlight)
.join(document, document.id == database.Highlight.document_id)
.filter(document.project == project)
.order_by(database.Highlight.document_id,
database.Highlight.start_offset)
).all()
return self.send_json({
'highlights': [
def get(self, project_id):
project, privileges = self.get_project(project_id)
if not privileges.can_delete_project():
self.set_status(403)
return self.finish(self.gettext(
"You don't have permission to delete this project",
))
doc = aliased(database.Document)
highlights = (
self.db.query(database.Highlight)
.join(doc, database.Highlight.document_id == doc.id)
.filter(doc.project_id == project.id)
).count()
return self.render('project_delete.html', project=project,
documents=len(project.documents),
tags=len(project.tags),
highlights=highlights)
def delete(self, project_id, document_id, highlight_id):
document, privileges = self.get_document(project_id, document_id)
if not privileges.can_delete_highlight():
self.set_status(403)
return self.send_json({'error': "Unauthorized"})
hl = self.db.query(database.Highlight).get(int(highlight_id))
if hl is None or hl.document_id != document.id:
self.set_status(404)
return self.send_json({'error': "No such highlight"})
old_tags = list(
self.db.query(database.HighlightTag)
.filter(database.HighlightTag.highlight == hl)
.all()
)
old_tags = [hl_tag.tag_id for hl_tag in old_tags]
self.db.delete(hl)
cmd = database.Command.highlight_delete(
self.current_user,
document,
hl.id,
)
cmd.tag_count_changes = {tag: -1 for tag in old_tags}
def get(self, project_id, path):
project = self.get_project(project_id)
tag = aliased(database.Tag)
hltag = aliased(database.HighlightTag)
highlights = (
self.db.query(database.Highlight)
.options(joinedload(database.Highlight.document))
.join(hltag, hltag.highlight_id == database.Highlight.id)
.join(tag, hltag.tag_id == tag.id)
.filter(tag.path.startswith(path))
.filter(tag.project == project)
).all()
html = self.render_string('export_highlights.html', path=path,
highlights=highlights)
return 'path', html
.join(tag, hltag.tag_id == tag.id)
.filter(tag.path.startswith(path))
.filter(tag.project == project)
.order_by(database.Highlight.document_id,
database.Highlight.start_offset)
).all()
else:
# Special case to select all highlights: we also need to select
# highlights that have no tag at all
document = aliased(database.Document)
highlights = (
self.db.query(database.Highlight)
.join(document, document.id == database.Highlight.document_id)
.filter(document.project == project)
.order_by(database.Highlight.document_id,
database.Highlight.start_offset)
).all()
return self.send_json({
'highlights': [
{
'id': hl.id,
'document_id': hl.document_id,
'content': hl.snippet,
'tags': [t.id for t in hl.tags],
}
for hl in highlights
],
def post(self, project_id, document_id):
document, privileges = self.get_document(project_id, document_id, True)
if not privileges.can_add_highlight():
self.set_status(403)
return self.send_json({'error': "Unauthorized"})
obj = self.get_json()
start, end = obj['start_offset'], obj['end_offset']
snippet = extract.extract(document.contents, start, end)
hl = database.Highlight(document=document,
start_offset=start,
end_offset=end,
snippet=snippet)
self.db.add(hl)
self.db.flush() # Need to flush to get hl.id
new_tags = sorted(set(obj.get('tags', [])))
self.db.bulk_insert_mappings(database.HighlightTag, [
dict(
highlight_id=hl.id,
tag_id=tag,
)
for tag in new_tags
])
cmd = database.Command.highlight_add(
self.current_user,
document,
.join(tag, hltag.tag_id == tag.id)
.filter(tag.path.startswith(path))
.filter(tag.project == project)
.order_by(database.Highlight.document_id,
database.Highlight.start_offset)
).all()
name = None
else:
# Special case to select all highlights: we also need to select
# highlights that have no tag at all
document = aliased(database.Document)
highlights = (
self.db.query(database.Highlight)
.join(document, document.id == database.Highlight.document_id)
.filter(document.project == project)
.order_by(database.Highlight.document_id,
database.Highlight.start_offset)
).all()
name = 'all_tags'
return name, highlights
tag = aliased(database.Tag)
hltag = aliased(database.HighlightTag)
highlights = (
self.db.query(database.Highlight)
.join(hltag, hltag.highlight_id == database.Highlight.id)
.join(tag, hltag.tag_id == tag.id)
.filter(tag.path.startswith(path))
.filter(tag.project == project)
).all()
else:
# Special case to select all highlights: we also need to select
# highlights that have no tag at all
document = aliased(database.Document)
highlights = (
self.db.query(database.Highlight)
.join(document, document.id == database.Highlight.document_id)
.filter(document.project == project)
).all()
self.send_json({
'highlights': [
{
'id': hl.id,
'document_id': hl.document_id,
'content': hl.snippet,
'tags': [t.id for t in hl.tags],
}
for hl in highlights
],