aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-09-09 03:12:07 -0400
committerBobby <[email protected]>2022-09-09 03:12:07 -0400
commitc68d7c6a7281d7c58dd00f60b2a9dc5dcaad2368 (patch)
tree310a8890affd6e622bda4081d06a34de5f1ef727
parent766d2d3558f27bc80ba03406d0d8c34832e18977 (diff)
downloadthatcomputerscientist-c68d7c6a7281d7c58dd00f60b2a9dc5dcaad2368.tar.xz
thatcomputerscientist-c68d7c6a7281d7c58dd00f60b2a9dc5dcaad2368.zip
manage users and search users page
-rw-r--r--blog_admin/urls.py1
-rw-r--r--blog_admin/views.py37
-rw-r--r--static/css/main.css29
-rw-r--r--templates/blog/partials/sidebar.html2
-rw-r--r--templates/blog_admin/users.html89
5 files changed, 154 insertions, 4 deletions
diff --git a/blog_admin/urls.py b/blog_admin/urls.py
index 63459ba9..52b30bbe 100644
--- a/blog_admin/urls.py
+++ b/blog_admin/urls.py
@@ -9,4 +9,5 @@ urlpatterns = [
path('categories', views.categories, name='categories'),
path('tags', views.tags, name='tags'),
path('new', views.new, name='new'),
+ path('search', views.search, name='search'),
] \ No newline at end of file
diff --git a/blog_admin/views.py b/blog_admin/views.py
index 6196c7ab..37c6f4eb 100644
--- a/blog_admin/views.py
+++ b/blog_admin/views.py
@@ -1,8 +1,18 @@
-from django.shortcuts import render
+from django.shortcuts import render, redirect
+from users.models import UserProfile
+from django.contrib.auth.models import User
# Create your views here.
def users(request):
- pass
+ if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff):
+ # Get the superusers
+ superusers = User.objects.filter(is_superuser=True)
+ # Get the normal users
+ users = User.objects.filter(is_superuser=False)
+
+ return render(request, 'blog_admin/users.html', { 'title': 'Manage Users', 'super_users': superusers, 'normal_users': users })
+ else:
+ return redirect('blog:home')
def posts(request):
pass
@@ -17,4 +27,25 @@ def tags(request):
pass
def new(request):
- pass \ No newline at end of file
+ pass
+
+def search(request):
+ q = request.GET.get('q')
+ if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff):
+ if q:
+ try:
+ # Get the superusers where username or email or first_name or last_name contains q or the user id is int(q)
+ superusers = User.objects.filter(is_superuser=True).filter(username__icontains=q) | User.objects.filter(is_superuser=True).filter(email__icontains=q) | User.objects.filter(is_superuser=True).filter(first_name__icontains=q) | User.objects.filter(is_superuser=True).filter(last_name__icontains=q) | User.objects.filter(is_superuser=True).filter(id = int(q))
+ # Get the normal users where username or email or first_name or last_name contains q or the user id is int(q)
+ users = User.objects.filter(is_superuser=False).filter(username__icontains=q) | User.objects.filter(is_superuser=False).filter(email__icontains=q) | User.objects.filter(is_superuser=False).filter(first_name__icontains=q) | User.objects.filter(is_superuser=False).filter(last_name__icontains=q) | User.objects.filter(is_superuser=False).filter(id = int(q))
+ except:
+ # Get the superusers where username or email or first_name or last_name contains q
+ superusers = User.objects.filter(is_superuser=True).filter(username__icontains=q) | User.objects.filter(is_superuser=True).filter(email__icontains=q) | User.objects.filter(is_superuser=True).filter(first_name__icontains=q) | User.objects.filter(is_superuser=True).filter(last_name__icontains=q)
+ # Get the normal users where username or email or first_name or last_name contains q
+ users = User.objects.filter(is_superuser=False).filter(username__icontains=q) | User.objects.filter(is_superuser=False).filter(email__icontains=q) | User.objects.filter(is_superuser=False).filter(first_name__icontains=q) | User.objects.filter(is_superuser=False).filter(last_name__icontains=q)
+
+ return render(request, 'blog_admin/users.html', { 'title': 'Search Results for "{}"'.format(q), 'super_users': superusers, 'normal_users': users })
+ else:
+ return redirect('blog-admin:users')
+ else:
+ return redirect('blog:home') \ No newline at end of file
diff --git a/static/css/main.css b/static/css/main.css
index 32afdeb8..1250a173 100644
--- a/static/css/main.css
+++ b/static/css/main.css
@@ -169,6 +169,35 @@ nav > ul > li {
cursor: pointer;
}
+.float-right {
+ /* Float to the top right corner */
+ position: absolute;
+ top: 2.5em;
+ right: 3em;
+}
+
+.table {
+ border-collapse: collapse;
+ width: 100%;
+ margin-top: 10px;
+}
+
+/* Add borders to all cells */
+.table, .table th, .table td {
+ border: 1px solid #191919;
+}
+
+.table-striped > tbody > tr:nth-child(odd) {
+ background-color: #f2f2f2;
+}
+
+summary {
+ cursor: pointer;
+ font-size: 1.2rem;
+ font-weight: bold;
+ margin-top: 1em;
+}
+
/* Optimize for phones */
@media only screen and (max-width: 480px) {
body {
diff --git a/templates/blog/partials/sidebar.html b/templates/blog/partials/sidebar.html
index cd0a8cda..3d49239b 100644
--- a/templates/blog/partials/sidebar.html
+++ b/templates/blog/partials/sidebar.html
@@ -50,7 +50,7 @@
<li><a href="/contact">Contact</a></li>
<li><a href="/blog">Blog</a></li>
{% if user.is_authenticated %}
- <li><a href="{% url 'account' %}">My Account</a></li>
+ <li><a href="{% url 'blog:account' %}">My Account</a></li>
<li><a href="{% url 'users:logout' %}">Logout</a></li>
{% else %}
<li><a href="/register">Register</a></li>
diff --git a/templates/blog_admin/users.html b/templates/blog_admin/users.html
new file mode 100644
index 00000000..4c6419fc
--- /dev/null
+++ b/templates/blog_admin/users.html
@@ -0,0 +1,89 @@
+{% extends 'blog/partials/base.html' %} {% block content %}
+<div class="main">
+ <section>
+ <h1 style="font-size: 2em;">{{ title }}</h1>
+ <div class="float-right">
+ <a href="#" >Create New User</a>
+ {% comment %} Search Users Box {% endcomment %}
+ <form style="display: inline-block; margin-left: 10px;" action="{% url 'blog-admin:search' %}" method="get">
+ <input style="display: inline-block" type="text" name="q" placeholder="Search Users" autocomplete="off"/>
+ <input style="display: inline-block" type="submit" value="Search" />
+ </form>
+ </div>
+ <hr>
+ {% comment %} Collapsible Section for super-users {% endcomment %}
+ <details>
+ <summary>Super Users</summary>
+ <table class="table table-striped">
+ <thead>
+ <tr>
+ <th scope="col">User ID</th>
+ <th scope="col">Username</th>
+ <th scope="col">First Name</th>
+ <th scope="col">Last Name</th>
+ <th scope="col">Email</th>
+ <th scope="col">Role(s)</th>
+ <th scope="col">Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for user in super_users %}
+ <tr>
+ <td>{{ user.id }}</td>
+ <td>{{ user.username }}</td>
+ <td>{{ user.first_name }}</td>
+ <td>{{ user.last_name }}</td>
+ <td>{{ user.email }}</td>
+ <td>
+ {% if user.is_superuser %}Super user, {% endif %}
+ {% if user.is_staff %}Staff, {% endif %}
+ {% if user.is_active %}Active{% endif %}
+ </td>
+ <td>
+ <a href="#" class="btn btn-primary btn-sm">Edit</a>
+ <a href="#" class="btn btn-danger btn-sm">Delete</a>
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </details>
+ {% comment %} Collapsible Section for normal users {% endcomment %}
+ <details open>
+ <summary>Normal Users</summary>
+ <table class="table table-striped">
+ <thead>
+ <tr>
+ <th scope="col">User ID</th>
+ <th scope="col">Username</th>
+ <th scope="col">First Name</th>
+ <th scope="col">Last Name</th>
+ <th scope="col">Email</th>
+ <th scope="col">Role(s)</th>
+ <th scope="col">Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for user in normal_users %}
+ <tr>
+ <td>{{ user.id }}</td>
+ <td>{{ user.username }}</td>
+ <td>{{ user.first_name }}</td>
+ <td>{{ user.last_name }}</td>
+ <td>{{ user.email }}</td>
+ <td>
+ {% if user.is_superuser %}Super user, {% endif %}
+ {% if user.is_staff %}Staff, {% endif %}
+ {% if user.is_active %}Active{% endif %}
+ </td>
+ <td>
+ <a href="#" class="btn btn-primary btn-sm">Edit</a>
+ <a href="#" class="btn btn-danger btn-sm">Delete</a>
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </section>
+</div>
+{% endblock %}