aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-05-26 15:56:17 +0100
committerMatthew Lemon <y@yulqen.org>2024-05-26 15:56:17 +0100
commitd209dcbbcc36e65b8e16aca94add3f39de6414ef (patch)
tree96c38b555688d97024fb771a172ce36112c106d9
parenteeaa3c469b33e614941596a04d56af3be1290456 (diff)
Basic feature slot presentation in index page
-rw-r--r--pyblackbird_cc/resources/forms.py1
-rw-r--r--pyblackbird_cc/resources/models.py4
-rw-r--r--pyblackbird_cc/resources/tests/test_models.py4
-rw-r--r--pyblackbird_cc/resources/tests/test_views.py14
-rw-r--r--pyblackbird_cc/resources/views.py10
-rw-r--r--pyblackbird_cc/templates/resources/resource_list.html140
6 files changed, 118 insertions, 55 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py
index 9320a77..75d23b4 100644
--- a/pyblackbird_cc/resources/forms.py
+++ b/pyblackbird_cc/resources/forms.py
@@ -157,4 +157,5 @@ class ResourceUpdateMetadataForm(forms.ModelForm):
"curriculum",
"main_resource_category",
"additional_resource_category",
+ "feature_slot",
]
diff --git a/pyblackbird_cc/resources/models.py b/pyblackbird_cc/resources/models.py
index 27d74e7..44a5024 100644
--- a/pyblackbird_cc/resources/models.py
+++ b/pyblackbird_cc/resources/models.py
@@ -73,7 +73,9 @@ class Resource(models.Model):
("Scottish", "Scottish"),
],
)
- feature_slot = models.IntegerField(choices=((1, 1), (2, 2), (3, 3)), unique=True, null=True)
+ feature_slot = models.IntegerField(
+ choices=((1, 1), (2, 2), (3, 3)), unique=True, null=True, blank=True
+ )
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
diff --git a/pyblackbird_cc/resources/tests/test_models.py b/pyblackbird_cc/resources/tests/test_models.py
index ec420ae..2e45d39 100644
--- a/pyblackbird_cc/resources/tests/test_models.py
+++ b/pyblackbird_cc/resources/tests/test_models.py
@@ -15,10 +15,6 @@ from pyblackbird_cc.resources.models import ResourceType
from pyblackbird_cc.resources.views import ResourceInfo
from pyblackbird_cc.resources.views import _extract_metadata_from_resource
-# TODO - convert this test into the test of a function
-# that takes a Resource object and returns all the PDF snapshots
-# file names
-
@pytest.mark.django_db()
def test_resource_model(resources):
diff --git a/pyblackbird_cc/resources/tests/test_views.py b/pyblackbird_cc/resources/tests/test_views.py
index 27a6a85..123476f 100644
--- a/pyblackbird_cc/resources/tests/test_views.py
+++ b/pyblackbird_cc/resources/tests/test_views.py
@@ -11,6 +11,7 @@ from django.urls import reverse
from .. import services
from ..models import ResourceCategory
from ..models import ResourceType
+from ..forms import ResourceCreateForm
from ..views import create_resource
@@ -21,6 +22,19 @@ def test_create_featured_resource_view(client):
assert response.status_code == 302
+@pytest.mark.django_db()
+def test_create_resource_view(client):
+ url = reverse("resources:create_resource")
+ response = client.get(url)
+ assert response.status_code == 302
+
+
+def test_create_resource_has_form(client):
+ url = reverse("resources:create_resource")
+ response = client.get(url)
+ assert type(response.context["form"]) == ResourceCreateForm
+
+
class PDFFileUploadTestCase(TestCase):
def setUp(self):
self.url = reverse("resources:create_resource")
diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py
index 9fcb77b..2bcb9c2 100644
--- a/pyblackbird_cc/resources/views.py
+++ b/pyblackbird_cc/resources/views.py
@@ -38,6 +38,7 @@ class ResourceInfo:
snapshot_urls: dict[str, list[str]]
thumbnail_filenames: list[str]
thumbnail_urls: list[str]
+ feature_slot: int
created: str
updated: str
@@ -98,6 +99,7 @@ def _extract_metadata_from_resource(resource_obj) -> ResourceInfo | None:
snapshot_urls=snapshot_url_dict,
thumbnail_filenames=resource_obj.thumbnail_filenames,
thumbnail_urls=thumbnail_urls,
+ feature_slot=resource_obj.feature_slot,
created=resource_obj.created_at,
updated=resource_obj.updated_at,
)
@@ -110,7 +112,11 @@ def _extract_metadata_from_resource(resource_obj) -> ResourceInfo | None:
def index(request):
resource_objs = Resource.objects.all()
resource_list = [_extract_metadata_from_resource(r) for r in resource_objs]
- context = {"resource_list": resource_list}
+ # featured_resources = Resource.objects.filter(feature_slot__gt=0).all()
+ featured_resources = [r for r in resource_list if r.feature_slot]
+ breakpoint()
+ featured_resources = sorted(featured_resources, key=lambda resource: resource.feature_slot)
+ context = {"resource_list": resource_list, "featured_resources": featured_resources}
return render(request, "resources/resource_list.html", context)
@@ -245,6 +251,7 @@ def create_resource(request):
curriculum = form.cleaned_data["curriculum"]
main_resource_category = form.cleaned_data["main_resource_category"]
additional_resource_category = form.cleaned_data["additional_resource_category"]
+ feature_slot = form.cleaned_data["feature_slot"]
try:
resource = Resource.objects.create(
@@ -255,6 +262,7 @@ def create_resource(request):
curriculum=curriculum,
main_resource_category=main_resource_category,
additional_resource_category=additional_resource_category,
+ feature_slot=feature_slot,
)
metadata_generator = create_metadata(pdf_files)
diff --git a/pyblackbird_cc/templates/resources/resource_list.html b/pyblackbird_cc/templates/resources/resource_list.html
index 046aa7f..eb2506a 100644
--- a/pyblackbird_cc/templates/resources/resource_list.html
+++ b/pyblackbird_cc/templates/resources/resource_list.html
@@ -2,71 +2,113 @@
{% load static %}
-{% block title %}
+ {% block title %}
Joanna Lemon Learning - Resource List
-{% endblock title %}
-{% block content %}
+ {% endblock title %}
+ {% block content %}
{% if request.user.is_authenticated and request.user.is_staff %}
- <div class="row bg-white p-4 rounded border border-success border-opacity-25">
- <h5 class="text-decoration-underline">Admin bar</h5>
- <p>
- Only you will see this bar - normal users will not see it. It allows us to
- include buttons for adding new resources, etc.
- </p>
- <div class="col">
- <div class="d-flex flex-row flex-wrap justify-content-between">
- <div>
- <a class="btn btn-primary my-md-2"
- href="{% url 'resources:create_resource' %} ">Add a new resource</a>
- </div>
- <div class="bg-danger p-2 my-2 text-dark bg-white border border-1 border-danger">
- Logged in as
- <strong>{{ request.user.email }}</strong>
- </div>
- <div class="my-md-2">
- <form action="{% url 'account_logout' %}" method="post">
- {% csrf_token %}
- <button type="submit" class="btn btn-primary">Log out</button>
- </form>
- </div>
+ <div class="row bg-white p-4 rounded border border-success border-opacity-25">
+ <h5 class="text-decoration-underline">Admin bar</h5>
+ <p>
+ Only you will see this bar - normal users will not see it. It allows us to
+ include buttons for adding new resources, etc.
+ </p>
+ <div class="col">
+ <div class="d-flex flex-row flex-wrap justify-content-between">
+ <div>
+ <a class="btn btn-primary my-md-2"
+ href="{% url 'resources:create_resource' %} ">Add a new resource</a>
+ </div>
+ <div class="bg-danger p-2 my-2 text-dark bg-white border border-1 border-danger">
+ Logged in as
+ <strong>{{ request.user.email }}</strong>
+ </div>
+ <div class="my-md-2">
+ <form action="{% url 'account_logout' %}" method="post">
+ {% csrf_token %}
+ <button type="submit" class="btn btn-primary">Log out</button>
+ </form>
</div>
</div>
</div>
- {% endif %}
- <div class="row my-4 text-center">
- <div class="col">
- <h5 class="display-6">Featured resources</h5>
- </div>
</div>
+ {% endif %}
+
<div class="row my-4">
+
+ <!-- Featured resources first -->
<div class="col">
- {% if resource_list %}
+ {% if featured_resources %}
+ <div class="row my-4 text-center">
+ <div class="col">
+ <h5 class="display-6">Featured resources</h5>
+ </div>
+ </div>
+ <div class="d-flex flex-row justify-content-between flex-wrap">
+ {% for resource in featured_resources %}
+ <div class="card mx-2 mt-2" style="width: 22rem;">
+ <img class="card-img-top"
+ src="{{ resource.thumbnail_urls|first }}"
+ alt="{{ resource.thumbnail_filename }}" />
+ <div class="card-body">
+ <h5 class="card-title">{{ resource.name }}</h5>
+ <div class="d-flex flex-row justify-content-start align-content-center">
+ <div>
+ <span class="badge bg-danger me-2">{{ resource.main_resource_category_name }}</span>
+ </div>
+ <div>
+ <span class="badge bg-secondary me-2">{{ resource.age_range }}</span>
+ </div>
+ </div>
+ <p class="card-text my-3">{{ resource.description }}</p>
+ <a href="{% url 'resources:resource_detail' resource_id=resource.id %}"
+ class="btn btn-primary">Details</a>
+ </div>
+ </div>
+ {% endfor %}
+ {% else %}
+ <p>There are no featured resources</p>
+ {% endif %}
+ </div>
+ </div>
+
+ <div class="d-flex flex-row justify-content-between flex-wrap">
+
+ <div class="col">
+ {% if resource_list %}
+ <div class="row my-4 text-center">
+ <div class="col">
+ <h5 class="display-6">Standard resources</h5>
+ </div>
+ </div>
<div class="d-flex flex-row justify-content-between flex-wrap">
{% for resource in resource_list %}
- <div class="card mx-2 mt-2" style="width: 22rem;">
- <img class="card-img-top"
- src="{{ resource.thumbnail_urls|first }}"
- alt="{{ resource.thumbnail_filename }}" />
- <div class="card-body">
- <h5 class="card-title">{{ resource.name }}</h5>
- <div class="d-flex flex-row justify-content-start align-content-center">
- <div>
- <span class="badge bg-danger me-2">{{ resource.main_resource_category_name }}</span>
- </div>
- <div>
- <span class="badge bg-secondary me-2">{{ resource.age_range }}</span>
- </div>
+ <div class="card mx-2 mt-2" style="width: 22rem;">
+ <img class="card-img-top"
+ src="{{ resource.thumbnail_urls|first }}"
+ alt="{{ resource.thumbnail_filename }}" />
+ <div class="card-body">
+ <h5 class="card-title">{{ resource.name }}</h5>
+ <div class="d-flex flex-row justify-content-start align-content-center">
+ <div>
+ <span class="badge bg-danger me-2">{{ resource.main_resource_category_name }}</span>
+ </div>
+ <div>
+ <span class="badge bg-secondary me-2">{{ resource.age_range }}</span>
</div>
- <p class="card-text my-3">{{ resource.description }}</p>
- <a href="{% url 'resources:resource_detail' resource_id=resource.id %}"
- class="btn btn-primary">Details</a>
</div>
+ <p class="card-text my-3">{{ resource.description }}</p>
+ <a href="{% url 'resources:resource_detail' resource_id=resource.id %}"
+ class="btn btn-primary">Details</a>
</div>
+ </div>
{% endfor %}
</div>
+ </div>
+
{% else %}
- <p>There are no resources</p>
+ <p>There are no resources</p>
{% endif %}
</div>
</div>
-{% endblock content %}
+ {% endblock content %}