This repository was archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuser_profile.py
More file actions
188 lines (154 loc) · 6.67 KB
/
user_profile.py
File metadata and controls
188 lines (154 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import json
from .problem_details import *
from flask_babel import lazy_gettext as _
class ProfileController(object):
"""Implement the User Profile Management Protocol.
https://github.com/NYPL-Simplified/Simplified/wiki/User-Profile-Management-Protocol
"""
MEDIA_TYPE = "vnd.librarysimplified/user-profile+json"
LINK_RELATION = "http://librarysimplified.org/terms/rel/user-profile"
def __init__(self, storage):
"""Constructor.
:param storage: An instance of ProfileStorage.
"""
self.storage = storage
def get(self):
"""Turn the storage object into a Profile document and send out its
JSON-based representation.
:param return: A ProblemDetail if there is a problem; otherwise,
a 3-tuple (entity-body, response code, headers)
"""
profile_document = None
try:
profile_document = self.storage.profile_document
except Exception as e:
if hasattr(e, 'as_problem_detail_document'):
return e.as_problem_detail_document()
else:
return INTERNAL_SERVER_ERROR.with_debug(str(e))
if not isinstance(profile_document, dict):
return INTERNAL_SERVER_ERROR.with_debug(
_("Profile document is not a JSON object: %r.") % (
profile_document
)
)
try:
body = json.dumps(profile_document)
except Exception as e:
return INTERNAL_SERVER_ERROR.with_debug(
_("Could not convert profile document to JSON: %r.") % (
profile_document
)
)
return body, 200, {"Content-Type": self.MEDIA_TYPE}
def put(self, headers, body):
"""Update the profile storage object with new settings
from a Profile document sent with a PUT request.
:param return: A ProblemDetail if there is a problem; otherwise,
a 3-tuple (response code, media type, entity-body)
"""
media_type = headers.get('Content-Type')
if media_type != self.MEDIA_TYPE:
return UNSUPPORTED_MEDIA_TYPE.detailed(
_("Expected %s") % self.MEDIA_TYPE
)
try:
profile_document = json.loads(body)
except Exception as e:
return INVALID_INPUT.detailed(
_("Submitted profile document was not valid JSON.")
)
if not isinstance(profile_document, dict):
return INVALID_INPUT.detailed(
_("Submitted profile document was not a JSON object.")
)
new_settings = profile_document.get(ProfileStorage.SETTINGS_KEY)
if new_settings:
# The incoming document is a request to change at least one
# setting in the profile.
writable = set(self.storage.writable_setting_names)
for k in list(new_settings.keys()):
# A Profile document is invalid if it attempts to
# change the value of a read-only profile setting.
if k not in writable:
return INVALID_INPUT.detailed(
_('"%s" is not a writable setting.' % k)
)
try:
# Update the profile storage with the new settings.
self.storage.update(new_settings, profile_document)
except Exception as e:
# There was a problem updating the profile storage.
if hasattr(e, 'as_problem_detail_document'):
return e.as_problem_detail_document()
else:
return INTERNAL_SERVER_ERROR.with_debug(str(e))
return body, 200, {"Content-Type": "text/plain"}
class ProfileStorage(object):
"""An abstract class defining a specific user's profile.
Subclasses should get profile information from somewhere specific,
e.g. a database row.
An instance of this class is responsible for one specific user's profile,
not the set of all profiles.
"""
NS = 'simplified:'
FINES = NS + 'fines'
AUTHORIZATION_IDENTIFIER = NS + "authorization_identifier"
AUTHORIZATION_EXPIRES = NS + "authorization_expires"
SYNCHRONIZE_ANNOTATIONS = NS + 'synchronize_annotations'
SETTINGS_KEY = 'settings'
@property
def profile_document(self):
"""Create a Profile document representing the current state of
the user's profile.
:return: A dictionary that can be serialized as JSON.
"""
raise NotImplementedError()
def update(self, new_values, profile_document):
"""(Try to) change the user's profile so it looks like the provided
Profile document.
:param new_values: A dictionary of settings that the
client wants to change.
:param profile_document: The full Profile document as provided
by the client. Should not be necessary, but provided in
case it's useful.
:raise Exception: If there's a problem making the user's profile
look like the provided Profile document.
"""
raise NotImplementedError()
@property
def writable_setting_names(self):
"""Return the subset of settings that are considered writable.
An attempt to modify a setting that's not in this list will fail
before update() is called.
:return: An iterable.
"""
raise NotImplementedError()
class MockProfileStorage(ProfileStorage):
"""A profile storage object for use in tests.
Keeps information in in-memory dictionaries rather than in a database.
"""
def __init__(self, read_only_settings=None, writable_settings=None):
"""Create a profile for a simulated user.
:param read_only_settings: A dictionary of values that cannot be
changed.
:param writable_settings: A dictionary of values that can be changed
through the User Profile Management Protocol.
"""
self.read_only_settings = read_only_settings or dict()
self.writable_settings = writable_settings or dict()
@property
def profile_document(self):
body = dict(self.read_only_settings)
body[self.SETTINGS_KEY] = dict(self.writable_settings)
return body
def update(self, new_values, profile_document):
"""(Try to) change the user's profile so it looks like the provided
Profile document.
"""
for k, v in list(new_values.items()):
self.writable_settings[k] = v
@property
def writable_setting_names(self):
"""Return the subset of fields that are considered writable."""
return list(self.writable_settings.keys())