# encoding: utf-8
# Complaint
from sqlalchemy import (
Column,
DateTime,
ForeignKey,
Integer,
String,
)
from sqlalchemy.orm.session import Session
from . import (
Base,
create,
get_one_or_create,
)
from ..util.datetime_helpers import utc_now
[docs]class Complaint(Base):
"""A complaint about a LicensePool (or, potentially, something else)."""
__tablename__ = 'complaints'
VALID_TYPES = set([
"http://librarysimplified.org/terms/problem/" + x
for x in [
'wrong-genre',
'wrong-audience',
'wrong-age-range',
'wrong-title',
'wrong-medium',
'wrong-author',
'bad-cover-image',
'bad-description',
'cannot-fulfill-loan',
'cannot-issue-loan',
'cannot-render',
'cannot-return',
]
])
LICENSE_POOL_TYPES = [
'cannot-fulfill-loan',
'cannot-issue-loan',
'cannot-render',
'cannot-return',
]
id = Column(Integer, primary_key=True)
# One LicensePool can have many complaints lodged against it.
license_pool_id = Column(
Integer, ForeignKey('licensepools.id'), index=True)
# The type of complaint.
type = Column(String, nullable=False, index=True)
# The source of the complaint.
source = Column(String, nullable=True, index=True)
# Detailed information about the complaint.
detail = Column(String, nullable=True)
timestamp = Column(DateTime(timezone=True), nullable=False)
# When the complaint was resolved.
resolved = Column(DateTime(timezone=True), nullable=True)
[docs] @classmethod
def register(self, license_pool, type, source, detail, resolved=None):
"""Register a problem detail document as a Complaint against the
given LicensePool.
"""
if not license_pool:
raise ValueError("No license pool provided")
_db = Session.object_session(license_pool)
if type not in self.VALID_TYPES:
raise ValueError("Unrecognized complaint type: %s" % type)
now = utc_now()
if source:
complaint, is_new = get_one_or_create(
_db, Complaint,
license_pool=license_pool,
source=source, type=type,
resolved=resolved,
on_multiple='interchangeable',
create_method_kwargs = dict(
timestamp=now,
)
)
complaint.timestamp = now
complaint.detail = detail
else:
complaint, is_new = create(
_db,
Complaint,
license_pool=license_pool,
source=source,
type=type,
timestamp=now,
detail=detail,
resolved=resolved
)
return complaint, is_new
@property
def for_license_pool(self):
return any(self.type.endswith(t) for t in self.LICENSE_POOL_TYPES)
[docs] def resolve(self):
self.resolved = utc_now()
return self.resolved