Skip to content

Commit 36f2dcd

Browse files
committed
Add automatic user admin creation
1 parent 3761370 commit 36f2dcd

11 files changed

Lines changed: 27 additions & 31 deletions

File tree

WebApi/backend/migrations/0001_initial.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
# Generated by Django 4.2.3 on 2023-07-14 05:49
1+
# Generated by Django 4.2.3 on 2023-07-16 07:55
22

33
import django.contrib.auth.models
44
import django.contrib.auth.validators
55
from django.db import migrations, models
66
import django.utils.timezone
7+
from django.contrib.auth import get_user_model
8+
9+
def create_admin_user(apps, schema_editor):
10+
User = get_user_model()
11+
admin_username = 'admin'
12+
admin_password = '123'
13+
if not User.objects.filter(is_staff=True).exists():
14+
User.objects.create_superuser(admin_username, '', admin_password)
715

816

917
class Migration(migrations.Migration):
@@ -23,7 +31,7 @@ class Migration(migrations.Migration):
2331
('date', models.CharField(max_length=10, verbose_name='Date')),
2432
('hour', models.CharField(max_length=10, verbose_name='Hour')),
2533
('switch', models.CharField(max_length=10, verbose_name='Switch')),
26-
('host', models.TextField(verbose_name='Host')),
34+
('host', models.TextField(verbose_name='Hosts')),
2735
('playbook', models.TextField(verbose_name='Playbook')),
2836
('output', models.TextField(verbose_name='Output')),
2937
],
@@ -57,4 +65,5 @@ class Migration(migrations.Migration):
5765
('objects', django.contrib.auth.models.UserManager()),
5866
],
5967
),
68+
migrations.RunPython(create_admin_user),
6069
]

WebApi/backend/migrations/0002_alter_ansiblelog_host.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

WebApi/backend/views/user.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from django.urls import reverse_lazy
77
from django.db.models import Q
88
from backend.views.utils import AdminRequired
9+
10+
911
class UserPageView(AdminRequired, TemplateView):
1012
template_name = 'tableUser.html'
1113

@@ -21,6 +23,7 @@ def post(self, request ,*args, **kwargs):
2123
users = CustomUser.objects.filter(Q(username__icontains=username))
2224
return render(request, self.template_name, {'users': users})
2325

26+
2427
class CreateUserView(AdminRequired, TemplateView):
2528
template_name = 'addUser.html'
2629

@@ -39,6 +42,7 @@ def post(self, request ,*args, **kwargs):
3942
result = "Error in creating user"
4043
return render(request, self.template_name, {'result': result})
4144

45+
4246
class UpdateUserView(AdminRequired, UpdateView):
4347
template_name = 'updateUser.html'
4448
model = CustomUser

WebApi/backend/views/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
2-
2+
from django.http import Http404, HttpResponse, HttpResponseForbidden
33

44
class AdminRequired(LoginRequiredMixin, UserPassesTestMixin):
55

WebApi/frontend/templates/_base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
<div class="nav_list">
2525
<a href="{% url 'home' %}" class="nav_link"> <i class='bx bx-home nav_icon'></i> <span class="nav_name">Home</span> </a>
2626
<a href="{% url 'ansible' %}" class="nav_link"> <i class='bx bx-message-square-detail nav_icon'></i> <span class="nav_name">Ansible</span> </a>
27-
<a href="{% url 'log' %}" class="nav_link"> <i class='bx bx-notepad nav_icon'></i> <span class="nav_name">User Logbook</span> </a>
27+
<a href="{% url 'log' %}" class="nav_link"> <i class='bx bx-notepad nav_icon'></i> <span class="nav_name">Activity Log</span> </a>
2828
<a href="#" class="nav_link"> <i class='bx bxs-book-open nav_icon'></i> <span class="nav_name">Documentation</span> </a>
2929
</a>
3030
</div>
3131
</div>
3232
<div>
3333
{% if user.is_staff %}
34-
<a href="{% url 'userList' %}" class="nav_link"> <i class='bx bx-user nav_icon'></i> <span class="nav_name">List Users</span> </a>
34+
<a href="{% url 'userList' %}" class="nav_link"> <i class='bx bx-user nav_icon'></i> <span class="nav_name">Manage Users</span> </a>
3535
<a href="{% url 'userCreate' %}" class="nav_link"> <i class='bx bx-user-plus nav_icon'></i> <span class="nav_name">Create User</span> </a>
3636
{% endif %}
3737
<a href="{% url 'account_logout' %}" class="nav_link"> <i class='bx bx-log-out nav_icon'></i> <span class="nav_name">SignOut</span></a>

WebApi/frontend/templates/addUser.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends '_base.html' %}
2+
{% block title %}SwitchSetup{% endblock %}
23
{% block content %}
34

45
{% if result %}

WebApi/frontend/templates/ansible.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{% extends '_base.html' %}
2-
3-
{% block title %}Ansible{% endblock %}
2+
{% block title %}SwitchSetup{% endblock %}
43
{% block content %}
54

65
<h1>Ansible Configuration</h1>
@@ -15,7 +14,7 @@ <h1>Ansible Configuration</h1>
1514
</div>
1615
<div class="form-group">
1716
<label for="exampleFormControlInput1">Switch SSH Password</label>
18-
<input class="form-control" type="password" name="password" placeholder="Digite seu usuário">
17+
<input class="form-control" type="password" name="password" placeholder="Password">
1918
</div>
2019
<div class="form-group">
2120
<label for="exampleFormControlSelect1">Select Switch</label>

WebApi/frontend/templates/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends '_base.html' %}
2-
{% block title %}SwitchSetupAPI{% endblock %}
2+
{% block title %}SwitchSetup{% endblock %}
33
{% block content %}
44
<h1 >What is <span style="color: #4723D9">SwitchSetup</span>?</h1>
55
<p>This software is designed to configure network devices using Ansible, assisting configurations of many equipment.</p>

WebApi/frontend/templates/log.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends '_base.html' %}
2-
{% block title %}SwitchSetupAPI - Log{% endblock %}
2+
{% block title %}SwitchSetup{% endblock %}
33
{% block content %}
44

55
<h1>Activity Log</h1>
@@ -47,7 +47,7 @@ <h3 style="margin-top: 35px;">Search Result</h3>
4747
<div class="panel-heading">
4848
Activity Table
4949
</div>
50-
<div class="panel-body">
50+
<div class="panel-body ">
5151
<table class="table table-condensed table-striped">
5252
<thead>
5353
<tr>

WebApi/frontend/templates/tableUser.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{% extends '_base.html' %}
2-
{% block title %}SwitchSetupAPI - Log{% endblock %}
2+
{% block title %}SwitchSetup{% endblock %}
33
{% block content %}
44

5-
<h1>User List</h1>
5+
<h1>Manage User</h1>
66

77
<form method="post">
88
{% csrf_token %}

0 commit comments

Comments
 (0)