Simplify network and orga forms

This commit is contained in:
Kumi 2019-03-16 13:25:37 +00:00
parent 4846d1da31
commit 35b43df7b4
4 changed files with 32 additions and 27 deletions

View file

@ -1,17 +1,14 @@
from django import forms
from .models import Organization
from .models import Organization, Network, Device
class NetworkForm(forms.Form):
name = forms.CharField(label="Common Name", max_length=64)
intip = forms.GenericIPAddressField(label="Internal IP Address", protocol="ipv4")
extip = forms.GenericIPAddressField(label="External IP Address", protocol="ipv4")
orgas = forms.MultipleChoiceField(label="Assigned Organizations", choices=[(orga.id, str(orga)) for orga in Organization.objects.all()])
class NetworkForm(forms.ModelForm):
class Meta:
model = Network
fields = ["name", "intip", "extip", "organization"]
def __init__(self, user, *args, **kwargs):
super(NetworkForm, self).__init__(*args, **kwargs)
self.fields['orgas'].choices = [(orga.id, str(orga)) for orga in user.organization_set.all()]
class OrgaForm(forms.ModelForm):
class Meta:
model = Organization
fields = ["name", "userlimit"]
class OrgaForm(forms.Form):
name = forms.CharField(label="Common Name", max_length=64)
users = forms.IntegerField(label="User Limit", required=False)

View file

@ -23,6 +23,7 @@ urlpatterns = [
path('makenet/', views.makenetwork, name='makenet'),
path('makeorga/', views.makeorganization, name='makeorga'),
path('networks/<int:network_id>/delete', views.deletenetwork, name='deletenetwork'),
path('networks/<int:network_id>/edit', views.editnetwork, name='editnetwork'),
path('wifi/<int:wifi_id>/edit/', views.editwifi, name='editwifi'),
path('wifi/<int:wifi_id>/delete/', views.deletewifi, name='deletewifi'),
path('users/<int:user_id>/edit/', views.edituser, name='edituser'),

View file

@ -418,18 +418,29 @@ def makewifi(request):
@user_passes_test(is_superuser)
def makenetwork(request):
if request.method == "POST":
form = NetworkForm(request.user, request.POST)
form = NetworkForm(request.POST)
if form.is_valid():
data = form.cleaned_data
network = Network.objects.create(name=data["name"], intip=data["intip"], extip=data["extip"])
network.organization.set(data["orgas"])
form.save()
return redirect(reverse("networks"))
else:
form = NetworkForm(request.user)
form = NetworkForm()
return render(request, "manager/form.html", { "title": "Add Network", "form": form })
@user_passes_test(is_superuser)
def editnetwork(request, network_id):
net = get_object_or_404(Network, id=network_id)
form = NetworkForm(request.POST if request.method == "POST" else None, instance=net)
form.fields["intip"].disabled = True
form.fields["extip"].disabled = True
if request.method == "POST" and form.is_valid():
form.save()
return redirect(reverse("networks"))
return render(request, "manager/form.html", { "title": "Edit Network", "form": form })
@login_required
def setactiveorga(request, orga_id):
request.user.userstatus.orga = get_object_or_404(Organization, id=orga_id, users=request.user)
@ -441,8 +452,7 @@ def makeorganization(request):
if request.method == "POST":
form = OrgaForm(request.POST)
if form.is_valid():
data = form.cleaned_data
orga = Organization.objects.create(name=data["name"], userlimit=data["users"])
orga = form.save()
request.user.organization_set.add(orga)
Network.objects.get(intip="No VPN").organization.add(orga)
return redirect(reverse("organizations"))
@ -456,16 +466,13 @@ def makeorganization(request):
def editorganization(request, orga_id):
orga = get_object_or_404(Organization, id=orga_id)
if request.method == "POST":
form = OrgaForm(request.POST)
form = OrgaForm(request.POST, instance=orga)
if form.is_valid():
data = form.cleaned_data
orga.name = data["name"]
orga.userlimit = data["users"]
orga.save()
form.save()
return redirect(reverse("organizations"))
else:
form = OrgaForm(initial={ "name": orga.name, "users": orga.userlimit })
form = OrgaForm(instance=orga)
return render(request, "manager/form.html", { "title": "Change Organization", "form": form })

View file

@ -122,7 +122,7 @@
<td><div style="display:inline;" title="{% netOrgaString network %}">{% if network.name %}{{ network.name }}{% endif %}</div></td>
<td>{{ network.intip }}</td>
<td>{{ network.extip }}</td>
<td>{% if not network.intip == "No VPN" %}<a href="#"><i style="color: darkred;" onclick="askdeletenet({{ network.id }});" class="fas fa-trash-alt" title="Delete Network"></i></a>{% endif %}</td>
<td>{% if not network.intip == "No VPN" %}<a href="/networks/{{ network.id }}/edit"><i class="fas fa-edit" title="Edit Network"></i></a> <a href="#"><i style="color: darkred;" onclick="askdeletenet({{ network.id }});" class="fas fa-trash-alt" title="Delete Network"></i></a>{% endif %}</td>
</tr>
{% endfor %}
</tbody>
@ -145,7 +145,7 @@
{% for orga in orgas %}
<tr class="orgarow">
<td><div style="display:inline;">{{ orga.name }}</div></td>
<td>{{ orga.users.all|length }}{% if orga.userlimit %}/{{ orga.userlimit }}{% endif %}</td>
<td>{{ orga.orgausers.all|length }}{% if orga.userlimit %}/{{ orga.userlimit }}{% endif %}</td>
<td><a href="/organizations/{{ orga.id }}/edit"><i class="fas fa-edit" title="Edit Organization"></i></a> <a href="#"><i style="color: darkred;" onclick="askdeleteorga({{ orga.id }});" class="fas fa-trash-alt" title="Delete Organization"></i></a></td>
</tr>
{% endfor %}