diff options
| -rw-r--r-- | blog_admin/urls.py | 1 | ||||
| -rw-r--r-- | blog_admin/views.py | 57 | ||||
| -rw-r--r-- | templates/blog/partials/sidebar.html | 6 | ||||
| -rw-r--r-- | templates/blog_admin/edit_user.html | 66 | ||||
| -rw-r--r-- | templates/blog_admin/new_user.html | 22 | ||||
| -rw-r--r-- | templates/blog_admin/users.html | 109 |
6 files changed, 167 insertions, 94 deletions
diff --git a/blog_admin/urls.py b/blog_admin/urls.py index 84d61bbc..2862db88 100644 --- a/blog_admin/urls.py +++ b/blog_admin/urls.py @@ -5,6 +5,7 @@ app_name = 'blog-admin' urlpatterns = [ path('users', views.users, name='users'), path('users/new', views.new_user, name='new-user'), + path('users/<int:user_id>/edit', views.edit_user, name='edit-user'), path('posts', views.posts, name='posts'), path('comments', views.comments, name='comments'), path('categories', views.categories, name='categories'), diff --git a/blog_admin/views.py b/blog_admin/views.py index 34b4176b..372ac1e2 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -11,12 +11,11 @@ def users(request): page = int(page) except: page = 1 - superusers = User.objects.filter(is_superuser=True) users = User.objects.filter(is_superuser=False)[(page-1)*50:page*50] num_pages = User.objects.filter(is_superuser=False).count() // 50 + 1 print(num_pages) url_to_render = 'blog_admin/users.html?page={}'.format(page) if int(page) and int(page) > 1 else 'blog_admin/users.html' - return render(request, url_to_render, { 'title': 'Manage Users', 'super_users': superusers, 'normal_users': users, 'num_pages': num_pages, 'page': page }) + return render(request, url_to_render, { 'title': 'Manage Users', 'normal_users': users, 'num_pages': num_pages, 'page': page }) else: return redirect('blog:home') @@ -40,17 +39,13 @@ def search(request): 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 }) + return render(request, 'blog_admin/users.html', { 'title': 'Search Results for "{}"'.format(q), 'normal_users': users }) else: return redirect('blog-admin:users') else: @@ -92,4 +87,50 @@ def new_user(request): return redirect('blog-admin:new-user') else: - return render(request, 'blog_admin/new_user.html', { 'title': 'Create New User' })
\ No newline at end of file + return render(request, 'blog_admin/new_user.html', { 'title': 'Create New User' }) + else: + return redirect('blog:home') + +def edit_user(request, user_id): + if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): + if request.method == 'POST': + request_user = request.user + user = User.objects.get(id=user_id) + user.username = request.POST.get('username') + user.email = request.POST.get('email') + user.first_name = request.POST.get('first_name') + user.last_name = request.POST.get('last_name') + user.is_superuser = True if request_user.is_superuser and request.POST.get('is_superuser') == 'on' else False + user.is_staff = True if request_user.is_superuser and request.POST.get('is_staff') == 'on' else False + user.is_active = True if request.POST.get('is_active') == 'on' else False + + # User Profile Data + try: + user_profile = UserProfile.objects.get(user=user) + except: + user_profile = UserProfile(user=user) + user_profile.bio = request.POST.get('bio') + user_profile.location = request.POST.get('location') + user_profile.gravatar_email = request.POST.get('gravatar_email') + user_profile.email_verified = True if request.POST.get('email_verified') == 'on' else False + + # Save the user + try: + user.save() + user_profile.save() + messages.success(request, 'User updated successfully!') + return redirect('blog-admin:users') + # maybe user name is taken + except Exception as e: + messages.error(request, 'Error: {}'.format(e), extra_tags='edit_user_update_error') + return redirect('blog-admin:edit-user', user_id=user_id) + + else: + user = User.objects.get(id=user_id) + try: + user_profile = UserProfile.objects.get(user=user) + except: + user_profile = None + return render(request, 'blog_admin/edit_user.html', { 'title': 'Edit User', 'edit_user': user, 'edit_user_profile': user_profile }) + else: + return redirect('blog:home')
\ No newline at end of file diff --git a/templates/blog/partials/sidebar.html b/templates/blog/partials/sidebar.html index 3d49239b..0891ad41 100644 --- a/templates/blog/partials/sidebar.html +++ b/templates/blog/partials/sidebar.html @@ -59,12 +59,14 @@ </nav> </fieldset> <br> - {% if user.is_superuser %} + {% if user.is_staff %} <fieldset> <legend>Admin</legend> <nav> <ul> - <li><a href="{% url 'admin:index' %}">Admin Area</a></li> + {% if user.is_superuser %} + <li><a href="{% url 'admin:index' %}">Admin Area</a></li> + {% endif %} <li><a href="{% url 'blog-admin:users' %}">Manage Users</a></li> <li><a href="{% url 'blog-admin:posts' %}">Manage Posts</a></li> <li><a href="{% url 'blog-admin:comments' %}">Manage Comments</a></li> diff --git a/templates/blog_admin/edit_user.html b/templates/blog_admin/edit_user.html new file mode 100644 index 00000000..be32f236 --- /dev/null +++ b/templates/blog_admin/edit_user.html @@ -0,0 +1,66 @@ +{% extends 'blog/partials/base.html' %} {% block content %} +<div class="main"> + <section> + {% include 'blog_admin/partials/main_section.html' %} + <form action="{% url 'blog-admin:edit-user' edit_user.id %}" method="post"> + {% csrf_token %} + <div class="form-group"> + <label for="username">Username</label> + <input type="text" class="form-control" id="username" name="username" placeholder="Username" value="{{ edit_user.username }}"> + </div> + <div class="form-group"> + <label for="email">First Name</label> + <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name" value="{{ edit_user.first_name }}"> + </div> + <div class="form-group"> + <label for="email">Last Name</label> + <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name" value="{{ edit_user.last_name }}"> + </div> + <div class="form-group"> + <label for="email">Email</label> + <input type="email" class="form-control" id="email" name="email" placeholder="Email" value="{{ edit_user.email }}"> + </div> + {% if user.is_superuser %} + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_superuser" name="is_superuser" {% if edit_user.is_superuser %}checked{% endif %}> + <label style="display:inline-block" for="is_superuser">Superuser</label> + </div> + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_staff" name="is_staff" {% if edit_user.is_staff %}checked{% endif %}> + <label style="display:inline-block" for="is_staff">Staff</label> + </div> + {% endif %} + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_active" name="is_active" {% if edit_user.is_active %}checked{% endif %}> + <label style="display:inline-block" for="is_active">Active</label> + </div> + <div class="form-group"> + <label for="bio">Bio</label> + <textarea class="form-control" id="bio" name="bio" placeholder="Bio"></textarea value="{{ edit_user_profile.bio }}"> + </div> + <div class="form-group"> + <label for="location">Location</label> + <input type="text" class="form-control" id="location" name="location" placeholder="Location" value="{{ edit_user_profile.location }}"> + </div> + <div class="form-group"> + <label for="gravatar_email">Gravatar Email</label> + <input type="email" class="form-control" id="gravatar_email" name="gravatar_email" placeholder="Gravatar Email" value="{{ edit_user_profile.gravatar_email }}"> + </div> + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_public" name="is_public" disabled> + <label style="display:inline-block" for="is_public">Public</label> + </div> + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="email_public" name="email_public" disabled> + <label style="display:inline-block" for="email_public">Email Public</label> + </div> + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="email_verified" name="email_verified" {% if edit_user_profile.email_verified %}checked{% endif %}> + <label style="display:inline-block" for="email_verified">Email Verified</label> + </div> + <div class="form-group"> + <input type="submit" name="submit" value="Save User Details" id="submit"> + </div> + </section> +</div> +{% endblock %} diff --git a/templates/blog_admin/new_user.html b/templates/blog_admin/new_user.html index b5d37b30..fd195912 100644 --- a/templates/blog_admin/new_user.html +++ b/templates/blog_admin/new_user.html @@ -28,16 +28,18 @@ <label for="password">Confirm Password</label> <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Confirm Password"> </div> - <div class="form-group"> - <input style="display:inline-block" type="checkbox" class="form-control" id="is_superuser" name="is_superuser"> - <label style="display:inline-block" for="is_superuser">Superuser</label> - </div> - <div class="form-group"> - <input style="display:inline-block" type="checkbox" class="form-control" id="is_staff" name="is_staff"> - <label style="display:inline-block" for="is_staff">Staff</label> - </div> - <div class="form-group"> - <input style="display:inline-block" type="checkbox" class="form-control" id="is_active" name="is_active" checked> + {% if user.is_superuser %} + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_superuser" name="is_superuser" {% if edit_user.is_superuser %}checked{% endif %}> + <label style="display:inline-block" for="is_superuser">Superuser</label> + </div> + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_staff" name="is_staff" {% if edit_user.is_staff %}checked{% endif %}> + <label style="display:inline-block" for="is_staff">Staff</label> + </div> + {% endif %} + <div class="form-group"> + <input style="display:inline-block" type="checkbox" class="form-control" id="is_active" name="is_active" {% if edit_user.is_active %}checked{% endif %}> <label style="display:inline-block" for="is_active">Active</label> </div> <div class="form-group"> diff --git a/templates/blog_admin/users.html b/templates/blog_admin/users.html index 411ee1f8..084198d7 100644 --- a/templates/blog_admin/users.html +++ b/templates/blog_admin/users.html @@ -2,80 +2,41 @@ <div class="main"> <section> {% include 'blog_admin/partials/main_section.html' %} - {% 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> - </details> + <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 current_user in normal_users %} + {% if current_user.id != user.id %} + <tr> + <td>{{ current_user.id }}</td> + <td>{{ current_user.username }}</td> + <td>{{ current_user.first_name }}</td> + <td>{{ current_user.last_name }}</td> + <td>{{ current_user.email }}</td> + <td> + {% if current_user.is_superuser %}Super user, {% endif %} + {% if current_user.is_staff %}Staff, {% endif %} + {% if current_user.is_active %}Active{% endif %} + </td> + <td> + <a href="{% url 'blog-admin:edit-user' current_user.id %}" class="btn btn-primary btn-sm">Edit</a> + <a href="#" class="btn btn-danger btn-sm">Delete</a> + </td> + </tr> + {% endif %} + {% endfor %} + </tbody> + </table> {% if num_pages and page %} <div class="pagination"> {% if page == 1 %} |
