forms.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. tag = forms.CharField(label="Tags (comma separated tag1,tag2,tag3)", strip=True, required=False)
  17. depth = forms.ChoiceField(label="Archive depth", choices=CHOICES, initial='0', widget=forms.RadioSelect(attrs={"class": "depth-selection"}))
  18. archive_methods = forms.MultipleChoiceField(
  19. label="Archive methods (select at least 1, otherwise all will be used by default)",
  20. required=False,
  21. widget=forms.SelectMultiple,
  22. choices=ARCHIVE_METHODS,
  23. )
  24. # TODO: hook these up to the view and put them
  25. # in a collapsible UI section labeled "Advanced"
  26. #
  27. # exclude_patterns = forms.CharField(
  28. # label="Exclude patterns",
  29. # min_length='1',
  30. # required=False,
  31. # initial=URL_BLACKLIST,
  32. # )
  33. # timeout = forms.IntegerField(
  34. # initial=TIMEOUT,
  35. # )
  36. # overwrite = forms.BooleanField(
  37. # label="Overwrite any existing Snapshots",
  38. # initial=False,
  39. # )
  40. # index_only = forms.BooleanField(
  41. # label="Add URLs to index without Snapshotting",
  42. # initial=False,
  43. # )
  44. class TagWidgetMixin:
  45. def format_value(self, value):
  46. if value is not None and not isinstance(value, str):
  47. value = edit_string_for_tags(value)
  48. return super().format_value(value)
  49. class TagWidget(TagWidgetMixin, forms.TextInput):
  50. pass
  51. class TagField(forms.CharField):
  52. widget = TagWidget
  53. def clean(self, value):
  54. value = super().clean(value)
  55. try:
  56. return parse_tags(value)
  57. except ValueError:
  58. raise forms.ValidationError(
  59. "Please provide a comma-separated list of tags."
  60. )
  61. def has_changed(self, initial_value, data_value):
  62. # Always return False if the field is disabled since self.bound_data
  63. # always uses the initial value in this case.
  64. if self.disabled:
  65. return False
  66. try:
  67. data_value = self.clean(data_value)
  68. except forms.ValidationError:
  69. pass
  70. if initial_value is None:
  71. initial_value = []
  72. initial_value = [tag.name for tag in initial_value]
  73. initial_value.sort()
  74. return initial_value != data_value