Skip to content

Commit

Permalink
add CategoryCreateView
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 24, 2024
1 parent c2a7c97 commit 4e9a0f8
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
70 changes: 70 additions & 0 deletions lacommunaute/documentation/tests/tests_category_create_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest # noqa

from django.urls import reverse

from lacommunaute.documentation.models import Category
from lacommunaute.users.factories import UserFactory


@pytest.fixture(name="url")
def fixture_url():
return reverse("documentation:category_create")


@pytest.fixture(name="superuser")
def fixture_superuser(db):
return UserFactory(is_superuser=True)


@pytest.mark.parametrize(
"user,status_code", [(None, 302), (lambda: UserFactory(), 403), (lambda: UserFactory(is_superuser=True), 200)]
)
def test_user_passes_test_mixin(client, db, url, user, status_code):
if user:
client.force_login(user())
response = client.get(url)
assert response.status_code == status_code


@pytest.fixture(name="expected_context")
def fixture_expected_context():
return {
"title": "Créer une nouvelle catégorie",
"back_url": reverse("documentation:category_list"),
}


def test_view(client, db, url, superuser, expected_context):
client.force_login(superuser)
response = client.get(url)
assert response.status_code == 200
assert {k: response.context[k] for k in expected_context.keys()} == expected_context
assert response.context["form"].fields.keys() == {"name", "short_description", "description", "image"}


@pytest.fixture(name="post_data")
def fixture_post_data():
return {
"name": "Test Name",
"short_description": "Test Short Description",
"description": "## Test Description\n- with markdown",
}


@pytest.fixture(name="expected_values")
def fixture_expected_values():
return {
"name": "Test Name",
"short_description": "Test Short Description",
"_description_rendered": "<h2>Test Description</h2>\n\n<ul>\n<li>with markdown</li>\n</ul>",
"slug": "test-name",
}


def test_create_category(client, db, url, superuser, post_data, expected_values):
client.force_login(superuser)
response = client.post(url, data=post_data)
assert response.status_code == 302

category = Category.objects.get()
assert {k: getattr(category, k) for k in expected_values.keys()} == expected_values
3 changes: 2 additions & 1 deletion lacommunaute/documentation/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from lacommunaute.documentation.views import CategoryDetailView, CategoryListView
from lacommunaute.documentation.views import CategoryCreateView, CategoryDetailView, CategoryListView


app_name = "documentation"
Expand All @@ -9,4 +9,5 @@
urlpatterns = [
path("", CategoryListView.as_view(), name="category_list"),
path("<str:slug>-<int:pk>/", CategoryDetailView.as_view(), name="category_detail"),
path("category/create/", CategoryCreateView.as_view(), name="category_create"),
]
25 changes: 25 additions & 0 deletions lacommunaute/documentation/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView
from taggit.models import Tag

from lacommunaute.documentation.models import Category, Document
Expand Down Expand Up @@ -31,3 +34,25 @@ def get_context_data(self, **kwargs):
context["tags"] = self.get_tags_of_documents()
context["active_tag_slug"] = self.request.GET.get("tag") or None
return context


class CreateUpdateMixin(UserPassesTestMixin):
def test_func(self):
return self.request.user.is_superuser

def get_success_url(self):
return self.object.get_absolute_url()


class CategoryCreateUpdateMixin(CreateUpdateMixin):
model = Category
template_name = "documentation/category_create_or_update.html"
fields = ["name", "short_description", "description", "image"]


class CategoryCreateView(CategoryCreateUpdateMixin, CreateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Créer une nouvelle catégorie"
context["back_url"] = reverse("documentation:category_list")
return context
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends "board_base.html" %}
{% load i18n %}
{% block sub_title %}
{{ title }}
{% endblock sub_title %}
{% block content %}
<div class="row">
<div class="col-12">
<div class="card post-edit">
<div class="card-header">
<h3 class="m-0 h4 card-title">{{ title }}</h3>
</div>
<div class="card-body">
<form method="post" action="." class="form" enctype="multipart/form-data" novalidate>
{% csrf_token %}
{% for error in post_form.non_field_errors %}
<div class="alert alert-danger">
<i class="icon-exclamation-sign"></i> {{ error }}
</div>
{% endfor %}
{% include "partials/form_field.html" with field=form.name %}
{% include "partials/form_field.html" with field=form.short_description %}
{% include "partials/form_field.html" with field=form.description %}
{% include "partials/form_field.html" with field=form.image %}
<hr class="mb-5">
<div class="form-actions form-row">
<div class="form-group col-auto">
<input type="submit" class="btn btn-primary" value="{% trans "Submit" %}" />
<a href="{{ back_url }}" class="btn btn-outline-warning">{% trans "Cancel" %}</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}

0 comments on commit 4e9a0f8

Please sign in to comment.