forms.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. __package__ = 'archivebox.core'
  2. from django import forms
  3. from ..util import URL_REGEX
  4. from .utils_taggit 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. ARCHIVE_METHODS = [
  10. ('title', 'title'),
  11. ('favicon', 'favicon'),
  12. ('wget', 'wget'),
  13. ('warc', 'warc'),
  14. ('pdf', 'pdf'),
  15. ('screenshot', 'screenshot'),
  16. ('dom', 'dom'),
  17. ('singlefile', 'singlefile'),
  18. ('git', 'git'),
  19. ('media', 'media'),
  20. ('archive_org', 'archive_org'),
  21. ]
  22. class AddLinkForm(forms.Form):
  23. url = forms.RegexField(label="URLs (one per line)", regex=URL_REGEX, min_length='6', strip=True, widget=forms.Textarea, required=True)
  24. depth = forms.ChoiceField(label="Archive depth", choices=CHOICES, widget=forms.RadioSelect, initial='0')
  25. archiveMethods = forms.MultipleChoiceField(
  26. required=False,
  27. widget=forms.SelectMultiple,
  28. choices=ARCHIVE_METHODS,)
  29. class TagWidgetMixin:
  30. def format_value(self, value):
  31. if value is not None and not isinstance(value, str):
  32. value = edit_string_for_tags(value)
  33. return super().format_value(value)
  34. class TagWidget(TagWidgetMixin, forms.TextInput):
  35. pass
  36. class TagField(forms.CharField):
  37. widget = TagWidget
  38. def clean(self, value):
  39. value = super().clean(value)
  40. try:
  41. return parse_tags(value)
  42. except ValueError:
  43. raise forms.ValidationError(
  44. "Please provide a comma-separated list of tags."
  45. )
  46. def has_changed(self, initial_value, data_value):
  47. # Always return False if the field is disabled since self.bound_data
  48. # always uses the initial value in this case.
  49. if self.disabled:
  50. return False
  51. try:
  52. data_value = self.clean(data_value)
  53. except forms.ValidationError:
  54. pass
  55. if initial_value is None:
  56. initial_value = []
  57. initial_value = [tag.name for tag in initial_value]
  58. initial_value.sort()
  59. return initial_value != data_value