| 179 | return q |
| 180 | |
| 181 | def to_proxy(self, ns=None): |
| 182 | ns = ns or self.collection.ns |
| 183 | proxy = model.get_proxy( |
| 184 | { |
| 185 | "id": ns.sign(self.id), |
| 186 | "schema": self.model, |
| 187 | "properties": {}, |
| 188 | "created_at": iso_text(self.created_at), |
| 189 | "updated_at": iso_text(self.updated_at), |
| 190 | "role_id": self.role_id, |
| 191 | "mutable": False, |
| 192 | } |
| 193 | ) |
| 194 | meta = dict(self.meta) |
| 195 | headers = meta.pop("headers", None) |
| 196 | if is_mapping(headers): |
| 197 | headers = {slugify(k, sep="_"): v for k, v in headers.items()} |
| 198 | proxy.set("headers", registry.json.pack(headers), quiet=True) |
| 199 | else: |
| 200 | headers = {} |
| 201 | proxy.set("contentHash", self.content_hash) |
| 202 | proxy.set("parent", ns.sign(self.parent_id)) |
| 203 | proxy.set("ancestors", [ns.sign(a) for a in self.ancestors]) |
| 204 | proxy.set("crawler", meta.get("crawler")) |
| 205 | proxy.set("author", meta.get("author")) |
| 206 | proxy.set("publisher", meta.get("publisher")) |
| 207 | proxy.set("sourceUrl", meta.get("source_url")) |
| 208 | proxy.set("title", meta.get("title")) |
| 209 | proxy.set("summary", meta.get("summary")) |
| 210 | proxy.set("fileName", meta.get("file_name")) |
| 211 | if not proxy.has("fileName"): |
| 212 | disposition = headers.get("content_disposition") |
| 213 | if disposition is not None: |
| 214 | _, attrs = cgi.parse_header(disposition) |
| 215 | proxy.set("fileName", attrs.get("filename")) |
| 216 | proxy.set("mimeType", meta.get("mime_type")) |
| 217 | if not proxy.has("mimeType"): |
| 218 | proxy.set("mimeType", headers.get("content_type")) |
| 219 | proxy.set("language", meta.get("languages")) |
| 220 | proxy.set("country", meta.get("countries")) |
| 221 | proxy.set("keywords", meta.get("keywords")) |
| 222 | proxy.set("date", meta.get("date")) |
| 223 | proxy.set("authoredAt", meta.get("authored_at")) |
| 224 | proxy.set("modifiedAt", meta.get("modified_at")) |
| 225 | proxy.set("publishedAt", meta.get("published_at")) |
| 226 | proxy.set("retrievedAt", meta.get("retrieved_at")) |
| 227 | return proxy |
| 228 | |
| 229 | def __repr__(self): |
| 230 | return "<Document(%r,%r)>" % (self.id, self.schema) |