MCPcopy
hub / github.com/django/django / _store

Method _store

django/contrib/messages/storage/cookie.py:130–173  ·  view source on GitHub ↗

Store the messages to a cookie and return a list of any messages which could not be stored. If the encoded data is larger than ``max_cookie_size``, remove messages until the data fits (these are the messages which are returned), and add the not_finished sent

(self, messages, response, remove_oldest=True, *args, **kwargs)

Source from the content-addressed store, hash-verified

128 )
129
130 def _store(self, messages, response, remove_oldest=True, *args, **kwargs):
131 """
132 Store the messages to a cookie and return a list of any messages which
133 could not be stored.
134
135 If the encoded data is larger than ``max_cookie_size``, remove
136 messages until the data fits (these are the messages which are
137 returned), and add the not_finished sentinel value to indicate as much.
138 """
139 unstored_messages = []
140 serialized_messages = MessagePartSerializer().dumps(messages)
141 encoded_data = self._encode_parts(serialized_messages)
142 if self.max_cookie_size:
143 # data is going to be stored eventually by SimpleCookie, which
144 # adds its own overhead, which we must account for.
145 cookie = SimpleCookie() # create outside the loop
146
147 def is_too_large_for_cookie(data):
148 return data and len(cookie.value_encode(data)[1]) > self.max_cookie_size
149
150 def compute_msg(some_serialized_msg):
151 return self._encode_parts(
152 [*some_serialized_msg, self.not_finished_json],
153 encode_empty=True,
154 )
155
156 if is_too_large_for_cookie(encoded_data):
157 if remove_oldest:
158 idx = bisect_keep_right(
159 serialized_messages,
160 fn=lambda m: is_too_large_for_cookie(compute_msg(m)),
161 )
162 unstored_messages = messages[:idx]
163 encoded_data = compute_msg(serialized_messages[idx:])
164 else:
165 idx = bisect_keep_left(
166 serialized_messages,
167 fn=lambda m: is_too_large_for_cookie(compute_msg(m)),
168 )
169 unstored_messages = messages[idx:]
170 encoded_data = compute_msg(serialized_messages[:idx])
171
172 self._update_cookie(encoded_data, response)
173 return unstored_messages
174
175 def _encode_parts(self, messages, encode_empty=False):
176 """

Callers

nothing calls this directly

Calls 6

_encode_partsMethod · 0.95
_update_cookieMethod · 0.95
bisect_keep_rightFunction · 0.85
bisect_keep_leftFunction · 0.85
dumpsMethod · 0.45

Tested by

no test coverage detected