|
| 1 | +import yaml |
| 2 | +from wtforms import Form |
| 3 | +from wtforms import RadioField |
| 4 | +from wtforms import SearchField |
| 5 | +from wtforms import StringField |
| 6 | + |
| 7 | +from reader._types import tag_filter_argument |
| 8 | + |
| 9 | + |
| 10 | +class TagFilterField(StringField): |
| 11 | + |
| 12 | + def process_formdata(self, valuelist): |
| 13 | + if not valuelist: |
| 14 | + return |
| 15 | + value = valuelist[0] |
| 16 | + if '[' not in value: |
| 17 | + value = f'[{value}]' |
| 18 | + try: |
| 19 | + data = yaml.safe_load(value) |
| 20 | + except yaml.error.MarkedYAMLError as e: |
| 21 | + raise ValueError(f"invalid YAML: {e.problem or e.context}") from e |
| 22 | + tag_filter_argument(data) |
| 23 | + self.data = data |
| 24 | + |
| 25 | + def _value(self): |
| 26 | + if self.raw_data: |
| 27 | + return self.raw_data[0] |
| 28 | + if not self.data: |
| 29 | + return '' |
| 30 | + return yaml.safe_dump(self.data, default_flow_style=True).rstrip() |
| 31 | + |
| 32 | + |
| 33 | +class ToFormdataMixin: |
| 34 | + def to_formdata(self): |
| 35 | + rv = {} |
| 36 | + |
| 37 | + for field in self: |
| 38 | + try: |
| 39 | + value = field._value() |
| 40 | + except AttributeError: |
| 41 | + values = [option._value() for option in field if option.checked] |
| 42 | + if values: |
| 43 | + value, *rest = values |
| 44 | + if rest: |
| 45 | + raise NotImplementedError( |
| 46 | + "multiple choices not supported" |
| 47 | + ) from None |
| 48 | + else: |
| 49 | + value = field.default |
| 50 | + |
| 51 | + if value and value != field.default: |
| 52 | + rv[field.name] = value |
| 53 | + |
| 54 | + return rv |
| 55 | + |
| 56 | + |
| 57 | +def radio_field(*args, choices, **kwargs): |
| 58 | + """Like RadioField, but choices is a list of (value, value_str), |
| 59 | + (value, value_str, label), or (value, value_str, label, render_kw) tuples. |
| 60 | +
|
| 61 | + """ |
| 62 | + return RadioField( |
| 63 | + *args, |
| 64 | + choices=[c[1] if len(c) == 2 else c[1:] for c in choices], |
| 65 | + coerce={c[1]: c[0] for c in choices}.get, |
| 66 | + **kwargs, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +BOOL_CHOICES = [(True, 'yes'), (False, 'no'), (None, 'all')] |
| 71 | +TRISTATE_CHOICES = [('notfalse', 'maybe')] + BOOL_CHOICES |
| 72 | +ENTRY_SORT_CHOICES = ['recent', 'random'] |
| 73 | + |
| 74 | + |
| 75 | +class EntryFilter(ToFormdataMixin, Form): |
| 76 | + search = SearchField("search", name='q') |
| 77 | + feed_tags = TagFilterField("tags", name='tags') |
| 78 | + read = radio_field("read", choices=BOOL_CHOICES, default='no') |
| 79 | + important = radio_field("important", choices=TRISTATE_CHOICES, default='maybe') |
| 80 | + has_enclosures = radio_field( |
| 81 | + "enclosures", name='enclosures', choices=BOOL_CHOICES, default='all' |
| 82 | + ) |
| 83 | + sort = RadioField("sort", choices=ENTRY_SORT_CHOICES, default='recent') |
| 84 | + |
| 85 | + |
| 86 | +class SearchEntryFilter(EntryFilter): |
| 87 | + sort = RadioField( |
| 88 | + "sort", choices=ENTRY_SORT_CHOICES + ['relevant'], default='relevant' |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +ENTRY_FILTER_PRESETS = { |
| 93 | + 'unread': {}, |
| 94 | + 'important': {'read': 'all', 'important': 'yes'}, |
| 95 | + 'podcast': {'enclosures': 'yes'}, |
| 96 | + 'random': {'sort': 'random'}, |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + from werkzeug.datastructures import MultiDict |
| 102 | + |
| 103 | + args = MultiDict(dict(tags='1')) |
| 104 | + for FormCls in EntryFilter, SearchEntryFilter: |
| 105 | + form = FormCls(args) |
| 106 | + for field in form: |
| 107 | + print(field()) |
| 108 | + print() |
| 109 | + print(form.data) |
| 110 | + print(form.to_formdata()) |
| 111 | + print() |
| 112 | + |
| 113 | + print(form.feed_tags.__dict__) |
| 114 | + import IPython |
| 115 | + |
| 116 | + IPython.embed() |
0 commit comments