Fixed wrong parsing logic for document filters/saved views

This commit is contained in:
Anton Stubenbord
2022-11-20 00:05:01 +01:00
parent cf49feb6eb
commit ebc158e3c6
5 changed files with 51 additions and 30 deletions

View File

@@ -43,7 +43,7 @@ class DocumentFilter with EquatableMixin {
this.correspondent = const CorrespondentQuery.unset(), this.correspondent = const CorrespondentQuery.unset(),
this.storagePath = const StoragePathQuery.unset(), this.storagePath = const StoragePathQuery.unset(),
this.asn = const AsnQuery.unset(), this.asn = const AsnQuery.unset(),
this.tags = const IdsTagsQuery.unset(), this.tags = const IdsTagsQuery(),
this.sortField = SortField.created, this.sortField = SortField.created,
this.sortOrder = SortOrder.descending, this.sortOrder = SortOrder.descending,
this.page = 1, this.page = 1,

View File

@@ -14,7 +14,7 @@ class FilterRule with EquatableMixin {
static const int correspondentRule = 3; static const int correspondentRule = 3;
static const int documentTypeRule = 4; static const int documentTypeRule = 4;
static const int includeTagsRule = 6; static const int includeTagsRule = 6;
static const int hasAnyTag = 7; // Corresponds to Not assigned static const int hasAnyTag = 7; // true = any tag, false = not assigned
static const int createdBeforeRule = 8; static const int createdBeforeRule = 8;
static const int createdAfterRule = 9; static const int createdAfterRule = 9;
static const int addedBeforeRule = 13; static const int addedBeforeRule = 13;
@@ -85,32 +85,36 @@ class FilterRule with EquatableMixin {
case includeTagsRule: case includeTagsRule:
assert(filter.tags is IdsTagsQuery); assert(filter.tags is IdsTagsQuery);
return filter.copyWith( return filter.copyWith(
tags: (filter.tags as IdsTagsQuery).withIdQueriesAdded([ tags: (filter.tags as IdsTagsQuery)
IncludeTagIdQuery(int.parse(value!)), .withIdQueriesAdded([IncludeTagIdQuery(int.parse(value!))]),
]),
); );
case excludeTagsRule: case excludeTagsRule:
assert(filter.tags is IdsTagsQuery); assert(filter.tags is IdsTagsQuery);
return filter.copyWith( return filter.copyWith(
tags: (filter.tags as IdsTagsQuery).withIdQueriesAdded([ tags: (filter.tags as IdsTagsQuery)
ExcludeTagIdQuery(int.parse(value!)), .withIdQueriesAdded([ExcludeTagIdQuery(int.parse(value!))]),
]),
); );
case createdBeforeRule: case createdBeforeRule:
return filter.copyWith( return filter.copyWith(
createdDateBefore: value == null ? null : DateTime.parse(value!)); createdDateBefore: value == null ? null : DateTime.parse(value!),
);
case createdAfterRule: case createdAfterRule:
return filter.copyWith( return filter.copyWith(
createdDateAfter: value == null ? null : DateTime.parse(value!)); createdDateAfter: value == null ? null : DateTime.parse(value!),
);
case addedBeforeRule: case addedBeforeRule:
return filter.copyWith( return filter.copyWith(
addedDateBefore: value == null ? null : DateTime.parse(value!)); addedDateBefore: value == null ? null : DateTime.parse(value!),
);
case addedAfterRule: case addedAfterRule:
return filter.copyWith( return filter.copyWith(
addedDateAfter: value == null ? null : DateTime.parse(value!)); addedDateAfter: value == null ? null : DateTime.parse(value!),
);
case titleAndContentRule: case titleAndContentRule:
return filter.copyWith( return filter.copyWith(
queryText: value, queryType: QueryType.titleAndContent); queryText: value,
queryType: QueryType.titleAndContent,
);
case extendedRule: case extendedRule:
return filter.copyWith(queryText: value, queryType: QueryType.extended); return filter.copyWith(queryText: value, queryType: QueryType.extended);
//TODO: Add currently unused rules //TODO: Add currently unused rules
@@ -146,10 +150,10 @@ class FilterRule with EquatableMixin {
.add(FilterRule(storagePathRule, filter.storagePath.id!.toString())); .add(FilterRule(storagePathRule, filter.storagePath.id!.toString()));
} }
if (filter.tags is OnlyNotAssignedTagsQuery) { if (filter.tags is OnlyNotAssignedTagsQuery) {
filterRules.add(FilterRule(hasAnyTag, "false")); filterRules.add(FilterRule(hasAnyTag, false.toString()));
} }
if (filter.tags is AnyAssignedTagsQuery) { if (filter.tags is AnyAssignedTagsQuery) {
filterRules.add(FilterRule(hasAnyTag, "true")); filterRules.add(FilterRule(hasAnyTag, true.toString()));
} }
if (filter.tags is IdsTagsQuery) { if (filter.tags is IdsTagsQuery) {
filterRules.addAll((filter.tags as IdsTagsQuery) filterRules.addAll((filter.tags as IdsTagsQuery)

View File

@@ -1,5 +1,4 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:paperless_mobile/extensions/dart_extensions.dart';
abstract class TagsQuery with EquatableMixin { abstract class TagsQuery with EquatableMixin {
const TagsQuery(); const TagsQuery();
@@ -33,8 +32,6 @@ class IdsTagsQuery extends TagsQuery {
const IdsTagsQuery([this._idQueries = const []]); const IdsTagsQuery([this._idQueries = const []]);
const IdsTagsQuery.unset() : _idQueries = const [];
IdsTagsQuery.included(Iterable<int> ids) IdsTagsQuery.included(Iterable<int> ids)
: _idQueries = ids.map((id) => IncludeTagIdQuery(id)); : _idQueries = ids.map((id) => IncludeTagIdQuery(id));
@@ -44,13 +41,14 @@ class IdsTagsQuery extends TagsQuery {
: _idQueries = ids.map((id) => ExcludeTagIdQuery(id)); : _idQueries = ids.map((id) => ExcludeTagIdQuery(id));
IdsTagsQuery withIdQueriesAdded(Iterable<TagIdQuery> idQueries) { IdsTagsQuery withIdQueriesAdded(Iterable<TagIdQuery> idQueries) {
final intersection = _idQueries final intersection = idQueries
.map((idQ) => idQ.id) .map((idQ) => idQ.id)
.toSet() .toSet()
.intersection(_idQueries.map((idQ) => idQ.id).toSet()); .intersection(_idQueries.map((idQ) => idQ.id).toSet());
return IdsTagsQuery( final res = IdsTagsQuery(
[...withIdsRemoved(intersection).queries, ...idQueries], [...withIdsRemoved(intersection).queries, ...idQueries],
); );
return res;
} }
IdsTagsQuery withIdsRemoved(Iterable<int> ids) { IdsTagsQuery withIdsRemoved(Iterable<int> ids) {

View File

@@ -52,9 +52,9 @@ class SavedView with EquatableMixin {
.where((order) => order.queryString == json['sort_field']) .where((order) => order.queryString == json['sort_field'])
.first, .first,
sortReverse: json['sort_reverse'], sortReverse: json['sort_reverse'],
filterRules: json['filter_rules'] filterRules: (json['filter_rules'] as List)
.cast<JSON>() .cast<JSON>()
.map<FilterRule>(FilterRule.fromJson) .map(FilterRule.fromJson)
.toList(), .toList(),
); );

View File

@@ -39,9 +39,13 @@ void main() {
'value': "2", 'value': "2",
}, },
{ {
'rule_type': FilterRule.includeTagsRule, 'rule_type': FilterRule.excludeTagsRule,
'value': "3", 'value': "3",
}, },
{
'rule_type': FilterRule.excludeTagsRule,
'value': "4",
},
{ {
'rule_type': FilterRule.extendedRule, 'rule_type': FilterRule.extendedRule,
'value': "Never gonna give you up", 'value': "Never gonna give you up",
@@ -73,7 +77,14 @@ void main() {
correspondent: const CorrespondentQuery.fromId(42), correspondent: const CorrespondentQuery.fromId(42),
documentType: const DocumentTypeQuery.fromId(69), documentType: const DocumentTypeQuery.fromId(69),
storagePath: const StoragePathQuery.fromId(14), storagePath: const StoragePathQuery.fromId(14),
tags: IdsTagsQuery.fromIds([1, 2, 3]), tags: IdsTagsQuery(
[
IncludeTagIdQuery(1),
IncludeTagIdQuery(2),
ExcludeTagIdQuery(3),
ExcludeTagIdQuery(4),
],
),
createdDateBefore: DateTime.parse("2022-10-27"), createdDateBefore: DateTime.parse("2022-10-27"),
createdDateAfter: DateTime.parse("2022-09-27"), createdDateAfter: DateTime.parse("2022-09-27"),
addedDateBefore: DateTime.parse("2022-09-26"), addedDateBefore: DateTime.parse("2022-09-26"),
@@ -121,8 +132,8 @@ void main() {
'value': null, 'value': null,
}, },
{ {
'rule_type': FilterRule.includeTagsRule, 'rule_type': FilterRule.hasAnyTag,
'value': null, 'value': false.toString(),
}, },
{ {
'rule_type': FilterRule.storagePathRule, 'rule_type': FilterRule.storagePathRule,
@@ -148,7 +159,13 @@ void main() {
correspondent: const CorrespondentQuery.fromId(1), correspondent: const CorrespondentQuery.fromId(1),
documentType: const DocumentTypeQuery.fromId(2), documentType: const DocumentTypeQuery.fromId(2),
storagePath: const StoragePathQuery.fromId(3), storagePath: const StoragePathQuery.fromId(3),
tags: IdsTagsQuery.fromIds([4, 5, 6]), tags: IdsTagsQuery([
IncludeTagIdQuery(4),
IncludeTagIdQuery(5),
ExcludeTagIdQuery(6),
ExcludeTagIdQuery(7),
ExcludeTagIdQuery(8),
]),
sortField: SortField.added, sortField: SortField.added,
sortOrder: SortOrder.ascending, sortOrder: SortOrder.ascending,
addedDateAfter: DateTime.parse("2020-01-01"), addedDateAfter: DateTime.parse("2020-01-01"),
@@ -175,7 +192,9 @@ void main() {
FilterRule(FilterRule.storagePathRule, "3"), FilterRule(FilterRule.storagePathRule, "3"),
FilterRule(FilterRule.includeTagsRule, "4"), FilterRule(FilterRule.includeTagsRule, "4"),
FilterRule(FilterRule.includeTagsRule, "5"), FilterRule(FilterRule.includeTagsRule, "5"),
FilterRule(FilterRule.includeTagsRule, "6"), FilterRule(FilterRule.excludeTagsRule, "6"),
FilterRule(FilterRule.excludeTagsRule, "7"),
FilterRule(FilterRule.excludeTagsRule, "8"),
FilterRule(FilterRule.addedAfterRule, "2020-01-01"), FilterRule(FilterRule.addedAfterRule, "2020-01-01"),
FilterRule(FilterRule.addedBeforeRule, "2020-03-01"), FilterRule(FilterRule.addedBeforeRule, "2020-03-01"),
FilterRule(FilterRule.createdAfterRule, "2020-02-01"), FilterRule(FilterRule.createdAfterRule, "2020-02-01"),
@@ -194,7 +213,7 @@ void main() {
correspondent: CorrespondentQuery.unset(), correspondent: CorrespondentQuery.unset(),
documentType: DocumentTypeQuery.unset(), documentType: DocumentTypeQuery.unset(),
storagePath: StoragePathQuery.unset(), storagePath: StoragePathQuery.unset(),
tags: IdsTagsQuery.unset(), tags: IdsTagsQuery(),
sortField: SortField.created, sortField: SortField.created,
sortOrder: SortOrder.descending, sortOrder: SortOrder.descending,
addedDateAfter: null, addedDateAfter: null,
@@ -246,7 +265,7 @@ void main() {
FilterRule(FilterRule.correspondentRule, null), FilterRule(FilterRule.correspondentRule, null),
FilterRule(FilterRule.documentTypeRule, null), FilterRule(FilterRule.documentTypeRule, null),
FilterRule(FilterRule.storagePathRule, null), FilterRule(FilterRule.storagePathRule, null),
FilterRule(FilterRule.includeTagsRule, null), FilterRule(FilterRule.hasAnyTag, false.toString()),
], ],
), ),
), ),