Implement reverse lookups, fixing #15, some code beautification/simplification

This commit is contained in:
Kumi 2019-02-15 10:52:56 +00:00
parent e99812426a
commit ca93c2e22b
4 changed files with 68 additions and 63 deletions

View file

@ -14,3 +14,8 @@ def isLatestCommit():
repo = Repo(settings.BASE_DIR) repo = Repo(settings.BASE_DIR)
origin = repo.remotes[0].refs[0] origin = repo.remotes[0].refs[0]
return "" if origin.commit.committed_datetime <= repo.head.commit.committed_datetime else " Update available!" return "" if origin.commit.committed_datetime <= repo.head.commit.committed_datetime else " Update available!"
@register.simple_tag
def isDirty():
repo = Repo(settings.BASE_DIR)
return " Uncommitted changes!" if repo.is_dirty() else ""

View file

@ -12,6 +12,7 @@ from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from django.conf import settings from django.conf import settings
from django.contrib.auth.forms import PasswordResetForm from django.contrib.auth.forms import PasswordResetForm
from django.urls import reverse
from .models import Device, Organization, Network, Model, Wifi, UserStatus from .models import Device, Organization, Network, Model, Wifi, UserStatus
from .signals import * from .signals import *
@ -42,7 +43,7 @@ def is_staff(user):
@login_required @login_required
def index(request): def index(request):
return redirect("/devices/") return redirect(reverse("devices"))
@csrf_exempt @csrf_exempt
def heartbeat(request): def heartbeat(request):
@ -174,7 +175,7 @@ def editdevice(request, device_id):
device.changed = timezone.now() device.changed = timezone.now()
device.save() device.save()
return redirect("/devices/") return redirect(reverse("devices"))
return render(request, "manager/edit.html", return render(request, "manager/edit.html",
{ {
@ -189,23 +190,17 @@ def editdevice(request, device_id):
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def makeuser(request): def makeuser(request):
if request.POST.get("username", ""): if request.POST.get("username", ""):
username = request.POST.get("username", "")
first = request.POST.get("firstname", "")
last = request.POST.get("lastname", "")
staff = request.POST.get("staff", "0") == "True"
suser = request.POST.get("superuser", "0") == "True"
orgas = set(request.POST.getlist("orga", []))
mail = request.POST.get("email", "")
user = User.objects.create_user( user = User.objects.create_user(
username=username, username=request.POST.get("username", ""),
password=str(uuid.uuid4().hex), password=str(uuid.uuid4().hex),
first_name=first, first_name=request.POST.get("firstname", ""),
last_name=last, last_name=request.POST.get("lastname", ""),
is_staff=staff, is_staff=request.POST.get("staff", "0") == "True",
is_superuser=suser, is_superuser=request.POST.get("superuser", "0") == "True",
email=mail email=request.POST.get("email", "")
) )
user.organization_set.set(orgas)
user.organization_set.set(set(request.POST.getlist("orga", [])))
form = PasswordResetForm({"email": user.email}) form = PasswordResetForm({"email": user.email})
@ -213,14 +208,13 @@ def makeuser(request):
form.save( form.save(
request=request, request=request,
use_https=True, use_https=True,
email_template_name='registration/password_reset_email.html') email_template_name='registration/add_user.html')
return redirect("/users/") return redirect(reverse("users"))
else: else:
return render(request, "manager/adduser.html", {"title": "Add User"}) return render(request, "manager/adduser.html", {"title": "Add User"})
@login_required @login_required
def edituser(request, user_id): def edituser(request, user_id):
if request.user.is_staff or request.user.id == user_id: if request.user.is_staff or request.user.id == user_id:
@ -228,7 +222,7 @@ def edituser(request, user_id):
user = request.user if request.user.id == user_id else User.objects.distinct().get(id=user_id, organization__in=orgas) user = request.user if request.user.id == user_id else User.objects.distinct().get(id=user_id, organization__in=orgas)
if not user: if not user:
return redirect("/users/") return redirect(reverse("users"))
if request.POST.get("form", ""): if request.POST.get("form", ""):
newfirst = request.POST.get("firstname", "") newfirst = request.POST.get("firstname", "")
@ -263,7 +257,7 @@ def edituser(request, user_id):
user.save() user.save()
return redirect("/users/") return redirect(reverse("users"))
return render(request, "manager/edituser.html", return render(request, "manager/edituser.html",
{ {
@ -273,7 +267,7 @@ def edituser(request, user_id):
) )
else: else:
return redirect('/account/login/?next=%s' % request.path) return redirect(reverse("two_factor:login", kwargs={'next': request.path}))
@login_required @login_required
def editwifi(request, wifi_id): def editwifi(request, wifi_id):
@ -283,7 +277,7 @@ def editwifi(request, wifi_id):
wifi = wifi or Wifi.objects.filter(id=wifi_id, organization=organization) wifi = wifi or Wifi.objects.filter(id=wifi_id, organization=organization)
if not wifi: if not wifi:
return redirect("/wifi/") return redirect(reverse("wifi"))
if request.POST.get("serial", ""): if request.POST.get("serial", ""):
newserial = request.POST.get("serial", "") newserial = request.POST.get("serial", "")
@ -303,7 +297,7 @@ def editwifi(request, wifi_id):
wifi[0].save() wifi[0].save()
return redirect("/wifi/") return redirect(reverse("wifi"))
return render(request, "manager/editwifi.html", return render(request, "manager/editwifi.html",
{ {
@ -318,7 +312,7 @@ def getconfig(request, device_id):
device = get_object_or_404(Device, id=device_id) device = get_object_or_404(Device, id=device_id)
if not mkfirmware(device, FWDIR): if not mkfirmware.delay(device, FWDIR).get(timeout=300):
return HttpResponse(status=503) return HttpResponse(status=503)
sigUpdateDevice(device.serial, None, False) sigUpdateDevice(device.serial, None, False)
@ -332,30 +326,25 @@ def getconfig(request, device_id):
@login_required @login_required
def rebootdevice(request, device_id): def rebootdevice(request, device_id):
device = None device = get_object_or_404(Device, id=device_id, organization__in=request.user.organization_set.all())
for organization in Organization.objects.filter(users=request.user):
device = device or Device.objects.filter(id=device_id, organization=organization)
if device and not device[0].reboot: if not device.reboot:
sigRebootDevice(device[0].serial, request.user.username, True) sigRebootDevice(device.serial, request.user.username, True)
device[0].reboot = True device.reboot = True
device[0].save() device.save()
return redirect("/devices/") return redirect(reverse("devices"))
@user_passes_test(is_staff) @user_passes_test(is_staff)
def updatedevice(request, device_id): def updatedevice(request, device_id):
device = None device = get_object_or_404(Device, id=device_id, organization__in=request.user.organization_set.all())
for organization in Organization.objects.filter(users=request.user): if not device.update:
device = device or Device.objects.filter(id=device_id, organization=organization) sigUpdateDevice(device.serial, request.user.username, True)
device.update = True
device.save()
if device and not device[0].update: return redirect(reverse("devices"))
sigUpdateDevice(device[0].serial, request.user.username, True)
device[0].update = True
device[0].save()
return redirect("/devices/")
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def deletedevice(request, device_id): def deletedevice(request, device_id):
@ -373,31 +362,31 @@ def deletedevice(request, device_id):
device.delete() device.delete()
return redirect("/devices/") return redirect(reverse("devices"))
@user_passes_test(is_staff) @user_passes_test(is_staff)
def deletewifi(request, wifi_id): def deletewifi(request, wifi_id):
wifi = get_object_or_404(Wifi, id=wifi_id, organization__in=request.user.organization_set.all()) wifi = get_object_or_404(Wifi, id=wifi_id, organization__in=request.user.organization_set.all())
wifi.delete() wifi.delete()
return redirect("/wifi/") return redirect(reverse("wifi"))
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def deleteuser(request, user_id): def deleteuser(request, user_id):
user = get_object_or_404(User, id=user_id) user = get_object_or_404(User, id=user_id)
user.delete() user.delete()
return redirect("/users/") return redirect(reverse("users"))
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def deletenetwork(request, network_id): def deletenetwork(request, network_id):
network = get_object_or_404(Network, id=network_id) network = get_object_or_404(Network, id=network_id)
network.delete() network.delete()
return redirect("/networks/") return redirect(reverse("networks"))
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def deleteorga(request, orga_id): def deleteorga(request, orga_id):
orga = get_object_or_404(Organization, id=orga_id) orga = get_object_or_404(Organization, id=orga_id)
orga.delete() orga.delete()
return redirect("/organizations/") return redirect(reverse("organizations"))
@user_passes_test(is_staff) @user_passes_test(is_staff)
def makewifi(request): def makewifi(request):
@ -420,7 +409,7 @@ def makewifi(request):
organization = Organization.objects.filter(id=wifi_organization)[0] organization = Organization.objects.filter(id=wifi_organization)[0]
) )
return redirect("/wifi/") return redirect(reverse("wifi"))
@user_passes_test(is_superuser) @user_passes_test(is_superuser)
def makenetwork(request): def makenetwork(request):
@ -430,7 +419,7 @@ def makenetwork(request):
data = form.cleaned_data data = form.cleaned_data
network = Network.objects.create(name=data["name"], intip=data["intip"], extip=data["extip"]) network = Network.objects.create(name=data["name"], intip=data["intip"], extip=data["extip"])
network.organization.set(data["orgas"]) network.organization.set(data["orgas"])
return redirect("/networks/") return redirect(reverse("networks"))
else: else:
form = NetworkForm(request.user) form = NetworkForm(request.user)
@ -445,7 +434,7 @@ def makeorganization(request):
data = form.cleaned_data data = form.cleaned_data
orga = Organization.objects.create(name=data["name"], userlimit=data["users"]) orga = Organization.objects.create(name=data["name"], userlimit=data["users"])
request.user.organization_set.add(orga) request.user.organization_set.add(orga)
return redirect("/organizations/") return redirect(reverse("organizations"))
else: else:
form = OrgaForm() form = OrgaForm()
@ -462,7 +451,7 @@ def editorganization(request, orga_id):
orga.name = data["name"] orga.name = data["name"]
orga.userlimit = data["users"] orga.userlimit = data["users"]
orga.save() orga.save()
return redirect("/organizations/") return redirect(reverse("organizations"))
else: else:
form = OrgaForm(initial={ "name": orga.name, "users": orga.userlimit }) form = OrgaForm(initial={ "name": orga.name, "users": orga.userlimit })
@ -480,9 +469,6 @@ def makedevice(request):
device_organization = request.POST.get("organization", "") device_organization = request.POST.get("organization", "")
device_model = request.POST.get("model", "") device_model = request.POST.get("model", "")
if not request.user.is_superuser:
return redirect("/devices/")
if not device_serial: if not device_serial:
orga = Organization.objects.all() orga = Organization.objects.all()
models = Model.objects.all() models = Model.objects.all()
@ -526,7 +512,7 @@ def makedevice(request):
changed = timezone.now() changed = timezone.now()
) )
return redirect("/devices/") return redirect(reverse("devices"))
@receiver(post_save, sender=settings.AUTH_USER_MODEL) @receiver(post_save, sender=settings.AUTH_USER_MODEL)
def createUserStatus(sender, instance, created, **kwargs): def createUserStatus(sender, instance, created, **kwargs):

View file

@ -40,30 +40,30 @@
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
{% if user.is_authenticated %} {% if user.is_authenticated %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/devices/">Devices</a> <a class="nav-link" href="{% url 'devices' %}">Devices</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/wifi/">WiFi</a> <a class="nav-link" href="{% url 'wifi' %}">WiFi</a>
</li> </li>
{% if user.is_staff %} {% if user.is_staff %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/users/">Users</a> <a class="nav-link" href="{% url 'users' %}">Users</a>
</li> </li>
{% endif %} {% endif %}
{% if user.is_superuser %} {% if user.is_superuser %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/networks/">Networks</a> <a class="nav-link" href="{% url 'networks' %}">Networks</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/organizations/">Organizations</a> <a class="nav-link" href="{% url 'organizations' %}">Organizations</a>
</li> </li>
{% endif %} {% endif %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/accounts/logout">Logout</a> <a class="nav-link" href="{% url 'logout' %}">Logout</a>
</li> </li>
{% else %} {% else %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/accounts/login">Login</a> <a class="nav-link" href="{% url 'two_factor:login' %}">Login</a>
</li> </li>
{% endif %} {% endif %}
</ul> </ul>
@ -85,7 +85,7 @@
<footer class="footer"> <footer class="footer">
<div class="container"> <div class="container">
<span class="text-muted">&copy; <a style="color: rgb(108, 117, 125);" href="https://kumi.systems/">Kumi Systems e.U.</a>{% if user.is_superuser %}<br/>Git Revision: {% getCommit %}{% isLatestCommit %}{% endif %}</span> <span class="text-muted">&copy; <a style="color: rgb(108, 117, 125);" href="https://kumi.systems/">Kumi Systems e.U.</a>{% if user.is_superuser %}<br/>Git Revision: {% getCommit %}{% isLatestCommit %}{% isDirty %}{% endif %}</span>
</div> </div>
</footer> </footer>

View file

@ -0,0 +1,14 @@
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because a user account has been created for you at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans "Your username is:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}