Skip to main content

paperless_api/
document_query.rs

1//! Query for documents using the [`DocumentQueryBuilder`].
2
3use crate::{
4    document::ArchiveSerialNumber,
5    id::{CorrespondentId, TagId},
6};
7
8/// Builder for constructing document queries.
9#[derive(Default)]
10pub struct DocumentQueryBuilder {
11    archive_serial_number: Option<ArchiveSerialNumber>,
12    correspondent_id_in: Option<Vec<CorrespondentId>>,
13    correspondent_name_icontains: Option<String>,
14    content_icontains: Option<String>,
15    tags_id_in: Option<Vec<TagId>>,
16    pub(crate) full_content: bool,
17    full_permissions: bool,
18}
19
20/// A constructed document query.
21pub struct DocumentQuery {
22    pub(crate) query: Vec<(&'static str, String)>,
23}
24
25pub(crate) const QUERY_PARAM_FULL_PERMISSIONS: &str = "full_perms";
26pub(crate) const QUERY_PARAM_TRUNCATE_CONTENT: &str = "truncate_content";
27const QUERY_PARAM_TAGS_ID_IN: &str = "tags__id__in";
28const QUERY_PARAM_ARCHIVE_SERIAL_NUMBER: &str = "archive_serial_number";
29const QUERY_PARAM_CORRESPONDENT_ID_IN: &str = "correspondent__id__in";
30const QUERY_PARAM_CORRESPONDENT_NAME_ICONTAINS: &str = "correspondent__name__icontains";
31const QUERY_PARAM_CONTENT_ICONTAINS: &str = "content__icontains";
32
33impl DocumentQueryBuilder {
34    /// Filters documents which have the given archive serial number.
35    #[must_use]
36    pub fn archive_serial_number(mut self, archive_serial_number: ArchiveSerialNumber) -> Self {
37        self.archive_serial_number = Some(archive_serial_number);
38        self
39    }
40
41    /// Filters documents which have any of the given correspondents.
42    #[must_use]
43    pub fn correspondent_id_in(mut self, correspondent_id_in: Vec<CorrespondentId>) -> Self {
44        self.correspondent_id_in = Some(correspondent_id_in);
45        self
46    }
47
48    /// Filters documents which have a correspondent name containing the given string.
49    #[must_use]
50    pub fn correspondent_name_icontains(mut self, correspondent_name_icontains: String) -> Self {
51        self.correspondent_name_icontains = Some(correspondent_name_icontains);
52        self
53    }
54
55    /// Filters documents which have content containing the given string.
56    #[must_use]
57    pub fn content_icontains(mut self, content_icontains: String) -> Self {
58        self.content_icontains = Some(content_icontains);
59        self
60    }
61
62    /// Filters documents which have any of the given tags.
63    #[must_use]
64    pub fn tags_id_in(mut self, tags_id_in: Vec<TagId>) -> Self {
65        self.tags_id_in = Some(tags_id_in);
66        self
67    }
68
69    /// Returns documents with full content (truncated by default to save bandwidth).
70    #[must_use]
71    pub fn full_content(mut self, full_content: bool) -> Self {
72        self.full_content = full_content;
73        self
74    }
75
76    /// Returns documents with full permissions data.
77    #[must_use]
78    pub fn full_permissions(mut self, full_permissions: bool) -> Self {
79        self.full_permissions = full_permissions;
80        self
81    }
82
83    /// Builds the query.
84    #[must_use]
85    pub fn build(self) -> DocumentQuery {
86        let mut query = vec![];
87
88        if let Some(archive_serial_number) = self.archive_serial_number {
89            query.push((
90                QUERY_PARAM_ARCHIVE_SERIAL_NUMBER,
91                archive_serial_number.0.to_string(),
92            ));
93        }
94        if let Some(correspondent_id_in) = self.correspondent_id_in {
95            query.push((
96                QUERY_PARAM_CORRESPONDENT_ID_IN,
97                correspondent_id_in
98                    .iter()
99                    .map(|id| id.0.to_string())
100                    .collect::<Vec<_>>()
101                    .join(","),
102            ));
103        }
104        if let Some(correspondent_name_icontains) = self.correspondent_name_icontains {
105            query.push((
106                QUERY_PARAM_CORRESPONDENT_NAME_ICONTAINS,
107                correspondent_name_icontains,
108            ));
109        }
110        if let Some(content_icontains) = self.content_icontains {
111            query.push((QUERY_PARAM_CONTENT_ICONTAINS, content_icontains));
112        }
113        if let Some(tags_id_in) = self.tags_id_in {
114            query.push((
115                QUERY_PARAM_TAGS_ID_IN,
116                tags_id_in
117                    .iter()
118                    .map(|id| id.0.to_string())
119                    .collect::<Vec<_>>()
120                    .join(","),
121            ));
122        }
123        if !self.full_content {
124            query.push((QUERY_PARAM_TRUNCATE_CONTENT, "false".to_string()));
125        }
126
127        if self.full_permissions {
128            query.push((QUERY_PARAM_FULL_PERMISSIONS, "true".to_string()));
129        }
130
131        DocumentQuery { query }
132    }
133}
134/*
135added__date__gt
136string($date)
137(query)
138
139added__date__gte
140string($date)
141(query)
142
143added__date__lt
144string($date)
145(query)
146
147added__date__lte
148string($date)
149(query)
150
151added__day
152number
153(query)
154
155added__gt
156string($date-time)
157(query)
158
159added__gte
160string($date-time)
161(query)
162
163added__lt
164string($date-time)
165(query)
166
167added__lte
168string($date-time)
169(query)
170
171added__month
172number
173(query)
174
175added__year
176number
177(query)
178
179content__icontains
180string
181(query)
182
183content__iendswith
184string
185(query)
186
187content__iexact
188string
189(query)
190
191content__istartswith
192string
193(query)
194
195correspondent__id
196integer
197(query)
198
199correspondent__id__in
200array<integer>
201(query)
202Mehrere Werte können durch Kommas getrennt sein.
203Add integer item
204correspondent__id__none
205integer
206(query)
207
208correspondent__isnull
209boolean
210(query)
211
212correspondent__name__icontains
213string
214(query)
215
216correspondent__name__iendswith
217string
218(query)
219
220correspondent__name__iexact
221string
222(query)
223
224correspondent__name__istartswith
225string
226(query)
227
228created__date__gt
229string($date)
230(query)
231
232created__date__gte
233string($date)
234(query)
235
236created__date__lt
237string($date)
238(query)
239
240created__date__lte
241string($date)
242(query)
243
244created__day
245number
246(query)
247
248created__gt
249string($date)
250(query)
251
252created__gte
253string($date)
254(query)
255
256created__lt
257string($date)
258(query)
259
260created__lte
261string($date)
262(query)
263
264created__month
265number
266(query)
267
268created__year
269number
270(query)
271
272custom_field_query
273string
274(query)
275
276custom_fields__icontains
277string
278(query)
279
280custom_fields__id__all
281integer
282(query)
283
284custom_fields__id__in
285integer
286(query)
287
288custom_fields__id__none
289integer
290(query)
291
292document_type__id
293integer
294(query)
295
296document_type__id__in
297array<integer>
298(query)
299Mehrere Werte können durch Kommas getrennt sein.
300Add integer item
301document_type__id__none
302integer
303(query)
304
305document_type__isnull
306boolean
307(query)
308
309document_type__name__icontains
310string
311(query)
312
313document_type__name__iendswith
314string
315(query)
316
317document_type__name__iexact
318string
319(query)
320
321document_type__name__istartswith
322string
323(query)
324
325fields
326array<string>
327(query)
328Add string item
329full_perms
330boolean
331(query)
332
333has_custom_fields
334boolean
335(query)
336Has custom field
337
338id
339integer
340(query)
341
342id__in
343array<integer>
344(query)
345Mehrere Werte können durch Kommas getrennt sein.
346Add integer item
347is_in_inbox
348boolean
349(query)
350
351is_tagged
352boolean
353(query)
354Is tagged
355
356mime_type
357string
358(query)
359
360modified__date__gt
361string($date)
362(query)
363
364modified__date__gte
365string($date)
366(query)
367
368modified__date__lt
369string($date)
370(query)
371
372modified__date__lte
373string($date)
374(query)
375
376modified__day
377number
378(query)
379
380modified__gt
381string($date-time)
382(query)
383
384modified__gte
385string($date-time)
386(query)
387
388modified__lt
389string($date-time)
390(query)
391
392modified__lte
393string($date-time)
394(query)
395
396modified__month
397number
398(query)
399
400modified__year
401number
402(query)
403
404ordering
405string
406(query)
407Feld, das zum Sortieren der Ergebnisse verwendet werden soll.
408
409original_filename__icontains
410string
411(query)
412
413original_filename__iendswith
414string
415(query)
416
417original_filename__iexact
418string
419(query)
420
421original_filename__istartswith
422string
423(query)
424
425owner__id
426integer
427(query)
428
429owner__id__in
430array<integer>
431(query)
432Mehrere Werte können durch Kommas getrennt sein.
433Add integer item
434owner__id__none
435integer
436(query)
437
438owner__isnull
439boolean
440(query)
441
442page
443integer
444(query)
445Eine Seitenzahl in der paginierten Ergebnismenge.
446
447page_size
448integer
449(query)
450Anzahl der pro Seite zurückzugebenden Ergebnisse.
451
452query
453string
454(query)
455Advanced search query string
456
457search
458string
459(query)
460Ein Suchbegriff.
461
462shared_by__id
463boolean
464(query)
465
466storage_path__id
467integer
468(query)
469
470storage_path__id__in
471array<integer>
472(query)
473Mehrere Werte können durch Kommas getrennt sein.
474Add integer item
475storage_path__id__none
476integer
477(query)
478
479storage_path__isnull
480boolean
481(query)
482
483storage_path__name__icontains
484string
485(query)
486
487storage_path__name__iendswith
488string
489(query)
490
491storage_path__name__iexact
492string
493(query)
494
495storage_path__name__istartswith
496string
497(query)
498
499tags__id
500integer
501(query)
502
503tags__id__all
504integer
505(query)
506
507tags__id__in
508integer
509(query)
510
511tags__id__none
512integer
513(query)
514
515tags__name__icontains
516string
517(query)
518
519tags__name__iendswith
520string
521(query)
522
523tags__name__iexact
524string
525(query)
526
527tags__name__istartswith
528string
529(query)
530
531title__icontains
532string
533(query)
534
535title__iendswith
536string
537(query)
538
539title__iexact
540string
541(query)
542
543title__istartswith
544string
545(query)
546
547title_content
548string
549(query)
550 */