Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _first(**spec) -> typing.Optional[entry.Entry]:
""" Get the earliest entry in this category, optionally including subcategories """
for record in self._entries(spec).order_by(model.Entry.local_date,
model.Entry.id)[:1]:
return entry.Entry.load(record)
return None
def _check_authorization(record, category):
""" Check the auth of an entry against the current user """
if record.auth:
cur_user = user.get_active()
authorized = record.is_authorized(cur_user)
user.log_access(record, cur_user, authorized)
if not authorized:
return handle_unauthorized(cur_user,
entry=Entry.load(record),
category=category)
return None
if result:
return result
# if the entry canonically redirects, do that now
if record.redirect_url:
LOGGER.debug("Redirecting to %s", record.redirect_url)
return redirect(record.redirect_url)
# check if the canonical URL matches
if not _mounted:
result = _check_canon_entry_url(record)
if result:
return result
# Get the viewable entry
entry_obj = Entry.load(record)
# does the entry-id header mismatch? If so the old one is invalid
try:
current_id: typing.Optional[int] = int(entry_obj.get('Entry-ID')) # type:ignore
except (KeyError, TypeError, ValueError) as err:
LOGGER.debug("Error checking entry ID: %s", err)
current_id = record.id
if current_id != record.id:
LOGGER.debug("entry %s says it has id %d, index says %d",
entry_obj.file_path, current_id, record.id)
from .entry import scan_file
scan_file(entry_obj.file_path, None, True)
return redirect(url_for('entry', entry_id=current_id))
def _entries(unauthorized=0) -> typing.List[Entry]:
result: typing.List[Entry] = []
count = self.spec.get('count')
cur_user = user.get_active()
for record in self._entries:
if count is not None and len(result) >= count:
break
auth = record.is_authorized(cur_user)
if auth or unauthorized:
result.append(Entry.load(record))
if not auth and unauthorized is not True:
unauthorized -= 1
if not auth:
tokens.request(cur_user)
return result
def _last(**spec) -> typing.Optional[entry.Entry]:
""" Get the latest entry in this category, optionally including subcategories """
for record in self._entries(spec).order_by(orm.desc(model.Entry.local_date),
orm.desc(model.Entry.id))[:1]:
return entry.Entry.load(record)
return None
""" Remap a link or source target to an appropriate entry or image rendition """
# Resolve external URLs
if re.match(r'([a-z][a-z0-9+.\-]*:)?//', path, re.I):
return path
# Resolve static assets
if path.startswith('@'):
return utils.static_url(path[1:], absolute)
path, sep, anchor = path.partition('#')
# Resolve entries
found = find_entry(path, search_path)
if found:
return entry.Entry.load(found).permalink(absolute=absolute) + sep + anchor
# Resolve images and assets
img_path, img_args, _ = image.parse_image_spec(path)
img = image.get_image(img_path, search_path)
if not isinstance(img, image.ImageNotFound):
path, _ = img.get_rendition(**{**img_args, 'absolute': absolute})
# We don't know what this is, so just treat it like a normal URL.
if absolute:
path = urljoin(request.url, path)
return path + sep + anchor