diff options
| author | Bobby <[email protected]> | 2022-09-18 23:32:07 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-18 23:32:07 -0400 |
| commit | cdf4502054453b101c9bf00d5c33241d767fcd94 (patch) | |
| tree | 44fbdfd77bf96f677662054046cedd45adc742d1 | |
| parent | 140331e3d9a5a411587f0a6a0d8fd6efc7fac1b2 (diff) | |
| download | thatcomputerscientist-cdf4502054453b101c9bf00d5c33241d767fcd94.tar.xz thatcomputerscientist-cdf4502054453b101c9bf00d5c33241d767fcd94.zip | |
Added function to create new categories and returning categories on new post page
| -rw-r--r-- | blog_admin/urls.py | 3 | ||||
| -rw-r--r-- | blog_admin/views.py | 42 | ||||
| -rw-r--r-- | templates/blog_admin/categories.html | 53 | ||||
| -rw-r--r-- | templates/blog_admin/new_category.html | 38 | ||||
| -rw-r--r-- | templates/blog_admin/partials/category_topbar.html | 22 |
5 files changed, 157 insertions, 1 deletions
diff --git a/blog_admin/urls.py b/blog_admin/urls.py index c47fb9cf..b0710851 100644 --- a/blog_admin/urls.py +++ b/blog_admin/urls.py @@ -12,6 +12,9 @@ urlpatterns = [ path('posts/search', views.posts_search, name='posts-search'), path('comments', views.comments, name='comments'), path('categories', views.categories, name='categories'), + path('categories/new', views.new_category, name='new-category'), + path('categories/<int:category_id>/edit', views.edit_category, name='edit-category'), + path('categories/<int:category_id>/delete', views.edit_category, name='delete-category'), path('tags', views.tags, name='tags'), path('new', views.new, name='new'), ]
\ No newline at end of file diff --git a/blog_admin/views.py b/blog_admin/views.py index 8ea2f815..b139a351 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -63,7 +63,8 @@ def new_post(request): # messages.error(request, 'Error: All fields are required!', extra_tags='new_post_create_error', data = { 'title': title, 'body': body, 'category': category, 'tags': tags, 'slug': slug }) # return redirect('blog-admin:new-post') else: - return render(request, 'blog_admin/new_post.html', { 'title': 'New Post' }) + categories = Category.objects.all() + return render(request, 'blog_admin/new_post.html', { 'title': 'New Post', 'categories': categories }) else: return redirect('blog:home') @@ -71,6 +72,45 @@ def comments(request): pass def categories(request): + if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): + page = request.GET.get('page') if request.GET.get('page') else 1 + try: + page = int(page) + except: + page = 1 + categories = Category.objects.all()[(page-1)*50:page*50] + num_pages = Category.objects.all().count() // 50 + 1 + url_to_render = 'blog_admin/categories.html?page={}'.format(page) if int(page) and int(page) > 1 else 'blog_admin/categories.html' + return render(request, url_to_render, { 'title': 'Manage Categories', 'categories': categories, 'num_pages': num_pages, 'page': page }) + else: + return redirect('blog:home') + +def new_category(request): + if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): + if request.method == 'POST': + name = request.POST.get('name') + slug = request.POST.get('slug') + description = request.POST.get('description') if request.POST.get('description') else '' + if name and slug: + try: + category = Category.objects.create(name = name, slug = slug, description = description) + messages.success(request, 'Category created successfully!') + return redirect('blog-admin:categories') + except Exception as e: + messages.error(request, 'Error: {}'.format(e), extra_tags='new_category_create_error', data = { 'name': name, 'slug': slug, 'description': description }) + return redirect('blog-admin:new-category') + else: + messages.error(request, 'Error: All fields are required!', extra_tags='new_category_create_error', data = { 'name': name, 'slug': slug, 'description': description }) + return redirect('blog-admin:new-category') + else: + return render(request, 'blog_admin/new_category.html', { 'title': 'New Category' }) + else: + return redirect('blog:home') + +def edit_category(request, slug): + pass + +def delete_category(request, slug): pass def tags(request): diff --git a/templates/blog_admin/categories.html b/templates/blog_admin/categories.html new file mode 100644 index 00000000..9a3152bd --- /dev/null +++ b/templates/blog_admin/categories.html @@ -0,0 +1,53 @@ +{% extends 'blog/partials/base.html' %} {% block content %} +<div class="main"> + <section> + {% include 'blog_admin/partials/category_topbar.html' %} + <table class="table table-striped"> + <thead> + <tr> + <th>Category Name</th> + <th>Category Description</th> + <th>Category Slug</th> + <th>Created At</th> + <th>Actions</th> + </tr> + </thead> + <tbody> + {% for category in categories %} + <tr> + <td>{{ category.name }}</td> + <td>{{ category.description }}</td> + <td>{{ category.slug }}</td> + <td>{{ category.created_at }}</td> + <td> + <a href="{% url 'blog-admin:edit-category' category.id %}">Edit</a> + <a href="{% url 'blog-admin:delete-category' category.id %}">Delete</a> + </td> + </tr> + {% endfor %} + </tbody> + </table> + {% if num_pages and page %} + <div class="pagination"> + {% if page == 1 %} + <a href="#" class="disabled">«</a> + <a href="#" class="disabled">‹</a> + {% else %} + <a href="{% url 'blog-admin:categories'%}?page=1">«</a> + <a href="{% url 'blog-admin:categories'%}?page={{ page|add:-1 }}">‹</a> + {% endif %} + {% for i in num_pages|make_list %} + <a href="{% url 'blog-admin:categories'%}?page={{ i }}">{{ i }}</a> + {% endfor %} + {% if page == num_pages %} + <a href="#" class="disabled">›</a> + <a href="#" class="disabled">»</a> + {% else %} + <a href="{% url 'blog-admin:categories'%}?page={{ page|add:1 }}">›</a> + <a href="{% url 'blog-admin:categories'%}?page={{ num_pages }}">»</a> + {% endif %} + </div> + {% endif %} + </section> +</div> +{% endblock %} diff --git a/templates/blog_admin/new_category.html b/templates/blog_admin/new_category.html new file mode 100644 index 00000000..ed27767e --- /dev/null +++ b/templates/blog_admin/new_category.html @@ -0,0 +1,38 @@ +{% extends 'blog/partials/base.html' %} {% block content %} +<div class="main"> + <section> + {% include 'blog_admin/partials/category_topbar.html' %} + <form action="{% url 'blog-admin:new-category' %}" method="post"> + {% csrf_token %} + <div class="form-group"> + <label for="name">Name</label> + <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" value="{{ category.name }}"> + </div> + <div class="form-group"> + <label for="slug">Slug</label> + <input type="text" class="form-control" id="slug" name="slug" placeholder="Enter slug" value="{{ category.slug }}"> + </div> + <div class="form-group"> + <label for="description">Description</label> + <textarea class="form-control" id="description" name="description" rows="3" placeholder="Enter description">{{ category.description }}</textarea> + </div> + <button type="submit" class="btn btn-primary">Submit</button> + </form> + </section> +</div> +<script> + function slugify(text) { + return text.toString().toLowerCase() + .replace(/\s+/g, '-') // Replace spaces with - + .replace(/[^\w\-]+/g, '') // Remove all non-word chars + .replace(/\-\-+/g, '-') // Replace multiple - with single - + .replace(/^-+/, '') // Trim - from start of text + .replace(/-+$/, ''); // Trim - from end of text + } + const name = document.getElementById('name'); + const slug = document.getElementById('slug'); + name.addEventListener('keyup', function() { + slug.value = slugify(name.value); + }); +</script> +{% endblock %}
\ No newline at end of file diff --git a/templates/blog_admin/partials/category_topbar.html b/templates/blog_admin/partials/category_topbar.html new file mode 100644 index 00000000..d03f82db --- /dev/null +++ b/templates/blog_admin/partials/category_topbar.html @@ -0,0 +1,22 @@ +<h1 style="font-size: 2em">{{ title }}</h1> +<div class="float-right"> + <a href="{% url 'blog-admin:new-category' %}">Create New Category</a> + <form + style="display: inline-block; margin-left: 10px" + action="{% url 'blog-admin:users-search' %}" + method="get" + > + <input + style="display: inline-block" + type="text" + name="q" + placeholder="Search Categories" + autocomplete="off" + /> + <input style="display: inline-block" type="submit" value="Search" /> + </form> +</div> +<hr /> +{% for message in messages %} +<p class="{{message.tags}}" style="text-align: center">{{ message }}</p> +{% endfor %} |
