|
|
@@ -9,7 +9,8 @@ from django.http import HttpResponse
|
|
|
from django.db.models import Q
|
|
|
from django.views import View, static
|
|
|
from django.views.generic.list import ListView
|
|
|
-from django.views import View
|
|
|
+from django.views.generic import FormView
|
|
|
+from django.contrib.auth.mixins import UserPassesTestMixin
|
|
|
|
|
|
from core.models import Snapshot
|
|
|
from core.utils import get_icons
|
|
|
@@ -115,38 +116,37 @@ class PublicArchiveView(ListView):
|
|
|
return redirect(f'/admin/login/?next={self.request.path}')
|
|
|
|
|
|
|
|
|
-class AddView(View):
|
|
|
- extra_context = {'title': 'Add URLs'}
|
|
|
-
|
|
|
- def get(self, request, *args, **kwargs):
|
|
|
- if PUBLIC_ADD_VIEW or self.request.user.is_authenticated:
|
|
|
- self.extra_context['form'] = AddLinkForm()
|
|
|
- return render(template_name='add_links.html', request=request, context=self.extra_context)
|
|
|
- else:
|
|
|
- return redirect(f'/admin/login/?next={request.path}')
|
|
|
-
|
|
|
- def post(self, request, *args, **kwargs):
|
|
|
- form = AddLinkForm(request.POST)
|
|
|
- if form.is_valid():
|
|
|
- url = form.cleaned_data["url"]
|
|
|
- print(f'[+] Adding URL: {url}')
|
|
|
- depth = 0 if form.cleaned_data["depth"] == "0" else 1
|
|
|
- input_kwargs = {
|
|
|
- "urls": url,
|
|
|
- "depth": depth,
|
|
|
- "update_all": False,
|
|
|
- "out_dir": OUTPUT_DIR,
|
|
|
- }
|
|
|
- add_stdout = StringIO()
|
|
|
- with redirect_stdout(add_stdout):
|
|
|
- add(**input_kwargs)
|
|
|
- print(add_stdout.getvalue())
|
|
|
-
|
|
|
- self.extra_context.update({
|
|
|
- "stdout": ansi_to_html(add_stdout.getvalue().strip()),
|
|
|
- "form": AddLinkForm()
|
|
|
- })
|
|
|
- else:
|
|
|
- self.extra_context["form"] = form
|
|
|
-
|
|
|
- return render(template_name='add_links.html', request=request, context=self.extra_context)
|
|
|
+class AddView(UserPassesTestMixin, FormView):
|
|
|
+ template_name = "add_links.html"
|
|
|
+ form_class = AddLinkForm
|
|
|
+
|
|
|
+ def test_func(self):
|
|
|
+ return PUBLIC_ADD_VIEW or self.request.user.is_authenticated
|
|
|
+
|
|
|
+ def get_context_data(self, *args, **kwargs):
|
|
|
+ context = super().get_context_data(*args, **kwargs)
|
|
|
+ context["title"]: "Add URLs"
|
|
|
+ return context
|
|
|
+
|
|
|
+ def form_valid(self, form):
|
|
|
+ url = form.cleaned_data["url"]
|
|
|
+ print(f'[+] Adding URL: {url}')
|
|
|
+ depth = 0 if form.cleaned_data["depth"] == "0" else 1
|
|
|
+ input_kwargs = {
|
|
|
+ "urls": url,
|
|
|
+ "depth": depth,
|
|
|
+ "update_all": False,
|
|
|
+ "out_dir": OUTPUT_DIR,
|
|
|
+ }
|
|
|
+ add_stdout = StringIO()
|
|
|
+ with redirect_stdout(add_stdout):
|
|
|
+ add(**input_kwargs)
|
|
|
+ print(add_stdout.getvalue())
|
|
|
+
|
|
|
+ context = self.get_context_data()
|
|
|
+
|
|
|
+ context.update({
|
|
|
+ "stdout": ansi_to_html(add_stdout.getvalue().strip()),
|
|
|
+ "form": AddLinkForm()
|
|
|
+ })
|
|
|
+ return render(template_name=self.template_name, request=self.request, context=context)
|