forms.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. __package__ = 'archivebox.core'
  2. from django import forms
  3. from ..util import URL_REGEX
  4. from ..vendor.taggit_utils import edit_string_for_tags, parse_tags
  5. CHOICES = (
  6. ('0', 'depth = 0 (archive just these URLs)'),
  7. ('1', 'depth = 1 (archive these URLs and all URLs one hop away)'),
  8. )
  9. from ..extractors import get_default_archive_methods
  10. ARCHIVE_METHODS = [
  11. (name, name)
  12. for name, _, _ in get_default_archive_methods()
  13. ]
  14. class AddLinkForm(forms.Form):
  15. url = forms.RegexField(label="URLs (one per line)", regex=URL_REGEX, min_length='6', strip=True, widget=forms.Textarea, required=True)
  16. depth = forms.ChoiceField(label="Archive depth", choices=CHOICES, initial='0', widget=forms.RadioSelect(attrs={"class": "depth-selection"}))
  17. archive_methods = forms.MultipleChoiceField(
  18. label="Archive methods (select at least 1, otherwise all will be used by default)",
  19. required=False,
  20. widget=forms.SelectMultiple,
  21. choices=ARCHIVE_METHODS,
  22. )
  23. # TODO: hook these up to the view and put them
  24. # in a collapsible UI section labeled "Advanced"
  25. #
  26. # exclude_patterns = forms.CharField(
  27. # label="Exclude patterns",
  28. # min_length='1',
  29. # required=False,
  30. # initial=URL_BLACKLIST,
  31. # )
  32. # timeout = forms.IntegerField(
  33. # initial=TIMEOUT,
  34. # )
  35. # overwrite = forms.BooleanField(
  36. # label="Overwrite any existing Snapshots",
  37. # initial=False,
  38. # )
  39. # index_only = forms.BooleanField(
  40. # label="Add URLs to index without Snapshotting",
  41. # initial=False,
  42. # )
  43. class TagWidgetMixin:
  44. def format_value(self, value):
  45. if value is not None and not isinstance(value, str):
  46. value = edit_string_for_tags(value)
  47. return super().format_value(value)
  48. class TagWidget(TagWidgetMixin, forms.TextInput):
  49. pass
  50. class TagField(forms.CharField):
  51. widget = TagWidget
  52. def clean(self, value):
  53. value = super().clean(value)
  54. try:
  55. return parse_tags(value)
  56. except ValueError:
  57. raise forms.ValidationError(
  58. "Please provide a comma-separated list of tags."
  59. )
  60. def has_changed(self, initial_value, data_value):
  61. # Always return False if the field is disabled since self.bound_data
  62. # always uses the initial value in this case.
  63. if self.disabled:
  64. return False
  65. try:
  66. data_value = self.clean(data_value)
  67. except forms.ValidationError:
  68. pass
  69. if initial_value is None:
  70. initial_value = []
  71. initial_value = [tag.name for tag in initial_value]
  72. initial_value.sort()
  73. return initial_value != data_value