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.storagePath = const StoragePathQuery.unset(),
this.asn = const AsnQuery.unset(),
this.tags = const IdsTagsQuery.unset(),
this.tags = const IdsTagsQuery(),
this.sortField = SortField.created,
this.sortOrder = SortOrder.descending,
this.page = 1,

View File

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

View File

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

View File

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

View File

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