forms.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. class AddLinkForm(forms.Form):
  10. url = forms.RegexField(label="URLs (one per line)", regex=URL_REGEX, min_length='6', strip=True, widget=forms.Textarea, required=True)
  11. depth = forms.ChoiceField(label="Archive depth", choices=CHOICES, widget=forms.RadioSelect, initial='0')
  12. class TagWidgetMixin:
  13. def format_value(self, value):
  14. if value is not None and not isinstance(value, str):
  15. value = edit_string_for_tags(value)
  16. return super().format_value(value)
  17. class TagWidget(TagWidgetMixin, forms.TextInput):
  18. pass
  19. class TagField(forms.CharField):
  20. widget = TagWidget
  21. def clean(self, value):
  22. value = super().clean(value)
  23. try:
  24. return parse_tags(value)
  25. except ValueError:
  26. raise forms.ValidationError(
  27. "Please provide a comma-separated list of tags."
  28. )
  29. def has_changed(self, initial_value, data_value):
  30. # Always return False if the field is disabled since self.bound_data
  31. # always uses the initial value in this case.
  32. if self.disabled:
  33. return False
  34. try:
  35. data_value = self.clean(data_value)
  36. except forms.ValidationError:
  37. pass
  38. if initial_value is None:
  39. initial_value = []
  40. initial_value = [tag.name for tag in initial_value]
  41. initial_value.sort()
  42. return initial_value != data_value