1
0
Fork 0
mirror of synced 2024-06-28 02:50:24 +12:00

refactor: Change View to FormView

This commit is contained in:
Cristian 2020-08-28 09:58:32 -05:00
parent a06bd715a9
commit bc116c25f8
3 changed files with 96 additions and 73 deletions

View file

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

View file

@ -28,7 +28,7 @@
<a href="/add" id="submit">&nbsp; Add more URLs </a> <a href="/add" id="submit">&nbsp; Add more URLs </a>
</center> </center>
{% else %} {% else %}
<form id="add-form" action="?" method="POST" class="p-form">{% csrf_token %} <form id="add-form" method="POST" class="p-form">{% csrf_token %}
<h1>Add new URLs to your archive</h1> <h1>Add new URLs to your archive</h1>
<br/> <br/>
{{ form.as_p }} {{ form.as_p }}

View file

@ -1,39 +1,62 @@
.dashboard #content { .dashboard #content {
width: 100%; width: 100%;
margin-right: 0px; margin-right: 0px;
margin-left: 0px; margin-left: 0px;
} }
#submit { #submit {
border: 1px solid rgba(0,0,0,0.2); border: 1px solid rgba(0, 0, 0, 0.2);
padding: 10px; padding: 10px;
border-radius: 4px; border-radius: 4px;
background-color: #f5dd5d; background-color: #f5dd5d;
color: #333; color: #333;
font-size: 18px; font-size: 18px;
font-weight: 800; font-weight: 800;
} }
#add-form button[role=submit]:hover { #add-form button[role="submit"]:hover {
background-color: #e5cd4d; background-color: #e5cd4d;
} }
#add-form label { #add-form label {
display: block; display: block;
font-size: 16px; font-size: 16px;
} }
#add-form textarea { #add-form textarea {
width: 100%; width: 100%;
min-height: 300px; min-height: 300px;
} }
#delay-warning div { #delay-warning div {
border: 1px solid red; border: 1px solid red;
border-radius: 4px; border-radius: 4px;
margin: 10px; margin: 10px;
padding: 10px; padding: 10px;
font-size: 15px; font-size: 15px;
background-color: #F5DD5D; background-color: #f5dd5d;
} }
#stdout { #stdout {
background-color: #ded; background-color: #ded;
padding: 10px 10px; padding: 10px 10px;
border-radius: 4px; border-radius: 4px;
white-space: normal; white-space: normal;
} }
ul#id_depth {
list-style-type: none;
padding: 0;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */
border-radius: 50%;
width: 30px;
height: 30px;
box-sizing: border-box;
animation: spin 2s linear infinite;
}