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 django import forms
from .models import Organization from .models import Organization, Network, Device
class NetworkForm(forms.Form): class NetworkForm(forms.ModelForm):
name = forms.CharField(label="Common Name", max_length=64) class Meta:
intip = forms.GenericIPAddressField(label="Internal IP Address", protocol="ipv4") model = Network
extip = forms.GenericIPAddressField(label="External IP Address", protocol="ipv4") fields = ["name", "intip", "extip", "organization"]
orgas = forms.MultipleChoiceField(label="Assigned Organizations", choices=[(orga.id, str(orga)) for orga in Organization.objects.all()])
def __init__(self, user, *args, **kwargs): class OrgaForm(forms.ModelForm):
super(NetworkForm, self).__init__(*args, **kwargs) class Meta:
self.fields['orgas'].choices = [(orga.id, str(orga)) for orga in user.organization_set.all()] 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('makenet/', views.makenetwork, name='makenet'),
path('makeorga/', views.makeorganization, name='makeorga'), path('makeorga/', views.makeorganization, name='makeorga'),
path('networks/<int:network_id>/delete', views.deletenetwork, name='deletenetwork'), 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>/edit/', views.editwifi, name='editwifi'),
path('wifi/<int:wifi_id>/delete/', views.deletewifi, name='deletewifi'), path('wifi/<int:wifi_id>/delete/', views.deletewifi, name='deletewifi'),
path('users/<int:user_id>/edit/', views.edituser, name='edituser'), path('users/<int:user_id>/edit/', views.edituser, name='edituser'),

View file

@ -418,18 +418,29 @@ def makewifi(request):
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def makenetwork(request): def makenetwork(request):
if request.method == "POST": if request.method == "POST":
form = NetworkForm(request.user, request.POST) form = NetworkForm(request.POST)
if form.is_valid(): if form.is_valid():
data = form.cleaned_data form.save()
network = Network.objects.create(name=data["name"], intip=data["intip"], extip=data["extip"])
network.organization.set(data["orgas"])
return redirect(reverse("networks")) return redirect(reverse("networks"))
else: else:
form = NetworkForm(request.user) form = NetworkForm()
return render(request, "manager/form.html", { "title": "Add Network", "form": form }) 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 @login_required
def setactiveorga(request, orga_id): def setactiveorga(request, orga_id):
request.user.userstatus.orga = get_object_or_404(Organization, id=orga_id, users=request.user) 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": if request.method == "POST":
form = OrgaForm(request.POST) form = OrgaForm(request.POST)
if form.is_valid(): if form.is_valid():
data = form.cleaned_data orga = form.save()
orga = Organization.objects.create(name=data["name"], userlimit=data["users"])
request.user.organization_set.add(orga) request.user.organization_set.add(orga)
Network.objects.get(intip="No VPN").organization.add(orga) Network.objects.get(intip="No VPN").organization.add(orga)
return redirect(reverse("organizations")) return redirect(reverse("organizations"))
@ -456,16 +466,13 @@ def makeorganization(request):
def editorganization(request, orga_id): def editorganization(request, orga_id):
orga = get_object_or_404(Organization, id=orga_id) orga = get_object_or_404(Organization, id=orga_id)
if request.method == "POST": if request.method == "POST":
form = OrgaForm(request.POST) form = OrgaForm(request.POST, instance=orga)
if form.is_valid(): if form.is_valid():
data = form.cleaned_data form.save()
orga.name = data["name"]
orga.userlimit = data["users"]
orga.save()
return redirect(reverse("organizations")) return redirect(reverse("organizations"))
else: 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 }) 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><div style="display:inline;" title="{% netOrgaString network %}">{% if network.name %}{{ network.name }}{% endif %}</div></td>
<td>{{ network.intip }}</td> <td>{{ network.intip }}</td>
<td>{{ network.extip }}</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> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -145,7 +145,7 @@
{% for orga in orgas %} {% for orga in orgas %}
<tr class="orgarow"> <tr class="orgarow">
<td><div style="display:inline;">{{ orga.name }}</div></td> <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> <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> </tr>
{% endfor %} {% endfor %}