2020-07-28 03:26:45 +00:00
|
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
2020-07-02 20:54:25 +00:00
|
|
|
from django import forms
|
|
|
|
|
2020-07-28 03:26:45 +00:00
|
|
|
from ..util import URL_REGEX
|
2021-04-01 06:34:16 +00:00
|
|
|
from ..parsers import PARSERS
|
2020-12-06 00:01:18 +00:00
|
|
|
from ..vendor.taggit_utils import edit_string_for_tags, parse_tags
|
2020-07-28 03:26:45 +00:00
|
|
|
|
2021-04-01 06:34:16 +00:00
|
|
|
PARSER_CHOICES = [
|
|
|
|
(parser_key, parser[0])
|
|
|
|
for parser_key, parser in PARSERS.items()
|
|
|
|
]
|
|
|
|
DEPTH_CHOICES = (
|
2020-07-28 03:26:45 +00:00
|
|
|
('0', 'depth = 0 (archive just these URLs)'),
|
|
|
|
('1', 'depth = 1 (archive these URLs and all URLs one hop away)'),
|
2020-07-13 16:21:37 +00:00
|
|
|
)
|
2020-07-02 20:54:25 +00:00
|
|
|
|
2020-12-10 17:46:16 +00:00
|
|
|
from ..extractors import get_default_archive_methods
|
|
|
|
|
2020-12-10 15:51:57 +00:00
|
|
|
ARCHIVE_METHODS = [
|
2020-12-10 17:46:16 +00:00
|
|
|
(name, name)
|
|
|
|
for name, _, _ in get_default_archive_methods()
|
2020-12-10 15:51:57 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-07-02 20:54:25 +00:00
|
|
|
class AddLinkForm(forms.Form):
|
2020-07-28 03:26:45 +00:00
|
|
|
url = forms.RegexField(label="URLs (one per line)", regex=URL_REGEX, min_length='6', strip=True, widget=forms.Textarea, required=True)
|
2021-04-01 06:34:16 +00:00
|
|
|
parser = forms.ChoiceField(label="URLs format", choices=[('auto', 'Auto-detect parser'), *PARSER_CHOICES], initial='auto')
|
2021-03-27 08:30:15 +00:00
|
|
|
tag = forms.CharField(label="Tags (comma separated tag1,tag2,tag3)", strip=True, required=False)
|
2021-04-01 06:34:16 +00:00
|
|
|
depth = forms.ChoiceField(label="Archive depth", choices=DEPTH_CHOICES, initial='0', widget=forms.RadioSelect(attrs={"class": "depth-selection"}))
|
2020-12-10 17:44:38 +00:00
|
|
|
archive_methods = forms.MultipleChoiceField(
|
2021-01-29 04:15:05 +00:00
|
|
|
label="Archive methods (select at least 1, otherwise all will be used by default)",
|
2020-12-10 17:44:38 +00:00
|
|
|
required=False,
|
|
|
|
widget=forms.SelectMultiple,
|
|
|
|
choices=ARCHIVE_METHODS,
|
|
|
|
)
|
2021-01-29 04:15:05 +00:00
|
|
|
# TODO: hook these up to the view and put them
|
|
|
|
# in a collapsible UI section labeled "Advanced"
|
|
|
|
#
|
|
|
|
# exclude_patterns = forms.CharField(
|
|
|
|
# label="Exclude patterns",
|
|
|
|
# min_length='1',
|
|
|
|
# required=False,
|
|
|
|
# initial=URL_BLACKLIST,
|
|
|
|
# )
|
|
|
|
# timeout = forms.IntegerField(
|
|
|
|
# initial=TIMEOUT,
|
|
|
|
# )
|
|
|
|
# overwrite = forms.BooleanField(
|
|
|
|
# label="Overwrite any existing Snapshots",
|
|
|
|
# initial=False,
|
|
|
|
# )
|
|
|
|
# index_only = forms.BooleanField(
|
|
|
|
# label="Add URLs to index without Snapshotting",
|
|
|
|
# initial=False,
|
|
|
|
# )
|
|
|
|
|
2020-10-12 18:47:03 +00:00
|
|
|
class TagWidgetMixin:
|
|
|
|
def format_value(self, value):
|
|
|
|
if value is not None and not isinstance(value, str):
|
|
|
|
value = edit_string_for_tags(value)
|
|
|
|
return super().format_value(value)
|
|
|
|
|
|
|
|
class TagWidget(TagWidgetMixin, forms.TextInput):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TagField(forms.CharField):
|
|
|
|
widget = TagWidget
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super().clean(value)
|
|
|
|
try:
|
|
|
|
return parse_tags(value)
|
|
|
|
except ValueError:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
"Please provide a comma-separated list of tags."
|
|
|
|
)
|
|
|
|
|
|
|
|
def has_changed(self, initial_value, data_value):
|
|
|
|
# Always return False if the field is disabled since self.bound_data
|
|
|
|
# always uses the initial value in this case.
|
|
|
|
if self.disabled:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
data_value = self.clean(data_value)
|
|
|
|
except forms.ValidationError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if initial_value is None:
|
|
|
|
initial_value = []
|
|
|
|
|
|
|
|
initial_value = [tag.name for tag in initial_value]
|
|
|
|
initial_value.sort()
|
|
|
|
|
|
|
|
return initial_value != data_value
|