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

View file

@ -40,30 +40,30 @@
<ul class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="/devices/">Devices</a>
<a class="nav-link" href="{% url 'devices' %}">Devices</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/wifi/">WiFi</a>
<a class="nav-link" href="{% url 'wifi' %}">WiFi</a>
</li>
{% if user.is_staff %}
<li class="nav-item">
<a class="nav-link" href="/users/">Users</a>
<a class="nav-link" href="{% url 'users' %}">Users</a>
</li>
{% endif %}
{% if user.is_superuser %}
<li class="nav-item">
<a class="nav-link" href="/networks/">Networks</a>
<a class="nav-link" href="{% url 'networks' %}">Networks</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/organizations/">Organizations</a>
<a class="nav-link" href="{% url 'organizations' %}">Organizations</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" href="/accounts/logout">Logout</a>
<a class="nav-link" href="{% url 'logout' %}">Logout</a>
</li>
{% else %}
<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>
{% endif %}
</ul>
@ -85,7 +85,7 @@
<footer class="footer">
<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>
</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 %}