Skip to content
Draft
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
18 changes: 18 additions & 0 deletions lib/id3c/api/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ def make_identifier_set(session: DatabaseSession, name: str, **fields) -> bool:
return cursor.rowcount == 1


@export
@catch_permission_denied
def mint_identifiers(session: DatabaseSession, name: str, n: int) -> None:
"""
Generate *n* new identifiers in the set *name*.

Raises a :class:`~werkzeug.exceptions.NotFound` exception if the set *name*
doesn't exist and a :class:`Forbidden` exception if the database reports a
`permission denied` error.
"""
with session:
try:
return db.mint_identifiers(session, name, n)

except db.IdentifierSetNotFoundError as error:
raise NotFound(str(error)) from None


@export
class BadRequestDatabaseError(BadRequest):
"""
Expand Down
29 changes: 29 additions & 0 deletions lib/id3c/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import pkg_resources
from flask import Blueprint, jsonify, request, send_file
from werkzeug.exceptions import BadRequest
from . import datastore
from .utils.routes import authenticated_datastore_session_required, content_types_accepted, check_content_length

Expand Down Expand Up @@ -222,6 +223,34 @@ def put_identifier_set(name, *, session):
return "", 201 if new_set else 204


@api_v1.route("/warehouse/identifier-sets/<name>/identifiers", methods = ['POST'])
@content_types_accepted(["application/x-www-form-urlencoded", "multipart/form-data"])
@check_content_length
@authenticated_datastore_session_required
def mint_in_identifier_set(name, *, session):
"""
Mint *n* new identifiers in the set *name*.

POST /warehouse/identifier-sets/*name*/identifiers with an *n* form
parameter specifying how many new identifiers you'd like to mint. Responds
with a JSON array of objects (the new identifiers) containing the keys
``uuid``, ``barcode``, and ``generated``.

This may take some time with a large *n* or with a lot of existing
identifiers.
"""
try:
n = int(request.form['n'])
except ValueError as error:
raise BadRequest("n must be a plain integer")

LOG.debug(f"Minting {n} new identifiers in set «{name}»")

minted = datastore.mint_identifiers(session, name, n)

return jsonify([ identifier._asdict() for identifier in minted ])


# Load all extra API routes from extensions
# Needs to be at the end of route declarations to allow customization of
# existing routes and avoid dependency errors
Expand Down